首页 > 解决方案 > Keras LSTM:使用 SHAP 的特征重要性条形图

问题描述

我正在研究用于时间序列预测的 LSTM。现在我想了解,哪些功能对输出的影响最大,哪些不重要。目标是像在 pycharm 中使用 matplotlib 的条形图(参考:https ://github.com/slundberg/shap ): 在此处输入图像描述

这是我的模型:

model = Sequential()
model.add(LSTM(num_neurons, activation='relu', input_shape=(train_x.shape[1], train_x.shape[2]), return_sequences=True))
model.add(SeqWeightedAttention())
model.add(Dropout(dropout))
model.add(Dense(1))
adam_opt = keras.optimizers.Adam(lr=adam_lr, beta_1=adam_b1, beta_2=adam_b2, amsgrad=False)
model.compile(loss='mae', optimizer=adam_opt, metrics=[metrics.mae, metrics.mape, metrics.mse])
history = model.fit(train_x, train_y, epochs=n_epoch, batch_size=batch_size, validation_data=(val_x, val_y), verbose=2, shuffle=False, callbacks=[early_stop, checkpoint])

shap.initjs()
background = train_x[np.random.choice(train_x.shape[0], 100, replace=False)]
explainer = shap.DeepExplainer(model, background)
shap_values = explainer.shap_values(val_x)
shap.summary_plot(shap_values, train_x, plot_type="bar", matplotlib=True)
plt.show()

在计算 shap_values (shap_values = ...) 时出现以下错误:

ValueError: Dimension 2 in both shapes must be equal, but are 20 and 1. Shapes are [?,3,20] and [?,?,1]. for 'gradients/seq_weighted_attention_1/mul_grad/Select_1' (op: 'Select') with input shapes: [?,?,1], [?,3,20], [?,3,20].

有任何想法吗?提前致谢!

标签: pythontensorflowkerasdeep-learningshap

解决方案


推荐阅读