Shap: KeyError: 'ClipByValue' during keras regression

Created on 3 Aug 2018  路  6Comments  路  Source: slundberg/shap

Hey Scott,

thanks for your great work.

I'm using keras for regression. My data set has 5 continuous features.

Following error occurs when shap is executed:

~AppDataLocalcondacondaenvstensorflowlibsite-packagesshapexplainersdeep.py in phi_symbolic(self, i)
194 reg = tf_ops._gradient_registry._registry # hack our way in to the registry (TODO: find an API for this)
195 for n in DeepExplainer.nonlinearities:
--> 196 self.orig_grads[n] = reg[n]["type"]
197 reg[n]["type"] = self.custom_grad
198

KeyError: 'ClipByValue'

What could be the reason?

Best,
Roman

Most helpful comment

Looks like ClipByValue was removed from the gradient registry in TF 1.9, which caused this problem. I pushed a fix that should prevent these issues in the future which will go out with v0.22.3

All 6 comments

Same error in example notebook

Hello Scott,
I get the same error when running the sample notebook for the DeepExplainer for MNIST:
(shap 0.21.0, Keras 2.2.2, TensorFlow 1.9)

KeyError Traceback (most recent call last)
in ()
11 # ...or pass tensors directly
12 # e = shap.DeepExplainer((model.layers[0].input, model.layers[-1].output), background)
---> 13 shap_values = e.shap_values(x_test[1:5])

~/virtualenvs/pyenv36/lib/python3.6/site-packages/shap/explainers/deep.py in shap_values(self, X, ranked_outputs, output_rank_order)
282 # run attribution computation graph
283 feature_ind = model_output_ranks[j,i]
--> 284 sample_phis = self.run(self.phi_symbolic(feature_ind), self.model_inputs, joint_input)
285
286 # assign the attributions to the right part of the output arrays

~/virtualenvs/pyenv36/lib/python3.6/site-packages/shap/explainers/deep.py in phi_symbolic(self, i)
194 reg = tf_ops._gradient_registry._registry # hack our way in to the registry (TODO: find an API for this)
195 for n in DeepExplainer.nonlinearities:
--> 196 self.orig_grads[n] = reg[n]["type"]
197 reg[n]["type"] = self.custom_grad
198

KeyError: 'ClipByValue'

Best,
Steve

My config:
Keras==2.2.0
tensorflow-gpu==1.9.0
shap==0.21.0

Best,
Roman

Same error:

import shap
from keras.models import Sequential
from keras.layers import Dense, Activation
from sklearn.datasets.california_housing import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

cal_housing = fetch_california_housing()

ss=StandardScaler()
X=ss.fit_transform(cal_housing.data)


# split 80/20 train-test
X_train, X_test, y_train, y_test = train_test_split(X,
                                                        cal_housing.target,
                                                        test_size=0.2,
                                                      random_state=1)
names = cal_housing.feature_names

model = Sequential()
model.add(Dense(32, input_dim=X_train.shape[1]))
model.add(Activation('relu'))
model.add(Dense(16))
model.add(Activation('relu'))
model.add(Dense(1, activation="linear"))
model.compile(optimizer='rmsprop',
              loss='mse')
model.fit(X_train,y_train,batch_size=8,epochs=10)

background = X_train
# explain predictions of the model on three images
e = shap.DeepExplainer(model, background)


shap_values = e.shap_values(X_test)

KeyError Traceback (most recent call last)
in ()
4
5
----> 6 shap_values = e.shap_values(X_test)

~/anaconda3/lib/python3.6/site-packages/shap/explainers/deep.py in shap_values(self, X, ranked_outputs, output_rank_order)
282 # run attribution computation graph
283 feature_ind = model_output_ranks[j,i]
--> 284 sample_phis = self.run(self.phi_symbolic(feature_ind), self.model_inputs, joint_input)
285
286 # assign the attributions to the right part of the output arrays

~/anaconda3/lib/python3.6/site-packages/shap/explainers/deep.py in phi_symbolic(self, i)
194 reg = tf_ops._gradient_registry._registry # hack our way in to the registry (TODO: find an API for this)
195 for n in DeepExplainer.nonlinearities:
--> 196 self.orig_grads[n] = reg[n]["type"]
197 reg[n]["type"] = self.custom_grad
198

KeyError: 'ClipByValue'

Just got to this issue... I can't seem to reproduce the problem using v0.22.2 which includes some updates to DeepExplainer. Could one of you check and see if the problem still happens for you in 0.22?

Same problem using v0.22.2. Ps: keras classification

KeyError Traceback (most recent call last)
in ()
4 # explain predictions of the model on four images
5 e = shap.DeepExplainer(classifier, background)
----> 6 shap_values = e.shap_values(X_test)

~Anaconda3libsite-packagesshapexplainersdeep.py in shap_values(self, X, ranked_outputs, output_rank_order)
302 # run attribution computation graph
303 feature_ind = model_output_ranks[j,i]
--> 304 sample_phis = self.run(self.phi_symbolic(feature_ind), self.model_inputs, joint_input)
305
306 # assign the attributions to the right part of the output arrays

~Anaconda3libsite-packagesshapexplainersdeep.py in phi_symbolic(self, i)
214 reg = tf_ops._gradient_registry._registry # hack our way in to the registry (TODO: find an API for this)
215 for n in DeepExplainer.nonlinearities:
--> 216 self.orig_grads[n] = reg[n]["type"]
217 reg[n]["type"] = self.custom_grad
218

KeyError: 'ClipByValue'

Looks like ClipByValue was removed from the gradient registry in TF 1.9, which caused this problem. I pushed a fix that should prevent these issues in the future which will go out with v0.22.3

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Nithanaroy picture Nithanaroy  路  4Comments

brookewenig picture brookewenig  路  3Comments

artemmavrin picture artemmavrin  路  4Comments

franciscorodriguez92 picture franciscorodriguez92  路  4Comments

SaadAhmed96 picture SaadAhmed96  路  3Comments