Search

Search papers and lessons

Generative Adversarial Networks

Ian J. Goodfellow, Jean Pouget-Abadie, Mehdi Mirza et al. · 2014

Goodfellow et al. train a generator and a discriminator against each other, and at the optimum the generator reproduces the data while the discriminator can only guess.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Generative models
  • Linear algebra
  • Neural network basics
  • Discrete math
  • Sign in to see your progress

TL;DR

Goodfellow et al. (2014) train two networks against each other. A generator GG turns random noise into samples, and a discriminator DD tries to tell those samples from real training data. GG is trained to fool DD and DD is trained to catch GG. At the optimum, GG reproduces the data distribution and DD outputs 12\frac{1}{2} everywhere. The whole system is trained with backpropagation, with no Markov chains and no explicit likelihood.

Where you'll see it: the adversarial loss behind most image synthesis and restoration work since 2014, and the starting point for conditional GANs, Wasserstein GANs and StyleGAN.

The problem

Before 2014, deep generative models were hard to train. Undirected models such as restricted Boltzmann machines and deep Boltzmann machines have a partition function whose gradient is intractable, so training leans on Markov chain Monte Carlo, which mixes slowly. Directed latent-variable models need approximate inference during training. Score matching and noise-contrastive estimation need the density to be analytically defined up to a normalising constant, which many multi-layer models do not offer. Generative stochastic networks avoid the density but still need a Markov chain at sampling time.

The paper's goal is a model where sampling takes one forward pass, training uses only backpropagation, and no inference network or chain is needed. Piecewise linear units such as ReLU and maxout train well under backprop, but they misbehave inside feedback loops, so the method should avoid loops entirely.

Key idea

Think of counterfeiters and police. The counterfeiter (the generator) makes fake banknotes. The police (the discriminator) inspect notes and decide real or fake. Each side improves in response to the other. The paper's observation is that the police's job is an ordinary binary classification problem, so the whole game can be trained with the same gradient tools used for classifiers.

flowchart LR
  Z[noise z] --> G[generator G]
  G --> F[fake sample]
  R[real sample x] --> D[discriminator D]
  F --> D
  D --> P[probability the input is real]

DD is pushed up on real samples and down on fakes. GG is pushed in the opposite direction on its fakes. Neither network ever sees the other's parameters, only the signal that flows through DD.

The method

Let pdatap_{\text{data}} be the data distribution and pzp_{\mathbf{z}} a simple prior over noise, such as a uniform or Gaussian distribution. The generator G(z;θg)G(\mathbf{z}; \theta_g) is a multi-layer perceptron that maps noise to data space. It defines a model distribution pgp_g as the distribution of G(z)G(\mathbf{z}) when z∼pz\mathbf{z} \sim p_{\mathbf{z}}. The discriminator D(x;θd)D(\mathbf{x}; \theta_d) is a second perceptron that outputs a single number in [0,1][0, 1], read as the probability that x\mathbf{x} came from the data.

The two players share one value function:

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

DD maximises VV and GG minimises it, which gives the minimax game min⁡Gmax⁡DV(G,D)\min_G \max_D V(G, D). The first term rewards DD for calling real data real. The second rewards DD for calling fakes fake, and it is the term GG attacks.

The optimal discriminator

Proposition 1. For a fixed GG, the best possible discriminator is

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

The proof rewrites VV as an integral over x\mathbf{x} of pdata(x)log⁡D(x)+pg(x)log⁡(1−D(x))p_{\text{data}}(\mathbf{x}) \log D(\mathbf{x}) + p_g(\mathbf{x}) \log(1 - D(\mathbf{x})). Each point contributes alog⁡y+blog⁡(1−y)a \log y + b \log(1 - y) with a=pdata(x)a = p_{\text{data}}(\mathbf{x}) and b=pg(x)b = p_g(\mathbf{x}). Setting the derivative ay−b1−y\frac{a}{y} - \frac{b}{1 - y} to zero gives y=aa+by = \frac{a}{a + b}, which is the formula above.

The global optimum

Substitute DG∗D^*_G back into VV to get a criterion that depends only on GG:

C(G)=max⁡DV(G,D)=−log⁡4+2 JSD(pdata ∥ pg)C(G) = \max_D V(G, D) = -\log 4 + 2\,\mathrm{JSD}(p_{\text{data}} \,\|\, p_g)

Here JSD\mathrm{JSD} is the Jensen–Shannon Divergence, built from two KL divergences to the average of the two distributions. The Jensen–Shannon divergence is never negative and is zero only when its inputs are equal. So C(G)C(G) reaches its smallest value, −log⁡4-\log 4, exactly when pg=pdatap_g = p_{\text{data}}. At that point DG∗=12D^*_G = \frac{1}{2} everywhere, and C=log⁡12+log⁡12=−log⁡4C = \log\frac{1}{2} + \log\frac{1}{2} = -\log 4.

This is the paper's main theoretical claim: in the non-parametric limit, where GG and DD can represent any function, the game has one solution and it is the data distribution.

Convergence of the training loop

Training cannot run DD to its optimum at every step, since that is too expensive and overfits a finite dataset. The paper's Proposition 2 says that if DD is allowed to reach its optimum at each step and pgp_g is updated to improve CC, then pgp_g converges to pdatap_{\text{data}}. The argument: for a fixed DD, the value is linear in pgp_g, and a supremum of linear functions is convex. A convex objective with a unique minimum, updated in small steps, converges to that minimum.

The catch, which the paper states, is that in practice pgp_g is not optimised directly. Only the parameters θg\theta_g are, and a multi-layer perceptron creates many critical points in parameter space. The argument therefore does not carry over to deep networks. The authors' answer is that the method works well empirically despite this.

The training algorithm

Algorithm 1 alternates between kk steps of ascending DD's stochastic gradient on a minibatch and one step of descending GG's. Each DD step uses mm real examples and mm fresh noise samples. Each GG step uses mm new noise samples. The paper uses k=1k = 1 in its experiments, the cheapest option, and applies momentum as the gradient rule.

Larger kk would keep DD closer to its optimum for the current GG, which matters for the synchronisation problem discussed later.

The non-saturating generator loss

The generator minimises log⁡(1−D(G(z)))\log(1 - D(G(\mathbf{z}))) in Algorithm 1. Early in training GG produces obvious fakes, DD rejects them confidently, and this term saturates, so its gradient is tiny. The paper proposes that GG instead maximise log⁡D(G(z))\log D(G(\mathbf{z})). This has the same fixed point but gives much stronger gradients at the start.

Example
Why the non-saturating loss learns faster

Let D(G(z))=σ(a)D(G(\mathbf{z})) = \sigma(a), where aa is the discriminator's pre-sigmoid score for one fake and σ(a)=11+e−a\sigma(a) = \frac{1}{1 + e^{-a}}.

  1. Saturating loss: ddalog⁡(1−σ(a))=−σ(a)\frac{d}{da}\log(1 - \sigma(a)) = -\sigma(a). At σ(a)=0.01\sigma(a) = 0.01, the gradient is −0.01-0.01.
  2. Non-saturating loss: ddalog⁡σ(a)=1−σ(a)\frac{d}{da}\log \sigma(a) = 1 - \sigma(a). At σ(a)=0.01\sigma(a) = 0.01, the gradient is 0.990.99.
  3. The ratio is 0.99/0.01=990.99 / 0.01 = 99. Early in training the non-saturating loss gives about 99 times the gradient signal to the generator.

Interpretation: when the discriminator is confident the fake is fake, the original loss gives the generator almost nothing to learn from. Switching to log⁡D\log D fixes that without changing where training ends.

Results

The paper trains on MNIST, the Toronto Face Database (TFD) and CIFAR-10. The generator is a mix of ReLU and sigmoid units, and the discriminator uses maxout units with dropout. Noise enters only at the bottom layer of the generator.

The paper has no standard metric for generative models, so it estimates the test-set log-likelihood under pgp_g. It draws samples from GG, fits a Gaussian Parzen window (a kernel density estimate with one Gaussian per sample) around them, and reports the log-likelihood of held-out test points under that window. The Gaussian width is chosen by cross-validation on a validation set. The authors say this estimator has high variance and does poorly in high dimensions. They call it the best available option.

ModelMNISTTFD
DBN138±2138 \pm 21909±661909 \pm 66
Stacked CAE121±1.6121 \pm 1.62110±50\mathbf{2110 \pm 50}
Deep GSN214±1.1214 \pm 1.11890±291890 \pm 29
Adversarial nets225±2\mathbf{225 \pm 2}2057±26\mathbf{2057 \pm 26}

Adversarial nets score highest on MNIST. On TFD the paper's table and the numbers differ in how they read. The table bolds both stacked CAE (21102110) and adversarial nets (20572057), but the numbers put the adversarial nets second, 5353 below the stacked CAE and above the DBN (19091909) and deep GSN (18901890). The paper does not say why both are bold. The gap is about the size of the quoted standard errors (5050 and 2626), so a tie is a reasonable reading, but that is our inference, not the paper's statement. Note also that the standard errors mean different things in the two columns: across test examples on MNIST, across dataset folds on TFD. The authors do not claim their samples are better than those of other models. They say the samples are "at least competitive" and that the figures show the framework's potential. The figures include random draws, not cherry-picked ones, and nearest-neighbour training images to show the model has not memorised data. The paper also shows digits from linear interpolation in z\mathbf{z} space, which suggests the generator learns a smooth latent space.

Why it mattered

The paper turned a set of theoretical ideas into a method anyone could run with a few lines of backpropagation code. Its key properties were no Markov chains, exact sampling by a single forward pass, and no need to write down a density. Each of these was a known pain point for the models of its day.

It also left clear problems open. The paper names two. The first is that pgp_g has no explicit form, so the model cannot score a given example. The second is training instability: DD must stay synchronised with GG. If GG trains too far ahead of DD, it can collapse many values of z\mathbf{z} onto one output, which the paper calls the "Helvetica scenario".

Later work took the paper in several directions. Conditional GANs add a label to both networks, which is extension 1 in the paper's conclusion. Deep convolutional GANs (2015) made training stable enough for natural images. Wasserstein GANs (2017) changed the distance being minimised to address the instability. StyleGAN (2018 to 2019) pushed image quality far beyond the paper's samples. Most of today's adversarial losses trace back to this paper's two-player setup.

Reading guide

Read the paper in this order. Each part needs the listed prerequisites.

  1. Introduction and Section 2 (related work). Needs Generative Model and Latent Variable Model to follow why the older approaches are hard to train.
  2. Section 3 (adversarial nets) and Figure 1. Needs Minimax Game, Feedforward Neural Network and Backpropagation for the value function and the two networks. The non-saturating loss paragraph uses Cross-Entropy Loss intuition: DD's objective is a binary log-likelihood.
  3. Algorithm 1. Needs Stochastic Gradient Descent and Momentum for the minibatch updates.
  4. Section 4.1 (Proposition 1 and Theorem 1). Needs Probability Density Function for the densities, Kullback–Leibler Divergence and Jensen–Shannon Divergence for the final identity. This is the mathematical core, so read it slowly.
  5. Section 4.2 (Proposition 2). Needs the idea of convexity. Treat it as motivation: the convergence result does not hold for deep networks, as the paper says.
  6. Section 5 (experiments). The Parzen estimate needs Probability Density Function and the Gaussian kernel. Table 2 is a useful summary of how each model family handles training, inference and sampling.
  7. Section 6 and 7. Advantages, disadvantages and extensions. Read these last, since they assume the rest of the paper.

Check yourself

QuestionWrite down the value function V(G, D) and say which player maximises it.Show answer

V(G,D)=Ex∼pdata[log⁡D(x)]+Ez∼pz[log⁡(1−D(G(z)))]V(G, D) = \mathbb{E}_{\mathbf{x} \sim p_{\text{data}}}[\log D(\mathbf{x})] + \mathbb{E}_{\mathbf{z} \sim p_{\mathbf{z}}}[\log(1 - D(G(\mathbf{z})))]. The discriminator DD maximises it, since it wants to label real data as real and fakes as fake. The generator GG minimises it.

QuestionAt a point x, p_data(x) = 0.8 and p_g(x) = 0.5. What is the optimal discriminator D*(x)?Show answer

D∗(x)=0.80.8+0.5=0.81.3≈0.615D^*(x) = \frac{0.8}{0.8 + 0.5} = \frac{0.8}{1.3} \approx 0.615. The discriminator should lean towards real here, because the data is more likely to produce this point than the generator is.

QuestionWhy does the optimal value of the game equal minus log 4?Show answer

At the global optimum pg=pdatap_g = p_{\text{data}}, so D∗=12D^* = \frac{1}{2} everywhere. Each expectation then contributes log⁡12\log \frac{1}{2}, and the sum is log⁡12+log⁡12=−log⁡4\log \frac{1}{2} + \log \frac{1}{2} = -\log 4. The theorem shows this is the minimum of C(G)C(G), because C(G)=−log⁡4+2 JSD(pdata ∥ pg)C(G) = -\log 4 + 2\,\mathrm{JSD}(p_{\text{data}} \,\|\, p_g) and the divergence is never negative.

QuestionWhy does the paper switch the generator's loss from minimising log(1 - D(G(z))) to maximising log D(G(z))?Show answer

When DD confidently rejects early fakes, D(G(z))D(G(\mathbf{z})) is near zero. The original loss then has a gradient near zero, so GG learns very slowly. The alternative loss has the same fixed point but a gradient larger by a factor of (1−σ(a))/σ(a)(1 - \sigma(a)) / \sigma(a) at that point. The worked example in this lesson gives a factor of 99 at σ(a)=0.01\sigma(a) = 0.01.

QuestionA generator trained on digits produces only sevens. Which failure does the paper describe, and what does it recommend to limit it?Show answer

This is the Helvetica scenario, where the generator collapses many noise values to the same output. The paper's advice is that GG must not be trained too much without updating DD, so the two stay synchronised. The kk inner discriminator steps of Algorithm 1 are the natural knob for this, though the paper itself used k=1k = 1. It does not claim any setting fully prevents collapse; it lists synchronisation as a disadvantage of the framework.

Sources

Read the full paper

Ian J. Goodfellow, Jean Pouget-Abadie, Mehdi Mirza et al., 2014. arXiv:1406.2661