首页 > 解决方案 > 解释递归神经网络特征 (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

提前致谢

标签: tensorflowkeraslstmrecurrent-neural-networkshap

解决方案


我设法通过在我的代码开头添加来修复它

import tensorflow as tf
tf.compat.v1.disable_v2_behavior()

Shap 不适用于 2.4.1 tensorflow 版本


推荐阅读