BiRNN Keras 模型中的 SHAP 解释

时间:2021-02-11 07:41:02

标签: python tensorflow machine-learning neural-network shap

我尝试使用带有 3D 输入的基本模型 LSTM 运行 SHAP。

import shap
import tensorflow.keras.backend 
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from tensorflow.keras.models import load_model
import shap

tf.compat.v1.disable_eager_execution()
regressor = load_model(Model_name)

pred_x = regressor.predict_classes(X_Training)

#explainer  = shap.DeepExplainer((regressor.layers[0].input, regressor.layers[-1].output),data)
background = X_Training[np.random.choice(X_Training.shape[0], 200, replace=False)]
explainer  = shap.DeepExplainer(regressor,background)


test = X_Training[np.random.choice(X_Training.shape[0], 20, replace=False)]
shap_val = explainer.shap_values(test,check_additivity=False)
shap_val = np.array(shap_val)
shap_val = np.reshape(shap_val,(int(shap_val.shape[1]),int(shap_val.shape[2]),int(shap_val.shape[3])))
shap_abs = np.absolute(shap_val)

sum_0 = np.sum(shap_abs,axis=0)
f_names = ['A','B','C','D','E','F','G','H','K','L','M','Results']

shap.initjs()
shap.summary_plot(
    shap_val[0], 
    #X_Testing,
    feature_names=f_names[:-1],
    max_display=50,
    plot_type='bar')

输出如下图。 enter image description here

模型摘要()

enter image description here

我检查了 shap_values,所有值都为零。

enter image description here

shap 和 tensorflow 的版本分别是 0.37.0 和 2.3.0。

0 个答案:

没有答案