Search

Search papers and lessons

Generative Adversarial Network

Level 4 · 10 min read

A generative adversarial network pits a generator that turns noise into fake samples against a discriminator that spots them, and training both at once makes the fakes look real.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Linear algebra
  • Generative models
  • Discrete math
  • Losses & regularization
  • Sign in to see your progress

TL;DR

A generative adversarial network, or GAN, is a pair of neural networks trained against each other. The generator takes random noise and turns it into a fake sample, such as an image. The discriminator looks at a sample and guesses whether it came from the real data or from the generator. Each network improves by beating the other, and when training works, the generator ends up producing samples the discriminator cannot tell apart from real ones.

Where you'll see it: early photorealistic face generation, image-to-image translation such as turning sketches into photos, and data augmentation for rare medical or industrial images.

Intuition

A counterfeiter and a detective

Picture a counterfeiter who wants to pass forged banknotes, and a detective who wants to catch them. At the start the forgeries are crude and the detective catches every one. Each time a forgery is caught, the counterfeiter studies what gave it away and improves. The detective, in turn, has to get sharper, because the forgeries are better now. Neither side is ever told exactly what a real note looks like. Each only knows whether it fooled the other.

A GAN is this contest written as a training loop. The generator is the counterfeiter, the discriminator is the detective, and the real data is the genuine currency. The generator never sees a real example directly. Its only feedback comes through the discriminator's judgement.

Why two networks instead of one

The obvious way to learn a data distribution is to maximise the likelihood of the training data, as in latent variable models and autoregressive models. That needs a density you can compute exactly or at least bound tightly, which is hard for images. A GAN sidesteps the problem. It never writes down the density of its output. It only needs to produce samples, and it trains by asking whether a sample looks real. The discriminator is a learned, flexible judge of realism, so the generator is not forced to fit a formula.

The plot below shows the goal in one dimension. The real data follows one curve, and the generator starts with a different one. Training pushes the generator's curve on top of the real one. When the two curves coincide, the discriminator is reduced to guessing.

−4−202400.10.20.30.40.5xdensity
  • real data p_data
  • generator p_g, early
  • generator p_g, late
Training moves the generator's distribution onto the real one. Once they overlap completely, the discriminator cannot do better than a coin flip.

The math

Setup

Let pdata(x)p_{\text{data}}(x) be the distribution of real samples xx, such as images. The generator GG takes a noise vector zz, drawn from a simple distribution pzp_z such as a standard normal, and outputs a sample G(z)G(z). The distribution of those outputs is written pgp_g. The discriminator DD takes a sample xx and returns a number D(x)D(x) between 0 and 1, read as the probability that xx is real.

The value function

Goodfellow and co-authors wrote the training problem as a single objective:

min⁡Gmax⁡D V(D,G)=Ex∼pdata[log⁡D(x)]+Ez∼pz[log⁡(1−D(G(z)))].\min_G \max_D\ V(D, G) = \mathbb{E}_{x \sim p_{\text{data}}}\big[\log D(x)\big] + \mathbb{E}_{z \sim p_z}\big[\log\big(1 - D(G(z))\big)\big].

Each term does one job. The first term rewards DD for scoring real samples close to 1, since log⁡D(x)\log D(x) is largest when D(x)D(x) is largest. The second term rewards DD for scoring fake samples close to 0, since log⁡(1−D(G(z)))\log(1 - D(G(z))) is largest when D(G(z))D(G(z)) is smallest. The discriminator maximises VV. The generator minimises it, which means making D(G(z))D(G(z)) large, so the fakes look real. Expectations here are averages over the sampling distribution written under E\mathbb{E}.

The discriminator's best response

Fix the generator, so pgp_g is a known fixed distribution. For each point xx, the discriminator's best score is the one that maximises the integrand. Writing the integrand as pdata(x)log⁡D+pg(x)log⁡(1−D)p_{\text{data}}(x)\log D + p_g(x)\log(1 - D) and setting its derivative in DD to zero gives

DG∗(x)=pdata(x)pdata(x)+pg(x).D^*_G(x) = \frac{p_{\text{data}}(x)}{p_{\text{data}}(x) + p_g(x)}.

Intuitively, D∗D^* says: at a point where real data is common and generated data is rare, the answer is close to 1, and the reverse gives a value close to 0. Where the two densities are equal, the answer is exactly one half.

What the generator is really minimising

Substitute DG∗D^*_G back into VV. Write M=12(pdata+pg)M = \tfrac{1}{2}(p_{\text{data}} + p_g) for the average of the two distributions. Each fraction can be rewritten as 12\tfrac{1}{2} times a ratio against MM:

V(DG∗,G)=Epdata[log⁡pdata2M]+Epg[log⁡pg2M]=−log⁡4+KL(pdata ∥ M)+KL(pg ∥ M)=−log⁡4+2 JSD(pdata ∥ pg).\begin{aligned} V(D^*_G, G) &= \mathbb{E}_{p_{\text{data}}}\Big[\log \tfrac{p_{\text{data}}}{2M}\Big] + \mathbb{E}_{p_g}\Big[\log \tfrac{p_g}{2M}\Big] \\ &= -\log 4 + \mathrm{KL}(p_{\text{data}} \,\|\, M) + \mathrm{KL}(p_g \,\|\, M) \\ &= -\log 4 + 2\,\mathrm{JSD}(p_{\text{data}} \,\|\, p_g). \end{aligned}

The last line uses the definition of the Jensen-Shannon divergence: an average of two KL divergences, each taken against the mixture MM. This is the key result of the paper. If the discriminator is perfect, the generator's objective is a constant plus twice a divergence between the real and generated distributions. The JSD is zero only when pg=pdatap_g = p_{\text{data}}, so that is the global minimum, with value −log⁡4-\log 4.

Training in practice

Real training cannot compute DG∗D^*_G at every step. Instead, both networks are neural networks, typically feedforward networks or convolutional ones, and they are trained by gradient descent with mini-batches. Each iteration does one or more gradient steps for DD that increase VV, then one step for GG that decreases VV. The discriminator's loss is a binary cross-entropy between its guesses and the labels real and fake.

For the generator, the literal objective has a known weakness, discussed in the worked examples. The standard fix is the non-saturating loss, which minimises −log⁡D(G(z))-\log D(G(z)) instead of log⁡(1−D(G(z)))\log(1 - D(G(z))). The two have the same fixed points, but the second gives much stronger gradients early in training.

Worked examples

Example
A tiny discrete world

Let the sample space be just two values, x=0x = 0 and x=1x = 1. Let the real distribution be pdata=(0.8, 0.2)p_{\text{data}} = (0.8,\ 0.2) and the generator's distribution be pg=(0.4, 0.6)p_g = (0.4,\ 0.6).

  1. The best discriminator scores are D∗(0)=0.80.8+0.4=0.667D^*(0) = \dfrac{0.8}{0.8 + 0.4} = 0.667 and D∗(1)=0.20.2+0.6=0.250D^*(1) = \dfrac{0.2}{0.2 + 0.6} = 0.250.
  2. Compute the value directly. The real term is 0.8ln⁡0.667+0.2ln⁡0.250=−0.324−0.277=−0.6020.8 \ln 0.667 + 0.2 \ln 0.250 = -0.324 - 0.277 = -0.602.
  3. The fake term is 0.4ln⁡(1−0.667)+0.6ln⁡(1−0.250)=−0.439−0.173=−0.6120.4 \ln(1 - 0.667) + 0.6 \ln(1 - 0.250) = -0.439 - 0.173 = -0.612.
  4. Add them: V(D∗,G)=−0.602−0.612=−1.214V(D^*, G) = -0.602 - 0.612 = -1.214 nats.
  5. Check with the formula. The mixture is M=(0.6, 0.4)M = (0.6,\ 0.4). The two KL terms are 0.09150.0915 and 0.08110.0811, so JSD=12(0.0915+0.0811)=0.0863\mathrm{JSD} = \tfrac{1}{2}(0.0915 + 0.0811) = 0.0863. Then −log⁡4+2⋅0.0863=−1.386+0.173=−1.213-\log 4 + 2 \cdot 0.0863 = -1.386 + 0.173 = -1.213, which matches up to rounding.

Interpretation: the value sits between −log⁡4=−1.386-\log 4 = -1.386, the best the generator can achieve, and 00, the worst case, reached when the two distributions are disjoint (JSD =log⁡2= \log 2 gives −log⁡4+2log⁡2=0-\log 4 + 2\log 2 = 0). The generator would improve by moving pgp_g toward (0.8, 0.2)(0.8,\ 0.2).

Example
Why the non-saturating loss exists

Let the discriminator's output on a fake sample be D(G(z))=0.1D(G(z)) = 0.1. Suppose the discriminator's raw score (its logit) is aa, with D=σ(a)D = \sigma(a) and σ\sigma the logistic function. Then ddalog⁡(1−σ(a))=−σ(a)=−0.1\frac{d}{da}\log(1 - \sigma(a)) = -\sigma(a) = -0.1, and dda(−log⁡σ(a))=−(1−σ(a))=−0.9\frac{d}{da}\big(-\log \sigma(a)\big) = -(1 - \sigma(a)) = -0.9.

  1. Saturating loss: the generator's gradient is about −0.1-0.1, a small signal.
  2. Non-saturating loss: the gradient is about −0.9-0.9, nine times larger.
  3. Numerically, −log⁡0.1=2.303-\log 0.1 = 2.303 and log⁡(1−0.1)=−0.105\log(1 - 0.1) = -0.105. The non-saturating loss is steep where the generator is losing, and flat where it is already winning.

Interpretation: early in training, the discriminator easily spots fakes, so D(G(z))D(G(z)) is near 0. The literal objective gives the generator almost no gradient exactly when it needs one most. The non-saturating form fixes this and is what most implementations use.

Tradeoffs and failure modes

GANs produce sharp samples and are fast to sample from, since one forward pass through GG gives one sample. They have no tractable likelihood, so they cannot tell you how probable a given image is, and they do not cover the whole data distribution by default.

SituationProblemBetter choice
Discriminator wins too earlyVanishing gradient for GGNon-saturating loss, or Wasserstein loss
Generator produces a few good samples repeatedlyMode collapse: low diversityMinibatch discrimination, unrolled updates, or a different objective
Need exact likelihoods or latent inferenceGAN gives samples only[[latent-variable-model
Need stable training at scaleOscillating, non-convergent updatesDiffusion models, which train by a simple regression loss
Disjoint supports between pgp_g and pdatap_{\text{data}}JSD is flat at log⁡2\log 2, so no signalWasserstein distance, which keeps changing as the distributions move

The training dynamics are the hard part. The game is not a simple minimisation, and the two networks can chase each other in circles, see minimax games. Evaluation is also difficult. There is no single loss curve that says the samples are good, so practitioners inspect samples or use proxy scores such as the Frechet Inception Distance.

History

Ian Goodfellow and collaborators at the Université de Montréal published the GAN framework at NeurIPS in 2014. The problem they were solving was generative modelling of high-dimensional data such as images, where maximum likelihood methods of the time either needed intractable densities or relied on approximations that produced blurry samples. Their idea was to replace the likelihood with a learned adversary.

Early GAN images were small and blurry. Progress came from architectural work, most notably DCGAN in 2015 by Radford, Metz and Chintala, which established convolutional designs and training recipes that became the default. The Wasserstein GAN of 2017 by Arjovsky, Chintala and Bottou addressed the instability of the JSD objective. By the late 2010s, GANs were producing high-resolution photorealistic faces and became the standard reference point for image generation research. Later, diffusion models overtook them on many benchmarks, but the adversarial idea remains widely used.

Check yourself

QuestionIn one sentence, what does the discriminator learn to do, and what does the generator learn to do?Show answer

The discriminator learns to output a high probability for real samples and a low probability for generated ones, so it acts as a learned realism judge. The generator learns to transform noise into samples that the discriminator scores as real, so it moves its output distribution toward the real data.

QuestionIf the generator's distribution exactly equals the real distribution, what is the value of V(DG∗,G)V(D^*_G, G), and why does the discriminator get stuck?Show answer

When pg=pdatap_g = p_{\text{data}}, the JSD is zero, so V=−log⁡4≈−1.386V = -\log 4 \approx -1.386. The optimal discriminator is D∗=pdata/(pdata+pg)=0.5D^* = p_{\text{data}}/(p_{\text{data}} + p_g) = 0.5 everywhere. It cannot tell real from fake, so it guesses at random.

QuestionA discriminator gives a fake sample the score D(G(z))=0.2D(G(z)) = 0.2. Compute the value of the non-saturating generator loss −log⁡D(G(z))-\log D(G(z)), and say whether the generator should be pleased with this score.Show answer

−log⁡0.2=1.609-\log 0.2 = 1.609. A high loss means the generator is not fooling the discriminator, so it should be unhappy and will get a strong gradient to improve. The score is not pleasing, since the discriminator still identifies this fake with probability 0.8.

QuestionWhy does a GAN's objective tie the generator to the JSD, and what goes wrong when the real and generated distributions do not overlap?Show answer

Substituting the optimal discriminator turns the value into a constant plus twice the JSD between the two distributions, so the generator is effectively minimising JSD. When the supports are disjoint, the JSD is at its maximum of log⁡2\log 2 no matter how far apart the distributions are, so small moves give no gradient. This is the flat-region problem that motivated the Wasserstein distance.

Sources