Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Measuring how far, not just whether
A standard GAN’s discriminator answers a yes-or-no question: real or fake? If the fakes are nowhere near the real data, the answer is always a confident ‘fake’, and the generator learns nothing about which way to move. The Wasserstein GAN asks a different question: how much work would it take to move the pile of fake samples onto the pile of real ones? That amount, the earth mover’s distance, shrinks steadily as the fakes get closer, so the generator always has a direction to follow. The demo compares the two measures for two narrow bell curves as you slide one toward the other.
Wasserstein distances are used well beyond GANs: comparing simulated and measured sensor distributions, aligning point clouds, and checking whether a model trained on one region of Earth transfers to another. The WGAN critic is a learned, scalable estimate of that distance.
- 1. The earth mover's distance measures…
- 2. For non-overlapping distributions JS is…
- 3. The WGAN discriminator is called a…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Wasserstein-1. W1(P, Q) = inf over couplings γ of E over (x, y) ~ γ of ‖x − y‖: the cheapest transport plan (A12.4).
- Parallel lines (Arjovsky et al.). P0 uniform on the vertical segment at x = 0 (y in [0, 1]), P_θ on the segment at x = θ: W1 = |θ|, while JS = log 2, KL = ∞ and total variation = 1 for every θ ≠ 0. Only W1 is continuous in θ.
- Duality. W1(P, Q) = sup over 1-Lipschitz f of E_P f − E_Q f (Kantorovich–Rubinstein). For a K-Lipschitz family the supremum gives K · W1.
- Algorithm. Critic f_w maximises E f(x) − E f(G(z)); after each update, clip every weight to [−c, c] with c = 0.01. Generator minimises −E f(G(z)). n_critic = 5 critic steps per generator step, RMSProp with learning rate 5 × 10⁻⁵, batch 64; no sigmoid and no log.
- Theory. If G is continuous in θ, W1(P_r, P_θ) is continuous; if G is locally Lipschitz, it is differentiable almost everywhere, with gradient −E over z of ∇_θ f*(G_θ(z)). W1 metrises weak convergence; KL, JS and TV are strictly stronger.
- Claims. Training the critic to optimality is encouraged rather than harmful; the critic loss correlated with sample quality; no mode collapse was observed in their experiments.
- Weaknesses. Clipping fixes the Lipschitz constant only crudely: small c gives vanishing gradients, large c slows training, and clipped weights pile up at ±c. This motivated the gradient penalty (H4.6) and spectral normalisation (E5.7).
- 1. Kantorovich–Rubinstein duality maximises over…
- 2. WGAN keeps the critic Lipschitz by…
- 3. For the parallel lines, W1 equals…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.W1 in 1-D and the WGAN critic
Exercise 1.
- Exact W1 between equal-size 1-D samples (sort and average the gaps).
- The parallel-lines distances: W1, JS and KL as functions of θ.
- The critic’s dual estimate E f(real) − E f(fake) and weight clipping.
Practice.
- Prefer WGAN-GP or spectral normalisation to weight clipping in new work.
- Use the critic estimate as a training curve: it is far more informative than a GAN discriminator loss, but only up to an unknown scale when clipping is used.
- Critic steps: 5 per generator step is standard; train the critic more at the start (the paper used 100 critic steps for the first 25 generator iterations).
- Optimizer: RMSProp or Adam with β1 = 0 (WGAN-GP used Adam with β1 = 0, β2 = 0.9).
- No batch norm in the critic if you later add a gradient penalty; layer norm is a common replacement.
import torch
for p in critic.parameters(): p.data.clamp_(-0.01, 0.01)
for it in range(n_iters):
for _ in range(5): # n_critic
loss_c = critic(G(torch.randn(B, z_dim)).detach()).mean() - critic(next(real_batches)).mean()
opt_c.zero_grad(); loss_c.backward(); opt_c.step()
for p in critic.parameters(): p.data.clamp_(-0.01, 0.01)
loss_g = -critic(G(torch.randn(B, z_dim))).mean()
opt_g.zero_grad(); loss_g.backward(); opt_g.step()
# opt_c, opt_g = torch.optim.RMSprop(..., lr=5e-5)- 1. New work usually replaces clipping with…
- 2. The WGAN critic estimate is informative…
- 3. WGAN-GP's Adam settings are…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Why JS fails. If P_r and P_θ have disjoint supports, D can be 1 on one and 0 on the other, so JS = log 2 regardless of how far apart the supports are. Natural images and GAN samples lie near low-dimensional manifolds, which in high dimension almost never overlap (Arjovsky and Bottou 2017), so this is the typical case early in training.
Continuity of W1. For the coupling that pairs each z with itself, W1(P_θ, P_θ′) ≤ E over z of ‖G_θ(z) − G_θ′(z)‖. If G is continuous in θ this tends to 0 as θ′ → θ, and by the triangle inequality W1(P_r, P_θ) is continuous. If G is locally Lipschitz with integrable constants, the bound is Lipschitz in θ, and W1 is differentiable almost everywhere (Rademacher’s theorem).
Gradient through the critic. With f* the optimal critic, ∇_θ W1(P_r, P_θ) = −E over z of ∇_θ f*(G_θ(z)) (Theorem 3 of the paper). So the generator update only needs ∇_x f at its samples, which an approximately optimal critic provides.
Clipping, precisely. Clipping to [−c, c] bounds each weight matrix’s spectral norm by c·√(n_in·n_out), so the network is K-Lipschitz for some K depending on c and the architecture. The critic therefore estimates K·W1 for an unknown K. Gulrajani et al. (2017) show it also biases the critic toward very simple functions and pushes weights to ±c.
- 1. Disjoint supports are typical because data lie near…
- 2. W1 is continuous in θ when…
- 3. A clipped critic estimates…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.Does the WGAN critic’s loss actually track the distance to the data during training?
History.
- M. Arjovsky and L. Bottou, “Towards principled methods for training generative adversarial networks” (ICLR 2017).
- M. Arjovsky, S. Chintala and L. Bottou, “Wasserstein generative adversarial networks” (ICML 2017).
- C. Villani, Optimal Transport: Old and New (Springer 2009): the duality theory.
Exercise 2. Data: two Gaussians at −2 and +2 with std 0.3 (for example two operating regimes of a sensor).
- Models (trained side by side): G 1 → 32 → 32 → 1 and critic or discriminator 1 → 32 → 32 → 1, LeakyReLU(0.2). WGAN: weights and biases clipped to [−0.05, 0.05], linear output. GAN: sigmoid output, non-saturating loss.
- Training: 5 critic or discriminator steps per generator step, 400 generator steps, RMSProp (lr 0.002), batch 64 for both.
- Logging: every 20 generator steps, on 1000 fresh samples, the critic’s estimate E f(real) − E f(fake), the GAN value E log D + E log(1 − D), and the exact W1 of each generator (sorted samples).
| quantity | seed 0 | seed 1 | seed 2 |
|---|---|---|---|
| correlation of critic estimate with true W1 | 0.82 (0.81) | 0.86 (0.86) | 0.77 (0.76) |
| correlation of GAN value with true W1 | 0.13 | 0.0 | 0.44 |
| WGAN: true W1 at the first and last check | 1.13 → 0.279 (0.346) | 1.129 → 0.221 (0.221) | 0.96 → 0.29 (0.317) |
| GAN: true W1 at the first and last check | 0.912 → 0.212 | 1.286 → 0.522 | 0.983 → 0.224 |
| critic estimate at the first check | 0.0066 | 0.0086 | 0.0039 |
- A meaningful loss curve: the clipped critic’s estimate rose and fell with the true distance (r ≈ 0.8); the GAN’s discriminator value was nearly uninformative (r between 0.0 and 0.44).
- Only up to scale: with clipping at 0.05 the critic’s raw estimate was about 0.004–0.009 when the true W1 was about 1: it measures K·W1 with K near 0.01.
- No clear sample-quality winner: both generators reached W1 of about 0.2–0.35 except the GAN on seed 1 (0.52). On this small problem WGAN’s advantage is the diagnostic, not the final fit.
- Round-off sensitivity: numbers in brackets come from the same code run in the browser (Pyodide, no BLAS). The GAN columns matched exactly, but the clipped WGAN’s trajectory diverged: final W1 on seed 0 was 0.279 natively and 0.346 in the browser. The correlation (about 0.8) and the conclusions are stable; individual final values are not.
Wasserstein GANs in AI, robotics and aerospace
- Training diagnostics: a critic estimate that tracks distance makes it possible to compare runs and stop training without looking at every sample.
- Scientific simulation: Wasserstein-type GANs have been used to emulate particle-physics detectors and cosmological fields, where mode coverage matters more than sharpness.
- Distribution shift checks: learned or exact Wasserstein distances compare simulated and real sensor statistics before deploying a perception model trained in simulation.
References
- M. Arjovsky, S. Chintala and L. Bottou (2017). “Wasserstein generative adversarial networks.” ICML.
- M. Arjovsky and L. Bottou (2017). “Towards principled methods for training generative adversarial networks.” ICLR.
- I. Gulrajani, F. Ahmed, M. Arjovsky, V. Dumoulin and A. Courville (2017). “Improved training of Wasserstein GANs.” NeurIPS.
- C. Villani (2009). Optimal Transport: Old and New. Springer.
- G. Peyré and M. Cuturi (2019). “Computational optimal transport.” Foundations and Trends in Machine Learning.