2

Would you prompt me, what arguments to put in shap.GradientExplainer() in SHAP library (using Tensorflow 2.0).

There is the only example in internet linked with images classification. In the example, the function accepts 2 inputs as the 1st argument:

model.layers[7].input, model.layers[-1].output

The full code snippet:

# explain how the input to the 7th layer of the model explains the top two classes
def map2layer(x, layer):
    feed_dict = dict(zip([model.layers[0].input], [preprocess_input(x.copy())]))
    return K.get_session().run(model.layers[layer].input, feed_dict)
e = shap.GradientExplainer((model.layers[7].input, model.layers[-1].output), map2layer(preprocess_input(X.copy()), 7))
shap_values,indexes = e.shap_values(map2layer(to_explain, 7), ranked_outputs=2)

But my model is Sequential see here. When I try to put 2 inputs as the first argument

shap_values = explainer.shap_values((model.input, model.output))

, an error occurs

Layer sequential expects 1 inputs, but it received 2 input tensors. 

When I use 1 input

shap_values = explainer.shap_values(model.output)

, another error occurs:

Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 46 but received input with shape `[None, 1]`

Then, I try to input data from dataset:

shap_values = explainer.shap_values(df3.to_numpy()[:100,:])

The error occurs:

KeyError: 31467

If you don't have an answer, may be, you know another example of using GradientExplainer.

1
  • As an alternative, alibi's integrated gradients explanation does a similar computation and does work with TF2 for me. Commented Feb 24, 2021 at 13:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.