Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does SHAP in Python support Keras or TensorFlow models while using DeepExplainer?

I am currently using SHAP Package to determine the feature contributions. I have used the approach for XGBoost and RandomForest and it worked really well. Since the data I am working on is a sequential data I tried using LSTM and CNN to train the model and then get the feature importance using the SHAP's DeepExplainer; but it is continuously throwing error. The error I am getting is:

AssertionError: <class 'keras.callbacks.History'> is not currently a supported model type!.

I am attaching the sample code as well (LSTM). It would be helpful if someone could help me with it.

shap.initjs()
model = Sequential()
model.add(LSTM(n_neurons, input_shape=(X.shape[1],X.shape[2]), return_sequences=True))
model.add(LSTM(n_neurons, return_sequences=False))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
h=model.fit(X, y, epochs=nb_epochs, batch_size=n_batch, verbose=1, shuffle=True)
background = X[np.random.choice(X.shape[0],100, replace=False)]
explainer = shap.DeepExplainer(h,background)
like image 549
Yash Sharma Avatar asked Apr 30 '20 06:04

Yash Sharma


People also ask

Does Shap work for neural networks?

SHAP values (SHapley Additive exPlanations) is an awesome tool to understand your complex Neural network models and other machine learning models such as Decision trees, Random forests. Basically, it visually shows you which feature is important for making predictions.

How does Shap deep explainer work?

Explain Image Classification by SHAP Deep ExplainerImage classification tasks can be explained by the scores on each pixel on a predicted image, which indicates how much it contributes to classifying that image into a particular class.

How do you use Shap in neural network?

An example in Python with neural networksLet's first install shap library. Then, let's import it and other useful libraries. Now we can load our dataset and the feature names, that will be useful later. We can now split our dataset into training and test.


1 Answers

The returned value of model.fit is not the model instance; rather, it's the history of training (i.e. stats like loss and metric values) as an instance of keras.callbacks.History class. That's why you get the mentioned error when you pass the returned History object to shap.DeepExplainer. Instead, you should pass the model instance itself:

explainer = shap.DeepExplainer(model, background)
like image 144
today Avatar answered Sep 27 '22 18:09

today