在使用DeepExplainer时,Python中的SHAP是否支持Keras或TensorFlow模型?

问题描述 投票:0回答:1

我目前正在使用SHAP Package来确定特征贡献。我已经为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个回答
1
投票

的返回值 model.fit 并不是模型实例,而是作为一个实例的训练历史(即损失和度量值等统计数据)。keras.callbacks.History 类。这就是为什么当你把返回的 History 反对 shap.DeepExplainer. 相反,你应该传递模型实例本身。

explainer = shap.DeepExplainer(model, background)
© www.soinside.com 2019 - 2024. All rights reserved.