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

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [256, 8]], which is output 0 of TBackward, is at version 2; expected version 1 instead.

从github上down下来的一个做无监督的GAN网络代码, 把所有参数改完后,开始run。第一个epoch跑完,在loss_G2.backward()这一句报错,这个bug卡了三天,记录一下让自己张张记性:

一、pytorch版本更新,不要使用inplace操作:

网上提到最多的解决方法:
1.将模型中所有的inplace=True改成inplace=False
2.将model += [nn.Conv2d(tch, 1, 1, 1, 0)]中的+=操作全部改为model = model + [nn.Conv2d(tch, 1, 1, 1, 0)](注意:不要写为model = [nn.Conv2d(tch, 1, 1, 1, 0)] + model,不然整个模型的顺序就反了)
我按照上面两条改了,仍然报错

二、降pytorch版本:

我自己是通过降pytorch的版本解决了这个bug,如果上面的方法行不通,真的建议尝试一下。我在服务器上跑,觉得创建一个新的环境,除了安装旧版本的pytorch,还要重新安装一大堆库,一直就没去弄。今天尝试安了一个老版本的pytorch,结果就通了:
我报错的环境:
pytorch=1.5.1
cuda=10.1
作者使用的环境:
pytorch=0.4.0
cuda=9.0
跑通的环境:
pytorch=1.4.0
cuda=9.0

######路漫漫其修远兮######

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值