tensorflow - 解释递归神经网络特征 (RNN/LSTM)
问题描述
我尝试使用 shap 来进行特征重要性分析。我正在使用 keras,我想获得条形图和小提琴图。使用我的 DNN,我得到了类似的东西: Violin chart bar chart
然而,当我用我的 SimpleRNN 尝试它时,我遇到了形状问题。输入形状是(样本,时间,特征),而我的输出形状是(样本,特征)。所以它是一个多对一的RNN。我在静态模型中使用的 KernelExplainer 因尺寸问题而无法工作。DeepExplainer 也不起作用。它告诉我这个错误:
Your TensorFlow version is newer than 2.4.0 and so graph support has been removed in eager mode. See PR #1483 for discussion.
Traceback (most recent call last):
File "..."
... = model.predict(data)
File "...", line 103, in predict
explainer = shap.DeepExplainer(self.model, self.background)
File ".../lib/python3.6/site-packages/shap/explainers/_deep/__init__.py", line 84, in __init__
self.explainer = TFDeep(model, data, session, learning_phase_flags)
File ".../lib/python3.6/site-packages/shap/explainers/_deep/deep_tf.py", line 131, in __init__
self.graph = _get_graph(self)
File ".../lib/python3.6/site-packages/shap/explainers/tf_utils.py", line 46, in _get_graph
return explainer.model_output.graph
AttributeError: 'KerasTensor' object has no attribute 'graph'
你知道这个错误可能是由于什么原因造成的吗?在这里,我看到有人对 shap 和 LSTM 有类似问题,但没有明确的解决方案。您是否推荐一种替代方法来获取这些图表?
我的 tensorflow 版本是 2.4.1,我的 shap 版本是 0.38.1
提前致谢
解决方案
我设法通过在我的代码开头添加来修复它
import tensorflow as tf
tf.compat.v1.disable_v2_behavior()
Shap 不适用于 2.4.1 tensorflow 版本
推荐阅读
- vue.js - 是否可以在 Vue 中使 props 有条件,例如 prop2 取决于 prop1 的值?
- regex - 用正则表达式循环括号
- spring-integration - 拆分后在spring cloud integration flow中所有消息都放入kafka后执行一个方法
- swift - 以替代格式附加字符
- python - 如何在 Python 中解析“这里的文档”?
- android - 如何清除当前的 MediaPlayer 播放列表?
- python-3.x - 使用 ConfigParser 读取部分下的列表
- c# - 使用第一个 foreach 词干中的元素嵌套 Foreach 语句
- python - 为什么替换功能没有用新列表替换我的列表?
- wordpress - Woocommerce 优惠券允许的电子邮件限制不起作用