Shap: Unexpectedly found an instance of type `<class 'numpy.ndarray'>`. Expected a symbolic tensor instance.

Created on 17 Apr 2020  路  2Comments  路  Source: slundberg/shap

I was trying to just run notebook at https://slundberg.github.io/shap/notebooks/deep_explainer/Keras%20LSTM%20for%20IMDB%20Sentiment%20Classification.html

but i am getting below error for DeepExapliner

ValueError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/keras/engine/base_layer.py in assert_input_compatibility(self, inputs)
309 try:
--> 310 K.is_keras_tensor(x)
311 except ValueError:

6 frames
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in is_keras_tensor(x)
696 raise ValueError('Unexpectedly found an instance of type ' + --> 697 str(type(x)) + '. '
698 'Expected a symbolic tensor instance.')

ValueError: Unexpectedly found an instance of type <class 'numpy.ndarray'>. Expected a symbolic tensor instance.

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last)
in ()
6 print(type(background))
7 # explain predictions of the model on three images
----> 8 e = shap.DeepExplainer(model, background)
9 # # ...or pass tensors directly
10 # # e = shap.DeepExplainer((model.layers[0].input, model.layers[-1].output), background)

/usr/local/lib/python3.6/dist-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.6/dist-packages/shap/explainers/deep/deep_tf.py in __init__(self, model, data, session, learning_phase_flags)
152 if type(self.model)is tuple:
153 sel.fModel(cnn.inputs, cnn.get_layer(theNameYouWant).outputs)
--> 154 self.expected_value = tf.reduce_mean(self.model(self.data), 0)
155
156 if not tf.executing_eagerly():

/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in symbolic_fn_wrapper(args, *kwargs)
73 if _SYMBOLIC_SCOPE.value:
74 with get_graph().as_default():
---> 75 return func(args, *kwargs)
76 else:
77 return func(args, *kwargs)

/usr/local/lib/python3.6/dist-packages/keras/engine/base_layer.py in __call__(self, inputs, **kwargs)
470 # Raise exceptions in case the input is not compatible
471 # with the input_spec set at build time.
--> 472 self.assert_input_compatibility(inputs)
473
474 # Handle mask propagation.

/usr/local/lib/python3.6/dist-packages/keras/engine/base_layer.py in assert_input_compatibility(self, inputs)
314 'Received type: ' +
315 str(type(x)) + '. Full input: ' +
--> 316 str(inputs) + '. All inputs to the layer '
317 'should be tensors.')
318

ValueError: Layer sequential_4 was called with an input that isn't a symbolic tensor. Received type: . Full input: [array([[[[0.],
[0.],

Most helpful comment

Check to see if you are seeing any log output like: keras is no longer supported, please use tf.keras instead.

I just resolved this issue by ensuring that all usages of keras were replaced with tensorflow.keras in my imports. It appears that breaking backwards compatibility has happened hard and fast. In my case, this was caused by from keras.models import Model. I replaced it with from tensorflow.keras.models import Model, and the problem stopped.

All 2 comments

Any updates here?

Check to see if you are seeing any log output like: keras is no longer supported, please use tf.keras instead.

I just resolved this issue by ensuring that all usages of keras were replaced with tensorflow.keras in my imports. It appears that breaking backwards compatibility has happened hard and fast. In my case, this was caused by from keras.models import Model. I replaced it with from tensorflow.keras.models import Model, and the problem stopped.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

artemmavrin picture artemmavrin  路  4Comments

Nithanaroy picture Nithanaroy  路  4Comments

resdntalien picture resdntalien  路  3Comments

SaadAhmed96 picture SaadAhmed96  路  3Comments

franciscorodriguez92 picture franciscorodriguez92  路  4Comments