解决RuntimeError: one of the variables needed for gradient computation has been modified by an inplace

错误:RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [6,128,60,80]], which is output 0 of SoftmaxBackward, 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).
变量的计算中包含了inplace操作,在SoftmaxBackward出。

报错位置:损失反向传递时,猜测可能在计算梯度时发生错误。错误原因可能是inplace的原因。

方法1:将inplce=True的地方全部改为False,主要是在ReLU处,经过修改发现对我没有用。

方法2:将所有的a+=b的地方改为a=a+b,对我没有用。

方法3:将代码中nn.Sigmoid函数删除掉,用其他的归一化方法。解决问题。

方法4:参考别的大佬文章,都是sigmoid函数处出错,大佬链接因为我的代码已经运行起来了,就不想暂停,下次再跑试验一下这个方法。

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
乘风的头像乘风管理团队
上一篇 2023年10月19日
下一篇 2023年10月19日

相关推荐