RuntimeError: one of the variables needed for gradient computation has been modified by an...

起因:pytorch训练,之前能train的程序,一点没改,现在报错,记录改bug过程。

报错代码段:

loss_qf = L1loss(QF, qf)
loss_img = L1loss(img_E, img_H_tensor)
loss_train = loss_img + 0.1 * loss_qf
print(loss_train)
optimizer.zero_grad()
loss_train.backward()
optimizer.step()
scheduler.step()

报错:

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [16, 512, 16, 16]], which is output 0 of AddBackward0, is at version 1; expected version 0 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).

搜索解决方法:

1、两个loss都进行backward()操作时,丢失参数。但是报错和我的有区别,原链接是TBackward且是两个loss,我只有一个loss,报错AddBackward0。方法不行。

https://discuss.pytorch.org/t/solved-pytorch1-5-runtimeerror-one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation/90256

2、将网络中的nn.ReLU(inplace=True)改成False;将所有inplace操作转换为非inplace操作,如将x += 1换为y = x + 1。也无效。

3、更改torch版本,原版本1.11.0升级到1.91报错相同,降级到1.10.0,报错变成ReluBackward0。尝试步骤2,删去nn.ReLU(inplace=True)中的inplace=True,程序跑通了!!!

https://github.com/pytorch/pytorch/issues/24853

总结,pytorch版本问题,有点玄学。如果是双loss回传,第一条能解决,报错ReluBackward0用第二条,没发现问题,建议更换pytorch版本。

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值