GANs: a generator against a discriminator
A generative adversarial network (GAN) trains two networks against each other: a generator that turns random noise into fake samples, and a discriminator that tries to tell fake from real. Each improves by beating the other.
Why it matters
A VAE learns to generate by rebuilding its inputs, and its samples come out blurry because its loss rewards safe averages. A GAN has no reconstruction loss at all. The only question it asks is "could this pass for real?", which pushes samples to be sharp.
GANs produced the first photo-realistic generated faces. Diffusion has replaced them for most image generation, but the adversarial idea is still used inside image, audio and video models.
The idea
A forger and a detective. The generator makes fakes from random noise; the discriminator tries to tell them from real data. Each one's mistakes are the other one's training signal. When the detective can only guess, the fakes are as good as real.
A forger printing banknotes and a bank clerk checking them. Every note the clerk catches teaches the forger what gave it away; every fake that slips through teaches the clerk to look closer.
A real forger wants the clerk to stay bad at the job. In a GAN the generator needs a good discriminator: a clerk who cannot tell the difference has nothing to teach.
How it works
- The generator G takes random noise z and outputs a fake sample G(z). At the start its outputs are rubbish: here, numbers near 0 when the real data sits around 4.
- The discriminator D takes a sample and outputs the probability that it is real. Train it on a batch of real samples labelled 1 and a batch of fakes labelled 0, with binary cross-entropy.
- Now train G with D held still: generate fakes, pass them through D, and change G so that D says "real". The gradient flows back through D into G, telling G which way to move its samples.
- Alternate: one step for D, one step for G, again and again. A better D gives G a sharper signal, and a better G gives D a harder job.
- The game is in balance when the fakes are distributed exactly like the real data. Then D can do no better than guess, and outputs 0.5 everywhere.
- In the lesson's run the fakes first overshoot to a mean of 6.25, then settle. After 3,000 rounds they have mean 4.04 and spread 0.38, against 4 and 0.5 for the real data. D gives fakes 0.50 on average.
- To generate afterwards, throw D away: sample z and compute G(z).
Key points
- Two networks, two losses: D learns to classify real against fake, and G learns to make D wrong.
- The objective is a game: D maximises E[log D(x)] + E[log(1 − D(G(z)))], and G minimises it.
- In practice G maximises log D(G(z)) instead, the non-saturating loss: it gives strong gradients early on, when D rejects every fake easily.
- At the ideal balance D outputs 0.5 everywhere, and the generator's distribution equals the data's.
- There is no likelihood and no reconstruction error, so the loss curves don't show progress. Look at the samples.
- Mode collapse: G finds a few outputs that fool D and produces only those, ignoring the rest of the data.
Common mistakes
- Reading a falling generator loss as progress. The two losses chase each other; judge by the samples.
- Letting the discriminator's training step change the generator. Detach the fakes for that step.
- Training D far ahead of G. A discriminator that wins easily gives the generator little useful signal.
Not true: A falling generator loss shows that a GAN's samples are getting better.
In fact: The two losses depend on each other, so either can fall or rise while the samples improve or get worse. Judge a GAN by looking at its samples.
Not true: A discriminator that wins easily: the strongest training signal for the generator.
In fact: When D wins easily, its feedback hardly changes whatever G does, so G learns little. The two networks have to stay roughly balanced.
Part of Unit 7: Generative models and reinforcement learning in the free ML Foundations course.