python - 在pytorch中获取gradCam的渐变
问题描述
我在我的 pytorch densenet121 模型中使用前向和后向钩子。我requires_grad
在训练时设置为 False。
for param in model.features.parameters():
param.requires_grad = False
我想获得神经网络中最后一个卷积层的梯度,为此我定义了这样的钩子:
model.features.denseblock4.denselayer16.conv2.register_backward_hook(h1)
但这给了我空列表。但是我是否设置requires_grad = True
了最后一层然后我得到了渐变。
for param in model.features.denseblock4.denselayer16.conv2.parameters():
param.requires_grad = True
我对gradCAM使用渐变。
是这种写方法还是有其他方法来获得梯度还是我需要requires_grad = True
为整个网络设置?
解决方案
推荐阅读
- c# - 将 ViewModel 中的 Id 转换为 ControlTemplate
- ios - 如何在 SwiftUI 中处理点击开始
- android - 如何使用Android中的导航控制器制作自定义后退按钮以返回某个目的地?
- android - 在 android API 29 android 中弃用了 telemanager.endCall()
- html - 如何制作上下垂直线
- android - FirebaseTranslatorOptions.Builder 对象未从下拉菜单(微调器)Firebase 翻译 Android 中的文本
- python - 有没有办法将下拉列表放在标签框内?
- javascript - 画布开始滞后时间
- android - 如何使用 MediaRecorder 和 OpenGL ES 记录过滤后的视频帧
- variables - STM32F0。如何将 RAM 中变量的值分配给单独的变量?