I want to use LIME for explaining my models results.
I have a DNN model ending with 1 neuron output layer with sigmoid function.
Nearly ALL examples I came across the internet had examples either on regression, or DT models.
I couldn’t find any matching my case.
DNN = tf.keras.models.Sequential([
#Input layer with 121 (nb of features) neurons
tf.keras.layers.Input(121),
#1st hidden layer with 182 neurons
tf.keras.layers.Dense(182, activation='relu'),
#Dropout layer with rate = 0.2
tf.keras.layers.Dropout(0.2),
#2nd hidden layer with 182 neurons
tf.keras.layers.Dense(182, activation='relu'),
#Ouput layer with 1 neuron
tf.keras.layers.Dense(1,activation='sigmoid')
])
DNN.compile(optimizer='adam',
loss="binary_crossentropy",
metrics=['accuracy'])
DNN_trained=DNN.fit(nsl_kdd_train_X2, nsl_kdd_train_Y2, epochs=5)
#lime
from lime import lime_tabular
explainer = lime_tabular.LimeTabularExplainer( training_data = np.array(nsl_kdd_train_X2),
feature_names = nsl_kdd_data_X2.columns,
class_names = ['normal', 'anomaly'],
mode="classification"
)
explainer = lime_tabular.LimeTabularExplainer(nsl_kdd_test_X2, mode='classification',feature_names=nsl_kdd_data_X2.columns, verbose=True) #,training_labels=nsl_kdd_data['class']
explanation = explainer.explain_instance(nsl_kdd_test_X2[1], DNN.predict_proba, num_features=len(nsl_kdd_data_X2.columns))
it will give error: “AttributeError: ‘Sequential’ object has no attribute ‘predict_proba'”
so predict_proba is not available, and only predict function is, which outputs 0 or 1 only.
I tried to borrow this part of code:
def prob(data):
return np.array(list(zip(1-model.predict(data),model.predict(data))))
from
https://towardsdatascience.com/decrypting-your-machine-learning-model-using-lime-5adc035109b5
but it transformed the data into 3D array, which LIME explain_instance function complained from. I’m not sure why it became 3D.