【问题标题】:Does SHAP in Python support Keras or TensorFlow models while using DeepExplainer?Python 中的 SHAP 在使用 DeepExplainer 时是否支持 Keras 或 TensorFlow 模型?
【发布时间】:2020-04-30 06:10:03
【问题描述】:

我目前正在使用 SHAP 包来确定功能贡献。我已经将这种方法用于 XGBoost 和 RandomForest,并且效果非常好。由于我正在处理的数据是顺序数据,我尝试使用 LSTM 和 CNN 来训练模型,然后使用 SHAP 的 DeepExplainer 获取特征重要性;但它不断抛出错误。我得到的错误是:

AssertionError: <class 'keras.callbacks.History'> is not currently a supported model type!.

我也附上了示例代码(LSTM)。如果有人可以帮助我,那将很有帮助。

shap.initjs()
model = Sequential()
model.add(LSTM(n_neurons, input_shape=(X.shape[1],X.shape[2]), return_sequences=True))
model.add(LSTM(n_neurons, return_sequences=False))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
h=model.fit(X, y, epochs=nb_epochs, batch_size=n_batch, verbose=1, shuffle=True)
background = X[np.random.choice(X.shape[0],100, replace=False)]
explainer = shap.DeepExplainer(h,background)

【问题讨论】:

    标签: python tensorflow keras deep-learning shap


    【解决方案1】:

    model.fit的返回值不是模型实例;相反,它是作为keras.callbacks.History 类的实例的训练历史(即损失和度量值等统计数据)。这就是为什么当您将返回的History 对象传递给shap.DeepExplainer 时会出现上述错误的原因。相反,您应该传递模型实例本身:

    explainer = shap.DeepExplainer(model, background)
    

    【讨论】:

    • 嗨,感谢它的帮助,也让我理解了基本问题。
    • 无论创建 Keras 模型的方式如何,它都能正常工作吗?我在使用模型子类化创建神经网络时遇到问题。
    猜你喜欢
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    • 2022-07-13
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多