python - 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].
有任何想法吗?提前致谢!
解决方案
推荐阅读
- c# - 无法使用 LINQ 连接从右侧列表中选择值
- python-3.x - 如何以所有可能的方式将列表划分为大小为 k 的子字符串
- robotframework - 使用ride.py 命令打开 RIDE(机器人框架 IDE)时出错
- react-native - 有什么方法可以在 React Native for iOS 中实现真正的暗模式?
- visual-studio - 特定于配置的 resw 资源字符串变体
- asp.net - 更新表单下拉列表的角度设置值
- python - Html 无法识别块、结束块参数。Django 查询
- javascript - React CRUD 应用程序中的错误:TypeError:无法解构“this.props.event”的属性“id”,因为它未定义
- c++ - 如何为 RGB 键盘编写微控制器?
- python - 将烧瓶部署到单个服务器