I am trying to run this example from the README:
https://github.com/slundberg/shap/blob/8f736c0cf9050183f3885db87f8fc1b4eb3833e9/README.md#L134-L149
I get the following error:
AttributeError: 'NoneType' object has no attribute 'backend'
which comes from this line:
My SHAP and TensorFlow versions are
shap: 0.32.0tensorflow: 2.0.0I do not have standalone Keras installed (see below).
A full working example to reproduce the error is
import tensorflow as tf
import shap
import numpy as np
# Load data
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train = tf.image.convert_image_dtype(x_train, dtype=tf.dtypes.float32)
x_test = tf.image.convert_image_dtype(x_test, dtype=tf.dtypes.float32)
x_train = x_train.numpy()
x_test = x_test.numpy()
# Dummy model
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(units=10),
])
background = x_train[np.random.choice(x_train.shape[0], 100, replace=False)]
e = shap.DeepExplainer(model, background)
The culprit is the following lines:
I do not have standalone Keras installed (because I use tf.keras), so the import statement silently fails, and keras remains None. Later on, the following line is executed, without checking if keras is None:
Are there plans to add support for tf.keras? (NB: Fran莽ois Chollet recommends switching Keras code to tf.keras.)
If I do install standalone Keras (version 2.3.1), then my example still fails, this time with the following error:
TypeError: Tensor is unhashable if Tensor equality is enabled. Instead, use tensor.experimental_ref() as the key.
coming from this line:
Like the error says, in TensorFlow 2, tensors are not hashable, and hence cannot be used as dictionary keys (tf.Tensor.__hash__ is not available; this has to do with the new behavior of tf.Tensor.__eq__ performing element-wise comparison). Related: #850 and #880.
Yep running into this as well
I ran into this today, too.
This is an issue for me as well.
The fundamental issue here is that DeepExplainer does not yet support TF 2.0. GradientExplainer does, so I recommend using that until we build the new version of DeepExplainer for TF 2.0 (we are working on it).
Most helpful comment
The fundamental issue here is that DeepExplainer does not yet support TF 2.0. GradientExplainer does, so I recommend using that until we build the new version of DeepExplainer for TF 2.0 (we are working on it).