Shap: Keras LSTM for IMDB Sentiment Classification demo code does not work

Created on 6 May 2020  ·  9Comments  ·  Source: slundberg/shap

Since SHAP does not support Keras anymore, I modified the code to use tf.keras. However, it stopped at the following line in Cell [3]:
shap_values = explainer.shap_values(x_test[:10])

The error message is:
TypeError: 'NoneType' object cannot be interpreted as an integer

Most helpful comment

I see the same error, would really like some help as well.

All 9 comments

Can you provide a reference to what CHAP is?

I meant SHAP.

I see the same error, would really like some help as well.

does not fully solve the problem, but downgrading to TensorFlow 1.14.0 helped to run it.

Same issue here :/
Trying to use SHAP to explain a RNN. The input shape is (rows, lag, features). Rows is variable, so it is declared as (None, 2).

# rnn model has an input shape of (None, 2)
# data has shape (500, 60, 2)
e = shap.DeepExplainer(rnn_model, data)
# test1 has shape (500, 60, 2)
shap_val = e.shap_values(test1) # this line gives the error!
TypeError                                 Traceback (most recent call last)
<ipython-input-105-328a0c2307b4> in <module>()
      6 test1 = X_train[random_ind[500:1000]]
      7 print(test1.shape)
----> 8 shap_val = e.shap_values(test1)
      9 shap_val = np.array(shap_val)
     10 shap_val = np.reshape(shap_val, (int(shap_val.shape[1]), int(shap_val.shape[2]), int(shap_val.shape[3])))

4 frames
/usr/local/lib/python3.6/dist-packages/shap/explainers/deep/deep_tf.py in anon()
    352                     shape = list(self.model_inputs[i].shape)
    353                     shape[0] = -1
--> 354                     data = X[i].reshape(shape)
    355                     v = tf.constant(data, dtype=self.model_inputs[i].dtype)
    356                     inputs.append(v)

TypeError: 'NoneType' object cannot be interpreted as an integer

I've tried manually editing the deep_tf.py file but no ball.

Anyone knows a solution?

The problem seems to be that the input shape is not defined for the LSTM (i.e., it can take inputs of arbitrary length) but DeepExplainer requires a fixed input shape.

You can check the input shape as follows:

>>> model.input_shape
(None, None)
````

If that is indeed the problem, you can circumvent this problem by wrapping the `model` in a model with defined input shape:

new_model_input = tf.keras.layers.Input(shape=(1000,))
new_model_output = model(new_model_input)
new_model = tf.keras.models.Model(inputs=new_model_input, outputs=new_model_output)
```

I guess that's something which could be done automatically, with the input shape inferred from the background samples.

@ionicsolutions I am not able to run the demo with the suggested solution. I get
LookupError: gradient registry has no entry for: shap_TensorListStack

Is it because of TF2 ?

I used tensorflow2.3 version and encountered NoneType Error.
I tried your method @ionicsolutions , but I still encountered an error of “LookupError: gradient registry has no entry for: shap_TensorListStack” using DeepExplainer for Keras Model.
Finally, I uninstalled tf2.3, reinstalled version 1.15, and then there was no error

@ionicsolutions I am not able to run the demo with the suggested solution. I get
LookupError: gradient registry has no entry for: shap_TensorListStack

Is it because of TF2 ?

I also think it is because of tf2.3. I tried tf1.15 and there is no error. Do you have a solution when using tf2.3?

Was this page helpful?
0 / 5 - 0 ratings