BUG: DeepExplainer throws error when using `__call__`
CloseChoice opened this issue · 0 comments
CloseChoice commented
Issue Description
The DeepExplainer works when using explainer.shap_values(X)
but fails when we do explainer(X)
. These interfaces should not require any additional inputs but should work interchangeable (disregarding the output here).
#2614 might be related.
Minimal Reproducible Example
# This is a slight adaption of the test test_tf_keras_mnist_cnn
rs = np.random.RandomState(random_seed)
tf.compat.v1.random.set_random_seed(random_seed)
from tensorflow import keras
from tensorflow.compat.v1 import ConfigProto, InteractiveSession
from tensorflow.keras import backend as K
from tensorflow.keras.layers import (
Activation,
Conv2D,
Dense,
Dropout,
Flatten,
MaxPooling2D,
)
from tensorflow.keras.models import Sequential
config = ConfigProto()
config.gpu_options.allow_growth = True
sess = InteractiveSession(config=config)
tf.compat.v1.disable_eager_execution()
batch_size = 64
num_classes = 10
epochs = 1
# input image dimensions
img_rows, img_cols = 28, 28
# the data, split between train and test sets
# (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
x_train = rs.randn(200, 28, 28)
y_train = rs.randint(0, 9, 200)
x_test = rs.randn(200, 28, 28)
y_test = rs.randint(0, 9, 200)
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
model = Sequential()
model.add(Conv2D(2, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape))
model.add(Conv2D(4, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(16, activation='relu')) # 128
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.legacy.Adadelta(),
metrics=['accuracy'])
model.fit(x_train[:10, :], y_train[:10, :],
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test[:10, :], y_test[:10, :]))
# explain by passing the tensorflow inputs and outputs
inds = rs.choice(x_train.shape[0], 3, replace=False)
e = shap.DeepExplainer((model.layers[0].input, model.layers[-1].input), x_train[inds, :, :])
shap_values = e.shap_values(x_test[:1]) # this works
e(x_test[:1]) # this fails
Traceback
*** AttributeError: 'DeepExplainer' object has no attribute 'masker'
Traceback (most recent call last):
File "/home/tobias/programming/github/shap/shap/explainers/_explainer.py", line 217, in __call__
if issubclass(type(self.masker), maskers.OutputComposite) and len(args)==2:
Expected Behavior
No response
Bug report checklist
- I have checked that this issue has not already been reported.
- I have confirmed this bug exists on the latest release of shap.
- I have confirmed this bug exists on the master branch of shap.
- I'd be interested in making a PR to fix this bug
Installed Versions
'0.44.2.dev35'