Skip to content
HasanAI
Log in Sign up

βš”οΈ GANs: Generative Adversarial Networks

A generator and a discriminator in a minimax game - no likelihood required
Ten Landmark Papers Advanced
Contents

    GANs: Generative Adversarial Networks (Goodfellow et al., 2014)

    The VAE trains a generator by maximising a likelihood bound, and pays for it with blurry samples. This paper skipped likelihoods entirely. Train a generator $G$ to turn noise into samples, and a discriminator $D$ to tell real samples from generated ones. Then let them compete:

    $$\min_G \max_D \; V(D, G) = \mathbb{E}_{x \sim p_{data}}[\log D(x)] + \mathbb{E}_{z \sim p_z}[\log(1 - D(G(z)))]$$

    $D$ wants to output 1 on real data and 0 on fakes. $G$ wants its fakes to be scored as real. Nobody ever writes down a density for the generator. It only has to produce samples, and the discriminator's gradient tells it how to make them more realistic.

    1. The optimal discriminator, derived and checked numerically
    2. What the game minimises: Jensen–Shannon divergence, checked
    3. Why the paper changes the generator's loss in practice
    4. Training a real GAN in 1-D, frame by frame
    5. Mode collapse and what came after
    helpers ready

    1. The optimal discriminator

    Fix the generator, so its samples follow some density $p_g$. Writing $V$ as one integral over $x$:

    $$V = \int \big[p_{data}(x)\log D(x) + p_g(x)\log(1 - D(x))\big]\,dx$$

    $D(x)$ can be chosen separately at every $x$. For $a\log y + b\log(1-y)$, the maximum is at $y = a/(a+b)$. So the best possible discriminator is

    $$D^*(x) = \frac{p_{data}(x)}{p_{data}(x) + p_g(x)}$$

    It says 1 where only real data lives, 0 where only fakes live, and Β½ where the two are equally likely. The cell checks this by maximising $V$ numerically, with gradient ascent on a free logit at each of 200 grid points, and comparing the result to the formula. Because the objective splits by $x$, each point gets its own step size, so the far tails converge as fast as the centre.

    ==============================================================================
      D* = p_data / (p_data + p_g), BY OPTIMISATION
    ==============================================================================
      max |D_optimised - D_formula| over the grid: 2.28e-04
      [PASS] gradient ascent on V lands on p_data / (p_data + p_g)
      x = -1.48:  p_data 0.3323   p_g 0.0849   D* 0.7964
      x = -0.03:  p_data 0.0293   p_g 0.3021   D* 0.0885
      x = +0.48:  p_data 0.0793   p_g 0.3324   D* 0.1925
      x = +2.99:  p_data 0.0152   p_g 0.0386   D* 0.2828

    2. What the generator is really minimising

    Substitute $D^*$ back into $V$ and the generator's objective becomes, as the paper shows,

    $$C(G) = \max_D V(D, G) = -\log 4 + 2\cdot \mathrm{JSD}(p_{data} \,\|\, p_g)$$

    The Jensen–Shannon divergence is zero only when the two distributions are equal. So the game has a unique optimum: $p_g = p_{data}$, where $D^* = Β½$ everywhere and $V = -\log 4 \approx -1.386$. The cell checks the identity for several generators.

    ==============================================================================
      max_D V  ==  -log 4 + 2 * JSD
    ==============================================================================
      wide Gaussian  max_D V = -1.14444   -log4 + 2*JSD = -1.14444   JSD = 0.12093
      one mode only  max_D V = -0.96691   -log4 + 2*JSD = -0.96691   JSD = 0.20969
      close          max_D V = -1.38015   -log4 + 2*JSD = -1.38015   JSD = 0.00307
      exact          max_D V = -1.38629   -log4 + 2*JSD = -1.38629   JSD = 0.00000
      [PASS] identity holds for every generator tried (to 1e-9)
      largest discrepancy: 2.2e-16
      at p_g = p_data:  V = -1.38629 = -log 4

    3. The loss the paper actually trains with

    Early in training the generator is bad and the discriminator rejects its samples confidently: $D(G(z)) \approx 0$. In that region $\log(1 - D(G(z)))$ is nearly flat, so the generator gets almost no gradient exactly when it needs it most. The paper's fix is to have the generator maximise $\log D(G(z))$ instead. It has the same fixed point and much stronger gradients early on. This "non-saturating" loss is what practically every GAN uses.

    The gradient with respect to the discriminator's logit $\ell$, where $D = \sigma(\ell)$, makes the difference concrete.

    ==============================================================================
      SATURATING vs NON-SATURATING GENERATOR LOSS
    ==============================================================================
      D(G(z)) = 0.001   gradient of log(1-D): -0.0010   gradient of log D: +0.9990   ratio   999.0x
      D(G(z)) = 0.01    gradient of log(1-D): -0.0100   gradient of log D: +0.9900   ratio    99.0x
      D(G(z)) = 0.1     gradient of log(1-D): -0.1000   gradient of log D: +0.9000   ratio     9.0x
      D(G(z)) = 0.5     gradient of log(1-D): -0.5000   gradient of log D: +0.5000   ratio     1.0x
      D(G(z)) = 0.9     gradient of log(1-D): -0.9000   gradient of log D: +0.1000   ratio     0.1x

    4. Training a GAN in one dimension

    The target is a two-peaked distribution. The generator is a small MLP that turns 8-dimensional Gaussian noise into one number. The discriminator is a small MLP that turns one number into a probability. They alternate updates with Adam (learning rate $2\times10^{-4}$, $\beta_1 = 0.5$, the DCGAN settings) and the non-saturating loss, as in the paper's Algorithm 1 with $k = 1$. These settings were chosen after trial runs: at a learning rate of $10^{-3}$ the generator's distance to the data kept oscillating between about 0.2 and 0.5 instead of settling. GANs are sensitive to this, as the next section explains.

    Every 150 steps the cell records the generator's density, estimated from 20,000 samples, and the discriminator's output across the line. Those snapshots drive the animation below.

    ==============================================================================
      TRAINING
    ==============================================================================
      step    0:  D loss 1.414   G loss 0.743   generator mean +0.187  std 0.056
      step  600:  D loss 1.325   G loss 0.735   generator mean +0.001  std 1.628
      step 1500:  D loss 1.397   G loss 0.702   generator mean -0.008  std 1.554
      step 6000:  D loss 1.389   G loss 0.693   generator mean -0.003  std 1.576
    
    Final generator vs data
    ------------------------------------------------------------------------------
      data       share below 0: 0.503   mean -0.007   std 1.613
      generator  share below 0: 0.501   mean -0.003   std 1.576
      Wasserstein-1 distance to the data: 1.458 at step 0  ->  0.036 at the end
      [PASS] the generator ends far closer to the data than it started (W1 shrinks by > 5x)
      [PASS] both modes are covered (each holds 35-65% of samples)
      discriminator output on [-3, 3] at the end: min 0.440  max 0.506  (1/2 would be a perfect tie)
      distance along the way: best 0.029 at step 3000; after step 750 it still swung as high as 0.260

    5. Mode collapse, and what came after

    The theory assumes $D$ is trained to optimality at every step and that the model has unlimited capacity. Neither holds in practice, and GAN training became famous for being unstable. The most notorious failure is mode collapse. The generator finds one output the discriminator currently accepts and produces only that. The discriminator adapts, the generator jumps to another single mode, and the two chase each other around. Small runs like the one above can show partial versions of this: one mode filled before the other, or temporary oscillations visible in the animation.

    A decade of follow-up work went into stability and quality:

    • DCGAN (2015): convolutional architecture guidelines that made image GANs train reliably.
    • WGAN (2017): replaces the JS divergence with the Wasserstein distance, which gives useful gradients even when the two distributions do not overlap.
    • StyleGAN (2019–2021): style-based generators with photorealistic faces.

    For most image generation, GANs have since been overtaken by diffusion models (next page), which are slower to sample from but far more stable to train and better at covering every mode. GAN-style adversarial losses survive inside other systems, such as the decoder of the autoencoder used by Stable Diffusion.

    Summary

    • Two networks play a minimax game: $D$ separates real from fake, and $G$ tries to fool $D$. No density for $G$ is ever written down.
    • The optimal discriminator is $p_{data}/(p_{data}+p_g)$, found here by direct optimisation to within $10^{-3}$.
    • At that discriminator the game value is $-\log 4 + 2\,\mathrm{JSD}$, checked to $10^{-9}$, so the global optimum is $p_g = p_{data}$.
    • The non-saturating generator loss $-\log D(G(z))$ has the same optimum and gradients up to hundreds of times larger early in training.
    • A 1-D GAN trained here moved from a narrow lump to both modes of the target. The Wasserstein distance to the data shrank by more than 5Γ—, though not monotonically, and the discriminator ended near Β½.

    References

    1. Generative Adversarial Networks β€” Goodfellow et al., 2014 (NeurIPS)
    2. Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks (DCGAN) β€” Radford et al., 2015
    3. Wasserstein GAN β€” Arjovsky et al., 2017