I’m training a GAN, but getting the following error. Can someone explain me why this is happening, and how to resolve it?
############################
# (1) Update D network: maximize D(x)-1-D(G(z))
###########################
real_img = torch.Tensor(target)
if torch.cuda.is_available():
real_img = real_img.cuda()
z = torch.Tensor(data)
if torch.cuda.is_available():
z = z.cuda()
fake_img = netG(z)
netD.zero_grad()
real_out_1 = netD(real_img)
real_out = torch.mean(real_out_1)
fake_out_1 = netD(fake_img)
fake_out = torch.mean(fake_out_1)
d_loss = -torch.log(real_out_1) - torch.log(1 - fake_out_1)
d_loss = torch.mean(d_loss)
d_loss.backward(retain_graph=True)
optimizerD.step()
############################
# (2) Update G network: minimize 1-D(G(z)) + Perception Loss + Image Loss
###########################
netG.zero_grad()
g_loss = generator_criterion(fake_out, fake_img, real_img)
g_loss.backward()
fake_img = netG(z)
fake_out = netD(fake_img).mean()
optimizerG.step()
RuntimeError Traceback (most recent call last)
in ()
119 netG.zero_grad()
120 g_loss = generator_criterion(fake_out, fake_img, real_img)
–> 121 g_loss.backward()
122
123 fake_img = netG(z)
1 frames
/usr/local/lib/python3.6/dist-packages/torch/tensor.py in backward(self, gradient, retain_graph, create_graph)
219 retain_graph=retain_graph,
220 create_graph=create_graph)
–> 221 torch.autograd.backward(self, gradient, retain_graph, create_graph)
222
223 def register_hook(self, hook):
/usr/local/lib/python3.6/dist-packages/torch/autograd/init.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)
130 Variable.execution_engine.run_backward(
131 tensors, grad_tensors, retain_graph, create_graph,
–> 132 allow_unreachable=True) # allow_unreachable flag
133
134
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [1, 1024, 1, 1]] is at version 2; expected version 1 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!