Pytorch 有人知道为什么SHAP';s的深度解释在ResNet-50预训练模型上失败?

Pytorch 有人知道为什么SHAP';s的深度解释在ResNet-50预训练模型上失败?,pytorch,resnet,shap,Pytorch,Resnet,Shap,我已经培训了一个ResNet-50模型,我正在尝试实现解释性方法SHAP。我发现这一直是中国其他人的一个问题 我的代码严格遵循中SHAP pytorch示例中的说明: 回溯错误为: Traceback (most recent call last): File "/.../PycharmProjects/thesis/SHAP.py", line 133, in <module> main() File ".../PycharmProje

我已经培训了一个ResNet-50模型,我正在尝试实现解释性方法SHAP。我发现这一直是中国其他人的一个问题

我的代码严格遵循中SHAP pytorch示例中的说明:

回溯错误为:

Traceback (most recent call last):
  File "/.../PycharmProjects/thesis/SHAP.py", line 133, in <module>
    main()
  File ".../PycharmProjects/thesis/SHAP.py", line 123, in main
    shap_values = e.shap_values(test_images)
  File ".../anaconda3/lib/python3.8/site-packages/shap/explainers/_deep/__init__.py", line 124, in shap_values
    return self.explainer.shap_values(X, ranked_outputs, output_rank_order, check_additivity=check_additivity)
  File ".../anaconda3/lib/python3.8/site-packages/shap/explainers/_deep/deep_pytorch.py", line 185, in shap_values
    sample_phis = self.gradient(feature_ind, joint_x)
  File ".../anaconda3/lib/python3.8/site-packages/shap/explainers/_deep/deep_pytorch.py", line 121, in gradient
    grad = torch.autograd.grad(selected, x,
  File ".../anaconda3/lib/python3.8/site-packages/torch/autograd/__init__.py", line 202, in grad
    return Variable._execution_engine.run_backward(
  File ".../anaconda3/lib/python3.8/site-packages/shap/explainers/_deep/deep_pytorch.py", line 226, in deeplift_grad
    return op_handler[module_type](module, grad_input, grad_output)
  File ".../anaconda3/lib/python3.8/site-packages/shap/explainers/_deep/deep_pytorch.py", line 358, in nonlinear_1d
    grad_output[0] * (delta_out / delta_in).repeat(dup0))
RuntimeError: The size of tensor a (512) must match the size of tensor b (2048) at non-singleton dimension 1
回溯(最近一次呼叫最后一次):
文件“/…/PycharmProjects/thesis/SHAP.py”,第133行,在
main()
文件“../PycharmProjects/thesis/SHAP.py”,第123行,主目录
形状值=e.形状值(测试图像)
文件“../anaconda3/lib/python3.8/site packages/shap/explainers/_deep/_init__.py”,第124行,在shap_值中
返回self.explainer.shap\u值(X,排列输出,输出排列顺序,检查可加性=检查可加性)
文件“../anaconda3/lib/python3.8/site packages/shap/explainers/\u deep/deep\u pytorch.py”,第185行,以shap\u值表示
示例\u phis=自梯度(特征\u ind,关节\u x)
文件“../anaconda3/lib/python3.8/site packages/shap/explainers/\u deep/deep\u pytorch.py”,第121行,渐变
梯度=火炬自动加载梯度(选定,x,
文件“../anaconda3/lib/python3.8/site packages/torch/autograd/_init__.py”,第202行,渐变
返回变量。\u执行\u引擎。向后运行\u(
文件“../anaconda3/lib/python3.8/site packages/shap/explainers/\u deep/deep\u pytorch.py”,第226行,在deeplift\u grad中
返回操作处理程序[模块类型](模块、梯度输入、梯度输出)
文件“../anaconda3/lib/python3.8/site packages/shap/explainers/\u deep/deep\u pytorch.py”,第358行,非线性1d
梯度输出[0]*(增量输出/增量输入)。重复(dup0))
RuntimeError:张量a(512)的大小必须与张量b(2048)在非单态维度1的大小相匹配
有人知道是什么原因导致了这种情况吗?我已经检查了代码很多次,但我找不到哪里出错了……在经过培训的网络体系结构上,张量大小不应该不匹配

请注意,我故意删除了任何个人识别路径:)


非常感谢您的任何意见

我认为发生此错误是因为ResNet在FC层中需要softmax。尝试这样做:

resnet=models.resnet50(pretrained=True)
resnet.fc=torch.nn.线性(2048,5)
resnet.load\u state\u dict(火炬加载(模型路径))
#在加载模型之后,在上面添加此代码
resnet.fc=nn.Sequential(
resnet.fc,
nn.Softmax(1),
)
resnet.eval()
Traceback (most recent call last):
  File "/.../PycharmProjects/thesis/SHAP.py", line 133, in <module>
    main()
  File ".../PycharmProjects/thesis/SHAP.py", line 123, in main
    shap_values = e.shap_values(test_images)
  File ".../anaconda3/lib/python3.8/site-packages/shap/explainers/_deep/__init__.py", line 124, in shap_values
    return self.explainer.shap_values(X, ranked_outputs, output_rank_order, check_additivity=check_additivity)
  File ".../anaconda3/lib/python3.8/site-packages/shap/explainers/_deep/deep_pytorch.py", line 185, in shap_values
    sample_phis = self.gradient(feature_ind, joint_x)
  File ".../anaconda3/lib/python3.8/site-packages/shap/explainers/_deep/deep_pytorch.py", line 121, in gradient
    grad = torch.autograd.grad(selected, x,
  File ".../anaconda3/lib/python3.8/site-packages/torch/autograd/__init__.py", line 202, in grad
    return Variable._execution_engine.run_backward(
  File ".../anaconda3/lib/python3.8/site-packages/shap/explainers/_deep/deep_pytorch.py", line 226, in deeplift_grad
    return op_handler[module_type](module, grad_input, grad_output)
  File ".../anaconda3/lib/python3.8/site-packages/shap/explainers/_deep/deep_pytorch.py", line 358, in nonlinear_1d
    grad_output[0] * (delta_out / delta_in).repeat(dup0))
RuntimeError: The size of tensor a (512) must match the size of tensor b (2048) at non-singleton dimension 1