探索 · an exploration

Generative adversarial networks

Train two neural networks in your browser. One generates samples while the other learns to distinguish them from real data.

01

the game

How the networks train

Scroll and the diagram fills in step by step.

01 / 05The generator
NOISE zG · GENERATORNOISE → SAMPLEGENERATED G(z)TRAINING DATAREAL EXAMPLESREAL xD · DISCRIMINATORREAL OR GENERATED?REALFAKE½CLASSIFICATION LOSS∂ LOSS / ∂ GENERATORBACKPROPAGATION THROUGH DDISTRIBUTIONS MATCH · D = ½

01

The generator

A generator turns random numbers into a sample, such as a point or an image. It learns from the discriminator rather than receiving real examples directly.

02

The discriminator

A discriminator receives real and generated samples. For each one, it estimates the probability that the sample is real.

03

Training the discriminator

The discriminator trains as a binary classifier. Its loss penalizes labeling real samples as generated and generated samples as real.

04

Training the generator

The generator learns to increase the probability that the discriminator assigns to its samples being real. Backpropagation through the discriminator computes how to change the generator weights.

05

Equilibrium

At the theoretical optimum, the generated distribution matches the real distribution. The discriminator cannot distinguish them and assigns a probability of one half to either.

02

the lab

Train a GAN

Train a generator and discriminator on a one-dimensional distribution with one or two peaks. Each network has 24 hidden units.

-4-2024D = ½
Real, pdata Generated, pg Discriminator, D(x)
data
speed

0

steps

—

D on real samples

—

D on generated samples

loss · discriminator · generator

The ink curve shows the real distribution. The vermilion curve shows the generated distribution. The gold dashes show the discriminator output, which is high where it classifies samples as real. When the distributions match, that output approaches one half.

Switch to two bumps and reset a few times. The generator sometimes learns both peaks and sometimes concentrates on just one. Failing to represent parts of the real distribution is called mode collapse.

03

the objective

The training objective

The discriminator maximizes this objective while the generator minimizes it.

MING MAXD𝔼x∼pdata[log D(x)]+𝔼z∼pz[log(1 − D(G(z)))]

The discriminator, D

Maximizes this objective by making log D(x) large on real samples and log(1 − D(G(z))) large on generated samples.

The generator, G

Changes only the second term. In practice, the generator maximizes log D(G(z)) instead. This provides a stronger gradient when the discriminator easily rejects generated samples.

For a fixed generator, the best possible discriminator is

D*(x) = pdata(x)pdata(x) + pg(x)

The discriminator output is high where real samples are more likely than generated ones. It equals one half where the two densities match. Goodfellow et al. showed that the objective reaches its global optimum when the generated distribution equals the real one.

04

the trouble

Training problems

Mode collapse

The generator produces samples from only part of the real distribution. With two peaks, it may learn one and miss the other.

Vanishing gradients

If the discriminator becomes too accurate early in training, the generator gradient can approach zero. The non-saturating loss reduces this problem.

Unstable training

The networks optimize competing objectives, so their losses can oscillate rather than settle. Later work used the Wasserstein distance to improve training stability.

05

the lineage

What came after

Selected developments in image generation between 2014 and 2020.

  1. 2014

    GAN Goodfellow et al.

    Trained on MNIST and faces. The samples were small and blurry.

  2. 2015

    DCGAN Radford, Metz, Chintala

    Convolutional generators and discriminators, with architectural guidelines that improved training stability.

  3. 2016

    pix2pix Isola et al.

    Condition the generator on an input image to learn mappings such as edges to photographs or maps to satellite views.

  4. 2017

    WGAN Arjovsky, Chintala, Bottou

    Use a critic that estimates the Wasserstein distance between distributions to provide more useful gradients during training.

  5. 2017

    CycleGAN Zhu et al.

    Learn image translation without paired examples. Translating a horse to a zebra and back should reproduce the original image.

  6. 2017

    Progressive GAN Karras et al.

    Increase both networks from 4×4 to 1024×1024 resolution during training to generate high-resolution faces.

  7. 2018

    BigGAN Brock, Donahue, Simonyan

    Larger models and batches improved the quality of images generated from ImageNet.

  8. 2018

    StyleGAN Karras, Laine, Aila

    Inject style information at each layer to control image attributes, such as hairstyle, separately.

  9. 2020

    DDPM Ho, Jain, Abbeel

    Diffusion models learn to remove noise one step at a time. They later surpassed GANs on image-generation benchmarks.