← Roadmap

Generative Adversarial Nets

Goodfellow et al. · 2014

① Intuition

Before GANs, deep generative models were hard to train well. Variational autoencoders required explicit likelihood formulations and blurry samples; energy-based models were unwieldy. In 2014, Goodfellow et al. proposed a radically different idea: pit two networks against each other in a minimax game.

The Generator G takes random noise z from a prior p_z (e.g. Gaussian) and tries to produce samples indistinguishable from real data. The Discriminator D looks at a sample and tries to tell real from fake. Each network is trained to defeat the other. At equilibrium, G produces samples so realistic that D can do no better than random guessing — outputting 0.5 for both real and fake inputs.

Before playing with the widget: if the generator improves but the discriminator stays fixed, what do you expect happens to D(fake)?

Drag the training-step slider to watch D(real) and D(fake) oscillate during training. Check “Converged equilibrium” to snap both to 0.5 — the Nash equilibrium state.

GAN Adversarial Dynamics

Discriminator output

D(real)80.0%
D(fake)52.1%

Generator

G mean: 0.500Target: 2.0

Blue = target (2.0) · Orange = G mean

At convergence both D(real) ≈ 0.5 and D(fake) ≈ 0.5. What does this say about the generator's distribution relative to the real data distribution?

Retrieval check

What does the generator G receive as input?
Why is the GAN training described as a minimax game?

Answer all 2 correctly to mark this stage mastered.

② Math

The GAN objective is a minimax value function over the discriminator D and generator G:

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

D wants to maximize V (correctly label real as real, fake as fake). G wants to minimize V (fool D into labeling fakes as real). Written compactly:

min⁡Gmax⁡D  V(G,D)\min_G \max_D \; V(G,D)

The first term rewards D for outputting high probabilities on real data. The second term rewards D for outputting low probabilities on generated data (and punishes G for failing to fool D).

Given a fixed G, what is the optimal discriminator D*(x) that maximises V(G,D) for each x independently?
Show next line
D∗(x)=pdata(x)pdata(x)+pg(x)D^*(x) = \frac{p_\text{data}(x)}{p_\text{data}(x) + p_g(x)}
Substituting D*(x) back into V(G, D*), what quantity does minimizing over G equal minimizing?
Show next line
min⁡GV(G,D∗)=−log⁡4+2 JSD(pdata ∥ pg)\min_G V(G,D^*) = -\log 4 + 2\,\mathrm{JSD}(p_\text{data} \,\|\, p_g)

Retrieval check

In the GAN value function, what does the term E_{z~p_z}[log(1 - D(G(z)))] represent for G?
Why does the optimal D* have value 0.5 when p_g = p_data?

Answer all 2 correctly to mark this stage mastered.

③ Pseudo code

Arrange the GAN alternating training loop steps in the correct order:

  1. Sample minibatch of m real examples x^(1)...x^(m) from p_data
  2. Sample minibatch of m noise vectors z^(1)...z^(m) from p_z
  3. Update D by ascending its stochastic gradient: ∇_θd [Σ log D(x^(i)) + log(1 - D(G(z^(i))))]
  4. Sample new minibatch of m noise vectors z^(1)...z^(m) from p_z
  5. Update G by descending its stochastic gradient: ∇_θg [Σ log(1 - D(G(z^(i))))]

Fill the blank in the discriminator update rule:

# Discriminator loss (to maximise)
d_loss = mean(log(D(x_real)) + log(1 - D(______)))

Retrieval check

In the alternating training loop, why is G updated with a fresh batch of noise rather than reusing the batch used for the D update?

Answer all 1 correctly to mark this stage mastered.

④ Code (Python)

Implement the discriminator loss for one training step in NumPy. Given arrays real_probs (D’s outputs on real data) and fake_probs (D’s outputs on G’s samples), compute the negative minimax objective that D minimises (i.e. the binary cross-entropy loss):

loss = -mean(log(D(real)) + log(1 - D(fake)))

Retrieval check

Why do we add a small epsilon (1e-8) inside the log in the discriminator loss?

Answer all 1 correctly to mark this stage mastered.

⑤ Original paper

Your browser can't embed PDFs. Download Generative Adversarial Nets.

Open in new tab · Download

Suggested reading order: §3 (Adversarial Nets — the minimax formulation) → §4.1 (Global optimality of p_g = p_data — the D* derivation and JSD proof) → §4.2 (Convergence of the algorithm — the alternating training convergence argument).