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()