AlgoPlusAlgoPlus
Learn/Machine Learning
Lesson

Generative Adversarial Network

A generator and a discriminator duel — a forger versus a detective — until the fakes are indistinguishable.

8 min read Watch it move Build it

A GAN trains two neural networks against each other, like a forger and a detective locked in a duel. The generator turns random noise into fake samples; the discriminator tries to tell those fakes from real training data. Each one's improvement forces the other to improve.

The two players

  1. 1Generator G: maps a random noise vector z to a fake sample G(z) — an image, say. Its goal is to fool the discriminator.
  2. 2Discriminator D: takes a sample and outputs the probability it's real. Its goal is to catch fakes.
  3. 3They train in alternation: improve D on real-vs-fake, then improve G to fool the freshly improved D.
A minimax game
The two optimize opposite objectives — the generator minimizes exactly what the discriminator maximizes. The balance point (a Nash equilibrium) is reached when the generated distribution matches the real one, p_g = p_data, and the discriminator is reduced to guessing at 50/50. At that point the generator has learned to reproduce the data.
min_G  max_D   E[ log D(x) ]        # D wants real x scored high
               + E[ log(1 - D(G(z))) ] # D wants fakes scored low; G wants the opposite
The non-saturating trick
Early on, D rejects fakes so confidently that G's gradient vanishes. In practice G is trained to maximize `log D(G(z))` instead of minimize log(1 - D(G(z))) — same direction, stronger gradient when G is losing.

Why GANs are hard to train

Mode collapse and instability
The duel can fail. In mode collapse the generator finds a few outputs that reliably fool D and produces only those, ignoring the diversity of real data. If one network overpowers the other, gradients dry up and learning stalls. Balancing the two is the central practical challenge.
OperationTimeSpace
Training · two networks, opposite goalsalternating gradient steps
Sampling · fast — one forward pass, vs diffusion's many1 generator passO(sample)
Check yourself
At the ideal equilibrium of a GAN, what does the discriminator output?