如何在SHAP库中使用GradientExplainer(Tensorflow 2.0)?

2 人关注

你能不能提示我,在SHAP库中的 shap.GradientExplainer() 要放什么参数(使用Tensorflow 2.0)。

There is the only 例子 in internet linked with images classification. In the 例子, 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)

但我的模型是顺序式的see here. 当我试图把2个输入作为第一个参数时

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

,发生错误

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

When I use 1 input

shap_values = explainer.shap_values(model.output)

,又出现了一个错误。

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]`

然后,我尝试从数据集中输入数据。

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