I am implementing a series of three deep learning models (RNN, 1D-CNN, and custom transformer), all utilizing the Keras API, for an NLP binary classification problem. I would like to generate a feature importance chart, similar to the one shown in the question linked here: Feature Importance Chart in neural network using Keras in Python
I have already run the models and have results, but I would like to show the feature importance chart as well. Here is the model architecture for the 1D-CNN model, which I will use as the example in this question:
from keras import layers
from keras.layers import Input, Embedding, Conv1D, Dropout, GlobalMaxPooling1D, Dense
from keras.optimizers import Adam
from keras.regularizers import L2
def create_model():
embedding_dim = 500
input = Input(shape = (maxlen,))
x = Embedding(input_dim = vocab_size,
output_dim = embedding_dim, input_shape = (1000,))(input)
x = Conv1D(256, 7, padding = 'valid', activation = 'gelu', strides = 3, kernel_regularizer = L2(0.01))(x)
x = Dropout(0.5)(x)
x = Conv1D(256, 7, padding = 'valid', activation = 'gelu', strides = 3, kernel_regularizer = L2(0.01))(x)
x = Dropout(0.5)(x)
x = Conv1D(256, 7, padding = 'valid', activation = 'gelu', strides = 3, kernel_regularizer = L2(0.01))(x)
x = GlobalMaxPooling1D()(x)
x = Dense(256, activation = 'gelu')(x)
x = Dropout(0.2)(x)
x = Dense(128, activation = 'gelu')(x)
x = Dropout(0.2)(x)
x = Dense(64, activation = 'gelu')(x)
x = Dropout(0.2)(x)
x = Dense(32, activation = 'gelu')(x)
x = Dropout(0.2)(x)
class_1 = Dense(1, activation = 'sigmoid', name = 'class_1')(x)
class_2 = Dense(1, activation = 'sigmoid', name = 'class_2')(x)
class_3 = Dense(1, activation = 'sigmoid', name = 'class_3')(x)
class_4 = Dense(1, activation = 'sigmoid', name = 'class_4')(x)
class_5 = Dense(1, activation = 'sigmoid', name = 'class_5')(x)
class_6 = Dense(1, activation = 'sigmoid', name = 'class_6')(x)
opt = Adam(learning_rate = 0.001)
model = keras.Model(
inputs = [input],
outputs = [class_1, class_2, class_3, class_4, class_5, class_6]
)
model.compile(optimizer = opt,
loss = {'class_1' : 'binary_crossentropy', 'class_2' : 'binary_crossentropy', 'class_3' : 'binary_crossentropy', 'class_4' : 'binary_crossentropy', 'class_5' : 'binary_crossentropy', 'class_6' : 'binary_crossentropy'},
metrics = ['accuracy', 'accuracy', 'accuracy', 'accuracy', 'accuracy', 'accuracy']
)
return model
Other Stack Overflow questions regarding this issue worked due to using a Sequential model, but I would like to continue using the Functional API. These questions are linked here:
Feature Importance keras regressionmodel
Feature Importance Chart in neural network using Keras in Python
Is there any way to get variable importance with Keras?
Feature importance with keras
I have attempted to use eli5, but this requires a Keras wrapper for a sequential model. SHAP also requires me to re-train the model, which is not desired.