Shap: "NameError: name 'Model' is not defined" when running example

Created on 28 Apr 2020  路  3Comments  路  Source: slundberg/shap

I'm trying to run the MNIST example notebook on a Kaggle notebook.

I'm importing keras through the tensorflow submodule, so I changed the initial imports by adding tensorflow. at the beginning:

import tensorflow.keras as keras
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras import backend as K

I would like to explicitly pass the layers of the network to explain, but when I run the line

e = shap.DeepExplainer((model.layers[0].input, model.layers[-1].output), background)

I get the following error

NameError                                 Traceback (most recent call last)
<ipython-input-6-4ea374755383> in <module>
     10 #e = shap.DeepExplainer(model, background)
     11 # ...or pass tensors directly
---> 12 e = shap.DeepExplainer((model.layers[0].input, model.layers[-1].output), background)

/opt/conda/lib/python3.6/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/conda/lib/python3.6/site-packages/shap/explainers/deep/deep_tf.py in __init__(self, model, data, session, learning_phase_flags)
    105             if type(model) is tuple or type(model) is list:
    106                 assert len(model) == 2, "When a tuple is passed it must be of the form (inputs, outputs)"
--> 107                 self.model = Model(model[0], model[1])
    108             else:
    109                 self.model = model

NameError: name 'Model' is not defined

My libraries versions are:

> print(tensorflow.__version__)
2.1.0
> print(tensorflow.keras.__version__)
2.2.4-tf
> print(shap.__version__)
0.35.0

I am probably missing something obvious, sorry but I am new to Python. I would like to understand if it is a version issue or something else and how to solve it.

Most helpful comment

It looks like there are a few errors and incomplete lines in the deep_tf.py module.
This line just needs to be changed to:
self.model = keras.Model(model[0], model[1])

All 3 comments

It looks like there are a few errors and incomplete lines in the deep_tf.py module.
This line just needs to be changed to:
self.model = keras.Model(model[0], model[1])

Thank you, modifying the following lines solved this particular issue

```
90 - import keras
90 + import tensorflow.keras as keras

107 - self.model = Model(model[0], model[1])
107 + self.model = keras.Model(model[0], model[1])
````

I received the same error message " name 'Model' is not defined" when trying to make image_plots. Is the updates available in more later version of SHAP? Thank you.

# select a set of background examples to take an expectation over
background = x_train[np.random.choice(x_train.shape[0], 100, replace=False)]

# explain predictions of the model on four images
e = shap.DeepExplainer((model.layers[0].input, model.layers[-1].output), background)

shap_values = e.shap_values(x_test[1:5])

# plot the feature attributions
shap.image_plot(shap_values, -x_test[1:5]).

Here is my version:
print(tensorflow.__version__)
print(tensorflow.keras.__version__)
print(shap.__version__)

2.4.0-dev20200721
2.4.0
0.35.0

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SaadAhmed96 picture SaadAhmed96  路  3Comments

SSMK-wq picture SSMK-wq  路  4Comments

yolle103 picture yolle103  路  3Comments

franciscorodriguez92 picture franciscorodriguez92  路  4Comments

cbeauhilton picture cbeauhilton  路  3Comments