liveBook · Manning

Gradient penalty

As explained in Inside Deep Learning: Math, Algorithms, Models

A regularization term that constrains discriminator gradients during Wasserstein GAN training.

Gradient penalty regularizes the discriminator in Wasserstein GAN training. It reduces vanishing-gradient opportunities and can make it easier for the generator to keep pace, helping address mode collapse. section 9.3.3

Implement the penalty inside the discriminator update:

  • Sample ε values from [0, 1], shaped for broadcasting across each data example. section 9.3.3
  • Interpolate between real and fake data with mixed = ε·real + (1−ε)·fake. section 9.3.3
  • Do not detach fake before passing it to the discriminator, because the generator must remain connected to the penalty calculation. section 9.3.3

Training checks

The same training function is applied to a toy grid and 28×28 generated digits, followed by generated-sample plots and smoothed generator/discriminator loss curves.

How Inside Deep Learning: Math, Algorithms, Models covers it

How deep it goes
Derives the interpolation and autograd computation in PyTorch, then stops short of a deeper theoretical derivation.
Examples
Uses a toy grid, generated 28×28 digits, sample plots, and smoothed generator and discriminator loss curves.
What it assumes
Builds on a GAN training loop with generator, discriminator, optimizers, batches, and PyTorch gradient handling.
What you can do after
Apply it via PyTorch by interpolating samples, differentiating discriminator outputs, and adding the weighted penalty to discriminator loss.
How it connects
Connects Wasserstein GANs with discriminators, generators, autograd, and mode collapse.

Also explained in

Compare how 2 books explain gradient penalty →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Inside Deep Learning: Math, Algorithms, Models. The section links point to the parts of the book it draws on.