Skip to content
BIASED

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 2 DJS(P ∥ Qθ)−2ln⁡22\,D_{\mathrm{JS}}(P \,\|\, Q_\theta) - 2\ln 2, and the topic Entropy, cross-entropy and KL showed that DJSD_{\mathrm{JS}} is stuck at its maximum, ln⁡2\ln 2, whenever PP and QθQ_\theta 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 RD\mathbb R^D, and their supports almost never overlap.

The classic picture is the parallel-lines example (Arjovsky, Chintala and Bottou, 2017, Example 1). Take Z∼U(0,1)Z \sim U(0, 1). The data are a vertical segment in the plane, and the model is the same segment shifted sideways by θ\theta:

P: x=(0, Z),Qθ: x=(θ, Z).P:\ x = (0,\ Z), \qquad Q_\theta:\ x = (\theta,\ Z).

For every θ≠0\theta \ne 0 the two segments are disjoint. DKL(P ∥ Qθ)=∞D_{\mathrm{KL}}(P \,\|\, Q_\theta) = \infty, because PP puts mass where QθQ_\theta has none. DJSD_{\mathrm{JS}} 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 PP and QQ 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 γ(x,y)\gamma(x, y) says how much mass moves from xx to yy. All of PP must be shipped and all of QQ must be filled, so the marginals of γ\gamma are PP and QQ; Π(P,Q)\Pi(P, Q) is the set of all such plans. Moving mass mm over a distance ∥x−y∥\|x - y\| costs m ∥x−y∥m\,\|x - y\|. The earth mover's distance, or Wasserstein-1 distance, is the cost of the best plan:

W1(P,Q)=inf⁡γ∈Π(P,Q)E(x,y)∼γ ∥x−y∥.W_1(P, Q) = \inf_{\gamma \in \Pi(P, Q)} \mathbb E_{(x, y) \sim \gamma}\,\|x - y\| .

For the parallel lines the best plan moves every point (0,Z)(0, Z) straight across to (θ,Z)(\theta, Z). Every unit of mass travels ∣θ∣|\theta|, so W1(P,Qθ)=∣θ∣W_1(P, Q_\theta) = |\theta|. Unlike JS, it shrinks steadily as the model approaches the data, and its slope ±1\pm 1 always points towards θ=0\theta = 0. The same argument shows that shifting any distribution by a vector aa costs exactly ∥a∥\|a\|.

In one dimension the best plan is easy to find: paths never need to cross. Match the quantiles — the lowest 10%10\% of PP goes to the lowest 10%10\% of QQ, and so on. Equivalently, W1W_1 is the area between the two CDFs, ∫∣FP(t)−FQ(t)∣ dt\int \lvert F_P(t) - F_Q(t) \rvert\, dt.

Duality and the critic

A network cannot search over all transport plans in RD\mathbb R^D. The Kantorovich–Rubinstein duality (Villani, 2009, Remark 6.5) turns the minimum over plans into a maximum over functions, which a network can represent:

W1(P,Q)=sup⁡∥f∥L≤1[ Ex∼P f(x)−Ex∼Q f(x)].W_1(P, Q) = \sup_{\|f\|_L \le 1} \Big[\, \mathbb E_{x \sim P}\, f(x) - \mathbb E_{x \sim Q}\, f(x) \Big].

The condition ∥f∥L≤1\|f\|_L \le 1 says that ff is 1-Lipschitz:

∣f(x)−f(y)∣≤∥x−y∥for all x,y.|f(x) - f(y)| \le \|x - y\| \quad \text{for all } x, y.

More generally, the smallest KK with ∣f(x)−f(y)∣≤K ∥x−y∥|f(x) - f(y)| \le K\,\|x - y\| is the Lipschitz constant of ff: the most ff can change per unit of distance. For example, 5x5x has constant 55, and tanh⁡x\tanh x has constant 11, its slope at 00.

The function ff 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 ff that separates PP from QQ could be multiplied by 1010, then by 10001000, and the supremum would be +∞+\infty for every P≠QP \ne Q.

With the limit, no critic can beat any plan, and this half of the duality takes two lines. Take any plan γ∈Π(P,Q)\gamma \in \Pi(P, Q) and any 1-Lipschitz ff:

Ex∼P f(x)−Ex∼Q f(x)=E(x,y)∼γ[f(x)−f(y)]≤E(x,y)∼γ ∥x−y∥.\begin{aligned} \mathbb E_{x \sim P}\, f(x) - \mathbb E_{x \sim Q}\, f(x) &= \mathbb E_{(x, y) \sim \gamma}\big[f(x) - f(y)\big] \\ &\le \mathbb E_{(x, y) \sim \gamma}\,\|x - y\| . \end{aligned}

The equality holds because the marginals of γ\gamma are PP and QQ, the inequality because ff is 1-Lipschitz. Taking the supremum over ff and the infimum over γ\gamma gives sup⁡≤W1\sup \le W_1. Read f(x)f(x) as the price of earth at xx: 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 δ0\delta_0 and δθ\delta_\theta with θ>0\theta > 0, the critic f(x)=−xf(x) = -x reaches the gap 0−(−θ)=θ=W10 - (-\theta) = \theta = W_1.

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:

Lf=−Ex∼P[f(x)]+Ez∼p(z)[f(Gθ(z))],LG=−Ez∼p(z)[f(Gθ(z))].\mathcal L_f = -\mathbb E_{x \sim P}\big[f(x)\big] + \mathbb E_{z \sim p(z)}\big[f(G_\theta(z))\big], \qquad \mathcal L_G = -\mathbb E_{z \sim p(z)}\big[f(G_\theta(z))\big].

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 f∗f^* is the optimal critic for the current θ\theta, then, under mild conditions on GθG_\theta (Arjovsky, Chintala and Bottou, 2017, Theorem 3),

∇θW1(P,Qθ)=−Ez∼p(z)[∇θf∗(Gθ(z))],\nabla_\theta W_1(P, Q_\theta) = -\mathbb E_{z \sim p(z)}\big[\nabla_\theta f^*(G_\theta(z))\big],

which is exactly ∇θLG\nabla_\theta \mathcal L_G with f=f∗f = f^*. The change of f∗f^* with θ\theta 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, ncritic=5n_{\text{critic}} = 5 in the original algorithm.

A practical bonus: within one run, −Lf-\mathcal L_f 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 K W1K\,W_1, where KK is the Lipschitz constant the critic is actually held to. KK depends on the critic's architecture, so the number is not comparable across models.

Weight clipping

The losses alone do not keep ff Lipschitz; something has to enforce it. The original WGAN used the bluntest tool available: after every critic update, clip each weight to the interval [−c,c][-c, c], with c=0.01c = 0.01.

Bounded weights mean bounded slopes, so the clipped critic is KK-Lipschitz for some KK that depends on cc and on the architecture. KK need not be 11. The critic then estimates K W1K\,W_1 instead of W1W_1, 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 cc makes the critic slow to train to optimality; a small cc 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 ±c\pm c, which pushes the critic towards overly simple functions. And without careful tuning of cc, 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 ff is 1-Lipschitz exactly when ∥∇f(x)∥2≤1\|\nabla f(x)\|_2 \le 1 everywhere. So the critic's loss gets a gradient penalty that pulls the gradient norm towards 11:

Lf=Ez[f(Gθ(z))]−Ex∼P[f(x)]+λ Ex^[(∥∇x^f(x^)∥2−1)2],\mathcal L_f = \mathbb E_{z}\big[f(G_\theta(z))\big] - \mathbb E_{x \sim P}\big[f(x)\big] + \lambda\, \mathbb E_{\hat x}\Big[\big(\|\nabla_{\hat x} f(\hat x)\|_2 - 1\big)^2\Big],

with λ=10\lambda = 10 in all of the paper's experiments. The penalty is two-sided: it also punishes norms below 11. That costs nothing, because the optimal critic has gradient norm exactly 11 along the segments between points that the optimal plan pairs up (Gulrajani et al., 2017, Proposition 1). In the parallel lines with θ>0\theta > 0, the optimal critic f(x)=−x1f(x) = -x_1 has unit gradient everywhere between the segments.

Where x^\hat x comes from. Checking the gradient everywhere in RD\mathbb R^D is impossible, and Proposition 1 says where it matters most: between real and generated points. Each real sample xrx_r is paired with a generated one xf=Gθ(z)x_f = G_\theta(z), and the penalty is evaluated at a random point of the segment between them, an interpolate:

x^=α xr+(1−α) xf,α∼U(0,1).\hat x = \alpha\, x_r + (1 - \alpha)\, x_f, \qquad \alpha \sim U(0, 1).

The pairs are random rather than taken from the optimal plan, so this is a heuristic, but it works well. In code α\alpha 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 h↦Whh \mapsto W h stretches the unit circle into an ellipse. Its singular values are the semi-axes, and the largest one, the spectral norm,

σmax⁡(W)=max⁡h≠0∥Wh∥2∥h∥2,\sigma_{\max}(W) = \max_{h \ne 0} \frac{\|W h\|_2}{\|h\|_2},

is the most any vector can be stretched — the Lipschitz constant of the layer. For W=(1.30.70.30.9)W = \begin{pmatrix} 1.3 & 0.7 \\ 0.3 & 0.9 \end{pmatrix} the semi-axes are about 1.661.66 and 0.580.58. Dividing every weight matrix by its spectral norm, W^=W/σmax⁡(W)\hat W = W / \sigma_{\max}(W), leaves no direction stretched by more than 11.

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),

∥f∥L≤∏lσmax⁡(Wl),\|f\|_L \le \prod_l \sigma_{\max}(W_l),

which is at most 11 after normalization. There is no λ\lambda to tune and no sampled points. In PyTorch a layer is wrapped with torch.nn.utils.parametrizations.spectral_norm, which estimates σmax⁡\sigma_{\max} with one step of power iteration per training forward pass instead of a full SVD.

The guarantee is approximate. Power iteration estimates σmax⁡\sigma_{\max} from below, since ∣u⊤Wv∣≤σmax⁡|u^\top W v| \le \sigma_{\max} for unit u,vu, v, so dividing by the estimate can leave a layer stretching some direction slightly more than 11; 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 KK-Lipschitz with KK that can be well below 11 but is not guaranteed to be at most 11 — again an estimate of roughly a multiple of W1W_1. 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, D∗(x)=pdata(x)/(pdata(x)+qθ(x))D^*(x) = p_{\text{data}}(x) / \big(p_{\text{data}}(x) + q_\theta(x)\big), 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. QθQ_\theta is defined only through sampling: draw zz, compute Gθ(z)G_\theta(z). To evaluate QθQ_\theta at a given image we would need every zz that maps to it and how much GθG_\theta squeezes or stretches volume around that zz. With d<Dd < D 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 xx comes from exactly one zz, 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.

Full notes

Answers are saved on the server and tied to this browser until you sign in.