解释递归神经网络特征 (RNN/LSTM)

Interpreting recurrent neural networks features (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 版本