Hi,
I am getting the following error when trying to use SHAP with TF 2.
Versions:
shap-0.30.0
tensorflow==2.0.0
type(mdl) is tensorflow.python.keras.engine.training.Model
TypeError Traceback (most recent call last)
----> 1 e = shap.DeepExplainer(mdl, Xtrain[0,:])
~/anaconda3/lib/python3.7/site-packages/shap/explainers/deep/__init__.py in __init__(self, model, data, session, learning_phase_flags)
78
79 if framework == 'tensorflow':
---> 80 self.explainer = TFDeepExplainer(model, data, session, learning_phase_flags)
81 elif framework == 'pytorch':
82 self.explainer = PyTorchDeepExplainer(model, data)
~/anaconda3/lib/python3.7/site-packages/shap/explainers/deep/deep_tf.py in __init__(self, model, data, session, learning_phase_flags)
142 if self.data[0].shape[0] > 5000:
143 warnings.warn("You have provided over 5k background samples! For better performance consider using smaller random sample.")
--> 144 self.expected_value = self.run(self.model_output, self.model_inputs, self.data).mean(0)
145
146 # find all the operations in the graph between our inputs and outputs
~/anaconda3/lib/python3.7/site-packages/shap/explainers/deep/deep_tf.py in run(self, out, model_inputs, X)
282 """ Runs the model while also setting the learning phase flags to False.
283 """
--> 284 feed_dict = dict(zip(model_inputs, X))
285 for t in self.learning_phase_flags:
286 feed_dict[t] = False
~/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py in __hash__(self)
711 if (Tensor._USE_EQUALITY and executing_eagerly_outside_functions() and
712 (g is None or g._building_function)): # pylint: disable=protected-access
--> 713 raise TypeError("Tensor is unhashable if Tensor equality is enabled. "
714 "Instead, use tensor.experimental_ref() as the key.")
715 else:
TypeError: Tensor is unhashable if Tensor equality is enabled. Instead, use tensor.experimental_ref() as the key.
I also have this problem, and som googling seems to stuggest that others packages using tensorflow has had the same kind of error. They were resolved by patching the package.
To fix this temporarily until they issue a patch of SHAP I would stuggest disabeling tensorflow v2 functionality if you don't really need it.
tf.compat.v1.disable_v2_behavior()
The master version of SHAP now has support for TF 2.0 in GradientExplainer (not DeepExplainer yet). We get pushed in a release soon.
Tutorial Code (using Shap 0.31.0) with tensorflow 2.0 does not work yet.
# load pre-trained model and choose two images to explain
model = VGG16(weights='imagenet', include_top=True)
X,y = shap.datasets.imagenet50()
to_explain = X[[39,41]]
# load the ImageNet class names
url = "https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json"
fname = shap.datasets.cache(url)
with open(fname) as f:
class_names = json.load(f)
# explain how the input to the 7th layer of the model explains the top two classes
def map2layer(x, layer):
feed_dict = dict(zip([model.layers[0].input], [preprocess_input(x.copy())]))
return K.get_session().run(model.layers[layer].input, feed_dict)
e = shap.GradientExplainer(
(model.layers[7].input, model.layers[-1].output),
map2layer(X, 7),
local_smoothing=0 # std dev of smoothing noise
)
shap_values,indexes = e.shap_values(map2layer(to_explain, 7), ranked_outputs=2)
# get the names for the classes
index_names = np.vectorize(lambda x: class_names[str(x)][1])(indexes)
# plot the explanations
shap.image_plot(shap_values, to_explain, index_names)
print(shap.__version__)
'0.31.0'
switch to "tf.compat.v1.disable_v2_behavior()" still does not work, "RuntimeError: get_session is not available when using TensorFlow 2.0."
Works for me with tensorflow.compat.v1.disable_v2_behavior() importing get_session() from tensorflow.compat.v1.keras.backend:
import tensorflow
from tensorflow.compat.v1.keras.backend import get_session
tensorflow.compat.v1.disable_v2_behavior()
I have some dependencies on TF 2.0/TF 2.1, hence downgrading to TF 1.* breaks other pipeline.
Is it possible to get DeepExplainer compatibility with >= TF 2.0 soon?
Is SHAP still incompatible with TF 2.0? I am having some issue when trying to explain my subclassed model
Any update on DeepExplainer compatibility with TF >= 2.0?
Most helpful comment
The master version of SHAP now has support for TF 2.0 in GradientExplainer (not DeepExplainer yet). We get pushed in a release soon.