I just deployed a tf.keras model using tensorflow 2.0 and I would like to use DeepExplainer but it creates an error message considering that
import shap
explainer = shap.DeepExplainer(model_pred, df[1][2])
When I run DeepExplainer (shap==0.28.5) with my tensorflow 2.0 model (which does not have a session anymore) I get this error message:
AttributeError Traceback (most recent call last)
in
3 # select a set of background examples to take an expectation over
4 #background = trainX[np.random.choice(trainX.shape[0], 500, replace=False)]
----> 5 explainer = shap.DeepExplainer(model_pred, df[1][2])/anaconda3/lib/python3.6/site-packages/shap/explainers/deep.py in __init__(self, model, data, session, learning_phase_flags)
127 # ...this will catch the one that keras uses
128 # we need to find them since we want to make sure learning phase flags are set to False
--> 129 if learning_phase_flags is None:
130 self.learning_phase_ops = []
131 for op in self.session.graph.get_operations():AttributeError: module 'tensorflow.python.keras.api._v2.keras.backend' has no attribute 'get_session'
I have not been looking forward to learning all the ways TF 2.0 will break things :) ...I am adding this as a todo since it will require a more in-depth review of TF 2.0 and it's impact on shap.
@slundberg @olivierblais1
I need shap to work with TF 2.0 as well, as will most people now that it is officially released.
You don't use tf.Session much anymore, so that is the issue. It has to do with eager execution being enabled by default and the backend being tensorflow by default, so no session is needed. See Tensorflow 2.0 Guide to Converting Models
Once I disable eager execution with tf.compat.v1.disable_eager_execution() I get this error: InvalidArgumentError: Operation 'dropout_6_1/cond' has no attr named '_XlaCompile'.
Is there any update on this? tf.compat.v1.disable_eager_execution() doesnt fix the issue for me and I continue to get:
session = tf.keras.backend.get_session()
AttributeError: module 'tensorflow_core.keras.backend' has no attribute 'get_session'
error
I have run tf.compat.v1.disable_eager_execution() before compiling and running the model and am still getting this error.
My understanding of TF is fairly limited, but to shift to the paradigm of Eager Execution, maybe SHAP could be implemented as a Keras Callback. That would be pretty amazing.
I just pushed a new version of GradientExplainer that supports TensorFlow 2.0 models. Updating DeepExplainer is going to take more work, since it relies a lot on graph traversal in the current session, so right now I recommend using GradientExplainer and let me know if you run into issues.
thank you! is the implementation of gradient explainer the same? i.e
explainer=shap.Gradientexplainer(model, train)?
shap_values.. etc
i continue to get an error with this..
AttributeError: module 'tensorflow_core.keras.backend' has no attribute 'get_session'
I am running a sequential model so unsure as to how this differs to the example in the vignettes for gradient explainer..
thanks!
Are you on the master version (0.31.0) or still on pypi?
I think pypi (0.30.2)..
im sorry for the confusion, i did do pip upgrade shap, but im guessing that didnt upgrade it?
You will need to either wait for the next release to drop, or you will need to git clone the repo and run python setup.py install. I plan to release the next version soon (few days).
aa awesome! thanks so much, i will wait for the push!
@slundberg This is great, thanks!
Any news on this? I feel like I might have a problem related to this
-------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-26-cc0528227812> in <module>
10
11 # explain predictions of the model on four images
---> 12 e = shap.DeepExplainer(new_model, background)
13 # ...or pass tensors directly
14 # e = shap.DeepExplainer((model.layers[0].input, model.layers[-1].output), background)
/usr/local/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)
/usr/local/lib/python3.7/site-packages/shap/explainers/deep/deep_tf.py in __init__(self, model, data, session, learning_phase_flags)
119 ksess = keras.backend.tensorflow_backend.tf_keras_backend._SESSION.session
120 if keras is not None and ksess is not None:
--> 121 session = keras.backend.get_session()
122 else:
123 try:
/usr/local/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py in get_session()
377 if not _is_tf_1():
378 raise RuntimeError(
--> 379 '`get_session` is not available '
380 'when using TensorFlow 2.0.')
381 if tf.executing_eagerly():
RuntimeError: `get_session` is not available when using TensorFlow 2.0.
I have the exact same issue! I can't run shap at all.
I installed shap with "pip install shap"
I wanted to use shap.GradientExplainer, but I get keyError 27282.
Does anybody know what this could be?, I have pasted my code and error message below:
explainer = shap.GradientExplainer(model, df)
shap_values = explainer.shap_values(df.iloc[1].values)
KeyError Traceback (most recent call last)
/mnt/c/Users/PARANJAPEAkshay/icon_work/PycharmRepo/venv_tf2/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2645 try:
-> 2646 return self._engine.get_loc(key)
2647 except KeyError:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 46649
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
----> 1 shap_values = explainer.shap_values(df.iloc[1].values)
/mnt/c/Users/PARANJAPEAkshay/icon_work/PycharmRepo/venv_tf2/lib/python3.6/site-packages/shap/explainers/gradient.py in shap_values(self, X, nsamples, ranked_outputs, output_rank_order, rseed, return_variances)
104 were chosen as "top".
105 """
--> 106 return self.explainer.shap_values(X, nsamples, ranked_outputs, output_rank_order, rseed, return_variances)
107
108
/mnt/c/Users/PARANJAPEAkshay/icon_work/PycharmRepo/venv_tf2/lib/python3.6/site-packages/shap/explainers/gradient.py in shap_values(self, X, nsamples, ranked_outputs, output_rank_order, rseed, return_variances)
251 else:
252 x = X[l][j]
--> 253 samples_input[l][k] = t * x + (1 - t) * self.data[l][rind]
254 samples_delta[l][k] = x - self.data[l][rind]
255
/mnt/c/Users/PARANJAPEAkshay/icon_work/PycharmRepo/venv_tf2/lib/python3.6/site-packages/pandas/core/frame.py in __getitem__(self, key)
2798 if self.columns.nlevels > 1:
2799 return self._getitem_multilevel(key)
-> 2800 indexer = self.columns.get_loc(key)
2801 if is_integer(indexer):
2802 indexer = [indexer]
/mnt/c/Users/PARANJAPEAkshay/icon_work/PycharmRepo/venv_tf2/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2646 return self._engine.get_loc(key)
2647 except KeyError:
-> 2648 return self._engine.get_loc(self._maybe_cast_indexer(key))
2649 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
2650 if indexer.ndim > 1 or indexer.size > 1:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 46649
Any news on this issue ? I am facing the same problem as well with DeepExplainer
AttributeError Traceback (most recent call last)
----> 1 explainer = shap.DeepExplainer(model, X_train)
2 shap_values = explainer.shap_values(X_train)
/opt/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)
/opt/anaconda3/lib/python3.7/site-packages/shap/explainers/deep/deep_tf.py in __init__(self, model, data, session, learning_phase_flags)
113 if session is None:
114 # if keras is installed and already has a session then use it
--> 115 if keras is not None and keras.backend.tensorflow_backend._SESSION is not None:
116 session = keras.backend.get_session()
117 else:
AttributeError: module 'keras.backend.tensorflow_backend' has no attribute '_SESSION'
Can anyone confirm that DeepExplainer is still not compatible with TF 2? Running into similar problems.
Most helpful comment
I have not been looking forward to learning all the ways TF 2.0 will break things :) ...I am adding this as a todo since it will require a more in-depth review of TF 2.0 and it's impact on shap.