Interactive Explainer
Feeling GAN training through the forger-and-detective game
Forget the loss function for a moment. Start with a counterfeiter and a detective, give them paint and magnifying glasses, and watch a generative model emerge as a pencil-and-paper game—no black box, no hand-waving.
The counterfeiter and the detective
A counterfeiter prints fake banknotes. A detective inspects every bill that passes through a bank. At first, the counterfeiter is bad — the fakes look like kindergarten art, and the detective catches them instantly. The detective looks smart. The counterfeiter looks ridiculous.
But the counterfeiter learns. Every time a fake is caught, she asks the detective: "What tipped you off?" She fixes that flaw. A few hundred iterations later the fakes pass muster on the old tests — the detective must find new ones. A few thousand iterations later the detective's "tests" are splitting hairs of the paper fiber.
At the limit, the counterfeiter's bills are indistinguishable from real ones. The detective is left flipping coins. A GAN is this game, with two neural networks as the players and probability distributions as the bills.
Pick a target distribution
Before any training, fix what the real data looks like. In a real GAN this is a dataset of images; here it's a 1D probability density we can plot. Three shapes come up in GAN debugging discussions:
Two symmetric Gaussians — think "heights of adult men and women mixed together."
What does a "bad" discriminator look like?
The detective — the discriminator D(x) — takes a point x and outputs a probability: "how confident am I this came from real data, not the generator?". A perfect D outputs 1 for real, 0 for fake.
Drag the slider below to build a manual linear discriminator: a simple threshold and slope. Watch how its accuracy (fraction correctly classified) changes when you move it around. The generator's distribution here is fixed at N(0, 1.5²) — the dumb initialization before training.
What does the optimal discriminator look like?
Turns out there's a closed-form answer. If the generator's distribution is fixed, the D that minimises the loss is exactly:
In plain English: for every point x, the optimal detective answers "probability this is real" by computing the ratio of real-density to total-density. You can build this by hand without any training — if you know pdata and pG.
p/(p+p) = 0.5 everywhere. The optimal discriminator is a horizontal line at 0.5 — it gives up. That flat line is the target of GAN training.
What does the generator do?
Given a discriminator, the generator's job is to fool it. If D thinks real points have high score and fakes have low score, G wants to push its output toward regions where D is high.
That's the gradient signal. At a sample x = G(z), the generator receives:
which points in the direction that raises D at the generated point. Interpretation: "take this sample and move it toward a place where the detective would have been more fooled."
Click anywhere on the x-axis to place a sample
pdata is low and D is low) get big arrows pointing toward the nearest real mode. That's the physical meaning of "move toward more plausible space."
1/D becomes unstable. That's the vanishing-gradient problem Goodfellow's non-saturating loss (maximize log D instead of minimize log(1-D)) was designed to fix.
The full dance
Now put both sides together. At every training step, we alternate:
- Update D: nudge it toward D∗ for the current G (via gradient descent on BCE loss).
- Update G: nudge it in the direction that raises D at its samples.
Scrub the training step slider below and watch the three curves evolve together. At step 0, G is noise; D quickly becomes peaky. As G improves, D's decision curve flattens until it's 0.5 everywhere.
Mode collapse · when the dance breaks down
In the ideal trajectory above, G learns the full distribution. In practice, G often takes a shortcut: it discovers that one particular output fools D well, and produces only that output for every input. Diversity is lost — mode collapse.
Three things a GAN is not
"GANs minimize a meaningful loss."
The minimax objective doesn't admit a single "loss goes down" interpretation. D's loss going up might mean G got better; G's loss going up might mean D caught on. Monitoring losses alone is a terrible way to judge GAN progress — use sample quality + diversity metrics (FID, precision/recall).
"The generator learns pdata."
Only approximately, and only at the idealised Nash equilibrium. In practice, G often learns a good-looking subset of pdata. This is why WGAN, Wasserstein distance, and regularisers matter — they push the model closer to the full distribution.
"You should train D to convergence before updating G."
The original paper suggested this; in practice it kills G's gradient (the stronger D gets, the more it saturates, the smaller G's gradient). Most GAN recipes alternate one step of each, or use the non-saturating G loss to stay robust.
From minimax to Wasserstein · why training got easier
The original GAN objective measures a Jensen-Shannon divergence between pdata and pG. The problem: JS is saturating — when the distributions barely overlap (early in training), JS ≈ log 2 no matter how close or far they are. So the generator gets no useful gradient signal.
Arjovsky et al. 2017 (WGAN) replaced JS with the Wasserstein-1 (earth-mover) distance:
Think of it as "the minimum cost to physically move the mass of pG onto pdata." This distance shrinks continuously as the distributions get closer — no saturation. Training is dramatically more stable.
Comparing the three objectives
| Objective | Gradient when distributions far apart | Training stability |
|---|---|---|
| Minimax (original, saturating) | Vanishing | Poor |
| Non-saturating (log D) | Healthy when G is bad | Standard |
| Wasserstein (WGAN / WGAN-GP) | Healthy always (linear in distance) | Best |
Part of the ES 667 Deep Learning course · IIT Gandhinagar · Aug 2026.