RuntimeError: Output 0 of BackwardHookFunctionBackward is a view and is being modified inplace. This

在使用Grad-CAM时遇到RuntimeError,该错误通常发生在尝试就地修改返回的视图上。解决方案是将可能导致就地修改的ReLU层的`inplace`参数设为`False`,例如将`nn.ReLU(inplace=True)`改为`nn.ReLU()`。这将避免自定义反向传播逻辑与PyTorch内部视图处理之间的冲突,确保正确计算梯度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

RuntimeError: Output 0 of BackwardHookFunctionBackward is a view and is being modified inplace. This view was created inside a custom Function (or because an input was returned as-is) and the autograd logic to handle view+inplace would override the custom backward associated with the custom Function, leading to incorrect gradients. This behavior is forbidden. You can fix this by cloning the output of the custom Function.

使用grad_cam 时报错了
参考如下:
https://discuss.pytorch.org/t/getting-this-warning-output-0-of-backwardhookfunctionbackward-is-a-view-and-is-being-modified-inplace/122766

激活函数的问题。
self.relu1 = nn.ReLU(inplace=True) 改为: self.relu1 = nn.ReLU()
即可

评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值