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 

源码:

netD.zero_grad()
# optimizerD.step()
real_out = netD(real_img).mean()
fake_out = netD(fake_img).mean()
d_loss = 1 - real_out + fake_out
d_loss.backward(retain_graph=True)
# d_loss.backward()
optimizerD.step()
netG.zero_grad()
g_loss = generator_criterion(fake_out, fake_img, real_img)
g_loss.backward()
optimizerG.step()
fake_img = netG(z)
fake_out = netD(fake_img).mean()
# this code is applied to the pytorch1.4 version and before.
# because the optimizer.step() actually modifies the weights of the model inplace while the original value of these weights is needed to compute the loss.backward() !!!!

原因:pytorch版本到达1.5以后的autograd变了。
一、第一种简单的方案就是把pytorch恢复到1.4之前的环境,Ubuntu命令

conda create -n torch python=3.7
conda activate torch
pip install torch==1.2.0 torchvision==0.4.0 
#如果网速太慢可以用清华源  后面加 -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
pip install torch==1.2.0 torchvision==0.4.0 -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
pip install numpy matplotlib scipy==1.2.0 pandas scikit-learn scikit-image

二、修改代码, 把更新梯度的步骤调后放在一起即可:

real_out = netD(real_img).mean()
fake_out = netD(fake_img).mean()
d_loss = 1 - real_out + fake_out
netD.zero_grad()
g_loss = generator_criterion(fake_out, fake_img, real_img)
netG.zero_grad()
d_loss.backward(retain_graph=True)
g_loss.backward()
optimizerD.step()
optimizerG.step()
fake_img = netG(z)
fake_out = netD(fake_img).mean()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值