A discriminator, usually written D, gives a generator feedback about its synthetic examples. In a basic generative adversarial network (GAN), D learns to distinguish real training data from generated data. The generator G then changes its outputs to make D judge them as real. D has the easier task, so training must keep the competition useful for G. section 9.1.2 section 9.9
The first implementation treats D as a binary classifier. For a real batch x and noise z, compute a real-data loss from D(x) with target 1 and a fake-data loss from D(G(z)) with target 0. Add the losses and update D. Then evaluate D(G(z)) against target 1 and update G: G is rewarded when its fakes look real to D. Repeat these two updates for each batch. section 9.1.2 section 9.1.3
A PyTorch discriminator update
With `loss_func = nn.BCEWithLogitsLoss()`, the core update is `D.zero_grad(); errD_real = loss_func(D(real_data), y_real); errD_real.backward(); fake = G(z); errD_fake = loss_func(D(fake.detach()), y_fake); errD_fake.backward(); optimizerD.step()`. Here `y_real` is 1 and `y_fake` is 0. Detaching `fake` keeps this discriminator update separate from the subsequent generator update, which uses `loss_func(D(fake), y_real)`. section 9.1.3
This classifier-style setup can leave G with poor feedback: GAN training may suffer vanishing gradients and mode collapse, where G produces too narrow a range of outputs. WGAN-GP changes D's job. Instead of classifying with bounded real/fake probabilities, D assigns unrestricted scores. Its loss minimizes the average fake score minus the average real score, plus a gradient penalty. G minimizes the negative score of its own generated data. section 9.3.1 section 9.3.2
The gradient penalty evaluates D on random mixtures of real and fake inputs and penalizes the norm of its input gradient when it differs from 1. Its weight controls how strongly D is constrained. This reduces opportunities for vanishing gradients and helps G keep up; it mitigates mode collapse rather than solving it. section 9.3.1 section 9.3.3
eps_shape = [batch_size]+[1]*(len(data.shape)-1) #1 eps = torch.rand(eps_shape, device=device) mixed = eps*real + (1-eps)*fake output = D(mixed) grad = torch.autograd.grad(outputs=output, inputs=mixed, grad_outputs=torch.ones_like(output), create_graph=True, retain_graph=True, only_inputs=True, allow_unused=True)[0] D_grad_penalty = ((grad.norm(2, dim=1) - 1) ** 2) errD = (D_failure-D_success).mean() + D_grad_penalty.mean()*10 #2 errD.backward() optimizerD.step() #3 #1 Now calculate tensors to use to compute the gradient penalty term. #2 Calculates D’s loss #3 Updates D
The WGAN-GP update constructs mixed inputs, obtains D's input gradients with `torch.autograd.grad`, and adds their penalty to the score difference before updating D. section 9.3.3
For images, D can be convolutional without mirroring G. A smaller D gives G more room to improve; average pooling shares gradients across pixels, while adaptive pooling encourages attention to overall content rather than exact positions. Plot both networks' losses and inspect generated images when adjusting their relative capacity. section 9.4.2







