Wasserstein GANs and Lipschitz constraints
Why JS goes silent when supports do not overlap, how the earth mover's distance brings the gradient back, and how to keep the critic 1-Lipschitz.
When JS stops helping
The topic GANs as a two-player game ended with a warning. With an optimal discriminator the generator minimizes , and the topic Entropy, cross-entropy and KL showed that is stuck at its maximum, , whenever and do not overlap.
As the GAN topic showed, this is the typical case, not an exotic one: real images and generated samples both concentrate near thin, low-dimensional surfaces in , and their supports almost never overlap.
The classic picture is the parallel-lines example (Arjovsky, Chintala and Bottou, 2017, Example 1). Take . The data are a vertical segment in the plane, and the model is the same segment shifted sideways by :
For every the two segments are disjoint. , because puts mass where has none. only asks whether the distributions overlap, not how far apart they are. A loss that does not change as the generator moves gives it no gradient — this is the saturating discriminator, seen from the generator's side.
The earth mover's distance
We need a distance that notices how far the model is from the data. Think of and as two piles of earth with the same total mass, and ask for the cheapest way to reshape one into the other.
A transport plan says how much mass moves from to . All of must be shipped and all of must be filled, so the marginals of are and ; is the set of all such plans. Moving mass over a distance costs . The earth mover's distance, or Wasserstein-1 distance, is the cost of the best plan:
For the parallel lines the best plan moves every point straight across to . Every unit of mass travels , so . Unlike JS, it shrinks steadily as the model approaches the data, and its slope always points towards . The same argument shows that shifting any distribution by a vector costs exactly .
In one dimension the best plan is easy to find: paths never need to cross. Match the quantiles — the lowest of goes to the lowest of , and so on. Equivalently, is the area between the two CDFs, .
Duality and the critic
A network cannot search over all transport plans in . The Kantorovich–Rubinstein duality (Villani, 2009, Remark 6.5) turns the minimum over plans into a maximum over functions, which a network can represent:
The condition says that is 1-Lipschitz:
More generally, the smallest with is the Lipschitz constant of : the most can change per unit of distance. For example, has constant , and has constant , its slope at .
The function is the critic: a network with a real-valued output and no sigmoid. It wants high scores on real data and low scores on generated data, but it may not change faster than distance. Without that limit, any that separates from could be multiplied by , then by , and the supremum would be for every .
With the limit, no critic can beat any plan, and this half of the duality takes two lines. Take any plan and any 1-Lipschitz :
The equality holds because the marginals of are and , the inequality because is 1-Lipschitz. Taking the supremum over and the infimum over gives . Read as the price of earth at : a price list whose prices never differ by more than the distance cannot charge more than the cheapest plan costs. That the best price list matches the cheapest plan exactly is the content of the theorem. For two point masses and with , the critic reaches the gap .
The WGAN losses
The Wasserstein GAN (Arjovsky, Chintala and Bottou, 2017) trains the critic to reach the supremum and the generator to make it small. The critic maximizes the dual objective, so its loss is minus that objective. The generator appears only in the second term and wants its samples to score high:
No logarithms and no sigmoid: the scores are simply averaged. The generator's gradient is the critic's slope at the generated points, and the Lipschitz constraint keeps that slope informative even when real and generated data are far apart.
The training rhythm flips compared with the GAN. A near-perfect GAN discriminator saturates and stops teaching the generator. A near-optimal critic, by contrast, gives the right gradient. If is the optimal critic for the current , then, under mild conditions on (Arjovsky, Chintala and Bottou, 2017, Theorem 3),
which is exactly with . The change of with does not enter: the derivative of a maximum is the partial derivative at the maximizer (the envelope argument). So the critic is updated several times per generator step, in the original algorithm.
A practical bonus: within one run, falls as the samples get better, which the GAN discriminator's loss does not do (Section 4.2 of the paper). It is a minibatch estimate of a lower bound on , where is the Lipschitz constant the critic is actually held to. depends on the critic's architecture, so the number is not comparable across models.
Weight clipping
The losses alone do not keep Lipschitz; something has to enforce it. The original WGAN used the bluntest tool available: after every critic update, clip each weight to the interval , with .
Bounded weights mean bounded slopes, so the clipped critic is -Lipschitz for some that depends on and on the architecture. need not be . The critic then estimates instead of , which still points the generator the right way: the constant only rescales the gradient.
The authors themselves called clipping "a clearly terrible way" to enforce the constraint. A large makes the critic slow to train to optimality; a small makes gradients vanish in a deep critic. The WGAN-GP paper (Gulrajani et al., 2017, Sections 3.1–3.2) added two findings. Most weights end up pressed against , which pushes the critic towards overly simple functions. And without careful tuning of , gradients through the critic either explode or vanish.
The gradient penalty
WGAN-GP (Gulrajani et al., 2017) constrains the critic through its gradient instead of its weights. The link is a fact from calculus: a differentiable is 1-Lipschitz exactly when everywhere. So the critic's loss gets a gradient penalty that pulls the gradient norm towards :
with in all of the paper's experiments. The penalty is two-sided: it also punishes norms below . That costs nothing, because the optimal critic has gradient norm exactly along the segments between points that the optimal plan pairs up (Gulrajani et al., 2017, Proposition 1). In the parallel lines with , the optimal critic has unit gradient everywhere between the segments.
Where comes from. Checking the gradient everywhere in is impossible, and Proposition 1 says where it matters most: between real and generated points. Each real sample is paired with a generated one , and the penalty is evaluated at a random point of the segment between them, an interpolate:
The pairs are random rather than taken from the optimal plan, so this is a heuristic, but it works well. In code is drawn once per image, the gradient comes from torch.autograd.grad(..., create_graph=True) so that the penalty itself can be backpropagated, and its norm is taken per sample. That last point rules out batch normalization in the critic: it mixes examples, so the per-image gradient no longer describes the function being constrained. Layer normalization is the recommended replacement.
Spectral normalization
Spectral normalization (Miyato et al., 2018) goes back to the weights, but constrains them precisely instead of clipping them. A linear map stretches the unit circle into an ellipse. Its singular values are the semi-axes, and the largest one, the spectral norm,
is the most any vector can be stretched — the Lipschitz constant of the layer. For the semi-axes are about and . Dividing every weight matrix by its spectral norm, , leaves no direction stretched by more than .
Lipschitz constants multiply under composition, and ReLU and LeakyReLU are 1-Lipschitz. For a critic that alternates linear layers with such activations (Miyato et al., 2018, Eq. 7),
which is at most after normalization. There is no to tune and no sampled points. In PyTorch a layer is wrapped with torch.nn.utils.parametrizations.spectral_norm, which estimates with one step of power iteration per training forward pass instead of a full SVD.
The guarantee is approximate. Power iteration estimates from below, since for unit , so dividing by the estimate can leave a layer stretching some direction slightly more than ; for a convolution the kernel reshaped into a matrix is only a proxy for its true norm; and normalization layers in between are not 1-Lipschitz in general. The product bound is also loose, so the critic ends up -Lipschitz with that can be well below but is not guaranteed to be at most — again an estimate of roughly a multiple of . Miyato et al. trained mostly with the standard and hinge GAN losses; the normalization itself is just a Lipschitz constraint and suits a WGAN critic equally well.
What GANs cannot do
Compare the two adversaries. The optimal GAN discriminator, , is built from a density ratio, so it inherits the blindness of JS when the supports do not overlap. The WGAN critic compares locations: the Lipschitz constraint ties it to the geometry of the data space, and that is what keeps its gradient alive.
What neither of them gives is a likelihood. is defined only through sampling: draw , compute . To evaluate at a given image we would need every that maps to it and how much squeezes or stretches volume around that . With there is not even a density to evaluate. GANs, Wasserstein or not, produce sharp samples but cannot score a test image, and cannot be trained or compared by log-likelihood.
Can a model keep the "noise in, sample out" generator and still have an exact likelihood? Yes, if the map is invertible and keeps the dimension: then every comes from exactly one , and the change of volume is a Jacobian determinant. That is the subject of the next topic, The change of variables: normalizing flows.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Go deeper — optional
Code and labs do not count toward the main percentage or the certificate.
- Numerical critic Lipschitz checkoptional
max |f'| for tanh(θx), weight clip, and GP penalty. Self-check in the notebook.
~15 min · easier on a computer · The WGAN topic read.
Self-check in the notebook — not part of mastery or the certificate.
Answers are saved on the server and tied to this browser until you sign in.