Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.A forger and a detective
A GAN is a contest between a forger and a detective. The forger (the generator) turns random noise into fake samples. The detective (the discriminator) looks at a mix of real and fake samples and guesses which is which. Each improves by beating the other, and if the contest goes well the forger ends up producing samples the detective cannot tell from real ones. The best possible detective has a simple formula: at each point it compares how much real data and how much fake data lands there. The demo shows this for 1-D bell curves; move the generator onto the data and the detective is reduced to guessing one half everywhere.
Adversarial training is how many synthetic-data generators for sensors work, for example realistic radar clutter or satellite image patches for testing detectors. Knowing what the discriminator is actually estimating (a density ratio) tells you what a well-trained GAN can and cannot guarantee.
- 1. The generator in a GAN…
- 2. The discriminator outputs…
- 3. If the generator matches the data perfectly, the best discriminator outputs…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Players. Generator G maps noise z ~ p(z) to x = G(z), defining an implicit density p_g. Discriminator D maps x to a probability D(x) that x is real.
- Value. V(D, G) = E over p_data of log D(x) + E over p(z) of log(1 − D(G(z))): the negative binary cross-entropy of D on a balanced real/fake mix. D maximises it, G minimises it.
- Optimal discriminator. For fixed G, maximise pointwise a·log y + b·log(1 − y) with a = p_data(x), b = p_g(x): the maximum is at y = a/(a + b), so D*(x) = p_data(x)/(p_data(x) + p_g(x)). Its logit is log p_data/p_g, a density ratio.
- Value at D*. V(D*, G) = −log 4 + 2·JSD(p_data ‖ p_g) ≥ −log 4, with equality only when p_g = p_data. With disjoint supports JSD = log 2 and V = 0.
- Algorithm 1 (Goodfellow et al.). Alternate k discriminator steps (k = 1 in their experiments) and one generator step on minibatches; they used SGD with momentum.
- Saturation. ∂ log(1 − D)/∂(logit) = −D. Early in training D rejects fakes confidently, D(G(z)) ≈ 0, and the generator gradient vanishes. The non-saturating loss −log D(G(z)) has gradient −(1 − D), which is large exactly then. Same fixed points, very different dynamics.
- Caveats. The proof assumes infinite capacity and optimisation in function space. Real GANs take simultaneous gradient steps on a non-convex game and can oscillate, diverge or collapse onto a few modes (see H4.14).
- 1. D*(x) equals…
- 2. At D*, the GAN value is…
- 3. The non-saturating generator loss is…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Checking the theory numerically
Exercise 1.
- The optimal discriminator D* = p/(p+q) on a grid.
- The value V(D, G) and the Jensen–Shannon divergence on a grid; verify V(D*, G) = 2·JSD − log 4.
- Generator gradients with respect to the logit for the minimax and non-saturating losses.
Practice.
- Use the non-saturating loss (or a hinge or Wasserstein loss); the minimax form is for proofs.
- Watch both losses: a discriminator loss stuck near 0 means D wins outright and the generator gets little signal; near log 4 ≈ 1.386 can mean balance or a useless D.
- Look at samples and use a sample-based metric (FID, precision/recall, H1.3), not loss values, to judge progress.
- Start from known-good recipes: DCGAN (H4.2) for small images, StyleGAN2-ADA or a diffusion model for serious image work.
- Remember the density ratio: a trained D estimates log p_data/p_g; this is the basis of density-ratio estimation and discriminator-based rejection sampling.
import torch, torch.nn.functional as F
# one training step of a GAN with the non-saturating generator loss
z = torch.randn(B, z_dim); fake = G(z)
d_loss = F.binary_cross_entropy_with_logits(D(real), torch.ones(B, 1)) + \
F.binary_cross_entropy_with_logits(D(fake.detach()), torch.zeros(B, 1))
opt_d.zero_grad(); d_loss.backward(); opt_d.step()
g_loss = F.binary_cross_entropy_with_logits(D(fake), torch.ones(B, 1)) # -log D(G(z))
opt_g.zero_grad(); g_loss.backward(); opt_g.step()- 1. A discriminator loss near 0 usually means…
- 2. Judge GAN progress by…
- 3. PyTorch's BCE-with-logits on fakes labelled 1 gives…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Theorem (optimal discriminator). For fixed G, V(D, G) = ∫ [p_data(x) log D(x) + p_g(x) log(1 − D(x))] dx (the second term by the law of the unconscious statistician, E over z of h(G(z)) = E over p_g of h(x)). For a, b ≥ 0 not both zero, y ↦ a log y + b log(1 − y) is concave on (0, 1) with derivative a/y − b/(1 − y), which vanishes at y = a/(a + b). Maximising pointwise gives D* = p_data/(p_data + p_g).
Theorem (global optimum). Substitute D*: C(G) = E over p_data of log [p_data/(p_data + p_g)] + E over p_g of log [p_g/(p_data + p_g)]. Add and subtract log 2 in each term with the mixture m = (p_data + p_g)/2: C(G) = −log 4 + KL(p_data ‖ m) + KL(p_g ‖ m) = −log 4 + 2·JSD(p_data ‖ p_g). JSD ≥ 0 with equality only if the distributions are equal, so the unique minimiser is p_g = p_data, where D* = 1/2.
Convergence claim. If G and D have enough capacity and D is optimised fully at each step, a small update of p_g along the gradient of C (a convex functional of p_g) converges to p_data. With parametrised networks this argument no longer holds: Nagarajan and Kolter (2017) and Mescheder et al. (2018) show that gradient descent on the game can converge locally only under conditions, and can cycle.
Saturation and the alternative loss. With D = σ(l), ∂ log(1 − σ(l))/∂l = −σ(l) → 0 when fakes are rejected. Using −log D(G(z)) keeps the same fixed point but, with an optimal D, Arjovsky and Bottou (2017) show its expected gradient is that of KL(p_g ‖ p_data) − 2·JSD: mode-seeking, which helps explain why GANs drop modes rather than blur.
Disjoint supports. When p_data and p_g live on low-dimensional manifolds that do not overlap, a perfect discriminator exists, JSD = log 2 is constant, and the generator receives no useful gradient: the motivation for Wasserstein GANs (H4.5).
- 1. The pointwise maximiser of a log y + b log(1 − y) is…
- 2. C(G) = −log 4 + 2·JSD follows by…
- 3. With an optimal D the non-saturating loss behaves like…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.Does a trained discriminator really reach D*, and does saturation matter in practice?
History.
- I. Goodfellow et al., “Generative adversarial nets” (NeurIPS 2014): the game, D*, the JSD identity and the non-saturating heuristic.
- M. Arjovsky and L. Bottou, “Towards principled methods for training generative adversarial networks” (ICLR 2017): vanishing gradients and disjoint supports.
- L. Mescheder, A. Geiger and S. Nowozin, “Which training methods for GANs do actually converge?” (ICML 2018).
Exercise 2. Everything is 1-D so it can be checked exactly.
- Part 1, D*. Data N(2, 0.5²), generator N(0.5, 1), 4000 samples each. The discriminator’s logit is w0 + w1x + w2x², which can represent log p/q exactly for two normals; fit it by Newton’s method on the GAN value.
- Part 2, saturation. Generator x = μ + σz starting at μ = −4, σ = 1. Same quadratic discriminator, one D step per G step, plain SGD (D step size 0.5, G step size 0.02), batch 128, 3000 steps. Compare the minimax and the non-saturating generator losses.
| quantity | seed 0 | seed 1 | seed 2 |
|---|---|---|---|
| max deviation of fitted D from D* on [−1, 4] | 0.038 | 0.105 | 0.029 |
| value of fitted D (theory −0.6839) | −0.7015 | −0.6788 | −0.7087 |
| non-saturating: final mean, std | 1.983, 0.469 | 1.929, 0.493 | 1.972, 0.481 |
| non-saturating: final JSD (nats) | 0.0012 | 0.0026 | 0.0008 |
| minimax: final mean, std | −0.635, 3.318 | −0.912, 3.306 | −0.863, 3.293 |
| minimax: final JSD (nats) | 0.4183 | 0.4298 | 0.4275 |
| early gradient ratio (non-saturating / minimax) | 1272.7 | 1047.6 | 1055.9 |
- D* is reachable when the discriminator family contains it: the fitted D matched p/(p+q) to within 0.03–0.11 (largest in the sparse tails) and its sample value was within 0.025 of 2·JSD − log 4.
- Saturation is real: in the first 100 steps the minimax generator gradient was about 1000× smaller. After 3000 steps the minimax generator had drifted halfway and spread out (std 3.3), while the non-saturating one matched the data.
- Caveat: an adaptive optimizer such as Adam rescales gradients and hides much of this difference; the effect is clearest with plain SGD, as in the original paper.
GANs in AI, robotics and aerospace
- Synthetic sensor data: GANs have been used to generate radar, sonar and satellite image patches for data augmentation; diffusion models now dominate where sample quality matters most.
- Density-ratio estimation: the discriminator’s logit estimates log p_data/p_g, which is used for two-sample tests, domain adaptation (DANN) and importance weighting.
- Adversarial losses as add-ons: super-resolution, image translation and codecs add a GAN loss to sharpen outputs (H4.8, H4.13, E2.14).
References
- I. Goodfellow, J. Pouget-Abadie, M. Mirza, B. Xu, D. Warde-Farley, S. Ozair, A. Courville and Y. Bengio (2014). “Generative adversarial nets.” NeurIPS.
- M. Arjovsky and L. Bottou (2017). “Towards principled methods for training generative adversarial networks.” ICLR.
- V. Nagarajan and J. Z. Kolter (2017). “Gradient descent GAN optimization is locally stable.” NeurIPS.
- L. Mescheder, A. Geiger and S. Nowozin (2018). “Which training methods for GANs do actually converge?” ICML.
- I. Goodfellow (2016). “NIPS 2016 tutorial: generative adversarial networks.” arXiv.