when training D, the input have been detached, it a data only not a Variable anymore.

  
      github.com
  
  
    pytorch/examples/blob/master/dcgan/main.py#L215 2



output = netD(real_cpu)
errD_real = criterion(output, label)
errD_real.backward()
D_x = output.mean().item()


# train with fake
noise = torch.randn(batch_size, nz, 1, 1, device=device)
fake = netG(noise)
label.fill_(fake_label)
output = netD(fake.detach())
errD_fake = criterion(output, label)
errD_fake.backward()
D_G_z1 = output.mean().item()
errD = errD_real + errD_fake
optimizerD.step()


############################
# (2) Update G network: maximize log(D(G(z)))
###########################
netG.zero_grad()