9 Generative Adversarial Networks
Every generative model we have studied so far (mixture models, the EM algorithm, variational inference) works by defining an explicit probability density and maximising its likelihood. This chapter introduces a radically different idea: what if we could learn to generate realistic data without ever writing down a density?
The generative adversarial network (GAN) achieves this by pitting two neural networks against each other in a game. A generator tries to produce fake data that looks real; a discriminator tries to tell fakes from real data. Through this adversarial competition, the generator learns to produce samples that are indistinguishable from the training data.
Key Idea.
The GAN is an implicit generative model: it defines a sampling procedure but never computes explicitly. This is both a strength (no intractable normalisation constant, no restrictive density assumptions) and a weakness (no likelihood for model comparison, harder to diagnose failure modes).
Historical Note.
Ian Goodfellow conceived the idea of GANs in June 2014 during a discussion with colleagues at a bar in MontrΓ©al [1]. The key insight, using a discriminator to provide a training signal for the generator, came from the realisation that the density ratio is all you need to measure how well the generator is doing, and a classifier naturally estimates this ratio. The paper was submitted, reviewed, and accepted at NeurIPS 2014 in a matter of months, a remarkable pace for what would become one of the most cited papers in machine learning. Within five years, GANs could generate photorealistic faces at resolution [2].
The GAN Framework
Definition 1 (Generator and Discriminator).
A GAN consists of two neural networks:
The generator maps a latent code (typically or ) to a synthetic data point . The distribution of is called .
The discriminator takes a data point and outputs the probability that is real (from ) rather than fake (from ).
The Minimax Objective
The generator and discriminator play a two-player minimax game:
Definition 2 (GAN Value Function).
The GAN value function is (V) The discriminator wants to maximise (correctly classify real and fake); the generator wants to minimise (fool the discriminator): (Minimax)
Example 1 (Reading the Objective).
When for real : (good for ).
When (discriminator correctly rejects fakes): (good for , bad for ).
When (discriminator fooled): (bad for , good for , but minimises, so this is large negative good).
The objective perfectly balances the two players: what helps one hurts the other.
Remark 1 (Connection to Binary Cross-Entropy).
Theoretical Analysis
The Optimal Discriminator
For a fixed generator, we can solve for the best possible discriminator in closed form.
Proposition 1 (Optimal Discriminator).
For a fixed generator with induced distribution , the discriminator that maximises is (OPTD)
Proof.
For fixed , we can write as an integral over : We maximise the integrand pointwise. For any , define and and consider for . Taking the derivative: The second derivative confirms this is a maximum. Substituting back gives .
Example 2 (Interpreting the Optimal Discriminator).
When (generator perfectly matches data), everywhere, so the discriminator can do no better than random guessing. When at some where (generator misses a mode), there, and the discriminator is certain is real.
Insight.
The optimal discriminator is a density ratio estimator: where is the sigmoid function. This connects GANs to the broader idea of density ratio estimation, which appears throughout statistical machine learning.
The GAN Objective Equals the JensenβShannon Divergence
Theorem 1 (GAN Objective and JSD).
Substituting the optimal discriminator into the value function gives: (C) where is the JensenβShannon divergence (Definition Definition 5 in Chapter 4).
Theorem 2 (Global Optimum of the GAN Game).
The global minimum of is , achieved if and only if .
Proof.
Since with equality iff , we have with equality iff . At this equilibrium, for all .
Remark 2 (Connection to Chapter 4).
The GAN game implicitly minimises the JensenβShannon divergence between and , one of the divergence measures studied in Chapter 4. Recall that is symmetric, bounded (), and always defined (unlike the KL divergence, which can be infinite when supports don't match). However, as we saw in Chapter 4, when and have disjoint supports, saturates at , a problem we will revisit when motivating the Wasserstein GAN in the next chapter.
The -GAN Perspective
The original GAN implicitly minimises the JSD. A natural question is: can we minimise any -divergence? The answer is yes, via the variational representation of -divergences.
Variational Lower Bound on -Divergences
Recall from Chapter 4 that the -divergence is , where is convex with . The key tool is the Fenchel conjugate.
Definition 3 (Fenchel Conjugate).
The Fenchel conjugate of a convex function is (Fenchel) Since is closed and convex, the biconjugate satisfies , so .
Proposition 2 (Variational Representation of -Divergence).
For any -divergence: (VAR)
Proof.
Substituting into the definition of : where the inequality (swapping and ) becomes an equality when is allowed to vary freely over all measurable functions. This gives .
Insight.
Equation transforms a divergence computation (which requires knowing the density ratio ) into an optimisation problem (which only requires samples from and ). This is the mathematical engine that powers all GAN-like methods: the function is parameterised by a neural network (the βcriticβ or discriminator), and the optimisation is performed via stochastic gradient ascent.
From -Divergence to GAN Objectives
Different choices of yield different GAN variants. Nowozin et al. [9] systematised this in the f-GAN framework:
Definition 4 (f-GAN Objective).
Given a generator with pushforward , the f-GAN training objective is (FGAN)
| Divergence | GAN Name | ||
| KL | KL-GAN | ||
| Reverse KL | RKLGAN | ||
| JensenβShannon | JS-GAN | ||
| GAN | Original GAN | ||
| Pearson | -GAN | ||
| Squared Hellinger | Hellinger GAN |
Example 3 (Recovering the Original GAN).
Take the GAN generator function from Table 1, whose conjugate is , together with the activation function (where ). Then so the variational objective becomes , which is exactly the original GAN objective . The closely related JensenβShannon row of Table 1 yields instead, giving the JS-GAN rather than the original formulation.
Training GANs in Practice
The Training Algorithm
Algorithm 1 (GAN Training via Alternating SGD).
Input: Dataset , batch size , discriminator steps , learning rates .
for each training iteration:
Discriminator update (repeat times): itemize
Sample from and from .
Update by ascending: itemize
Generator update (once): itemize
Sample from .
Update by descending: itemize
Remark 3 (Choosing ).
The hyperparameter controls the discriminator-to-generator update ratio. Using (one discriminator step per generator step) is the most common choice and keeps training balanced. Using too large risks overfitting the discriminator; using too small means the discriminator provides poor gradient signal. Goodfellow et al. [1] used in their experiments.
The Non-Saturating Loss
Caution.
The minimax generator loss suffers from vanishing gradients early in training. When the discriminator easily rejects fakes (), we have , so the gradient is near zero. The generator receives almost no learning signal precisely when it needs it most.
The practical fix is the non-saturating (NS) loss:
Definition 5 (Non-Saturating Generator Loss).
Instead of minimising , the generator maximises: (LOSS)
Example 4 (Why Non-Saturating Helps).
When (early training):
Saturating loss: , flat, no gradient.
Non-saturating loss: , strong gradient pushing to increase .
When (equilibrium), both losses provide similar gradients. The NS loss simply provides stronger signal when the generator is far from equilibrium.
Remark 4 (What Divergence Does the NS Loss Minimise?).
Under the optimal discriminator, the NS generator loss corresponds to minimising: The reverse KL term is mode-seeking: it penalises the generator for placing mass where is small, but does not penalise missing modes. This contributes to the mode collapse phenomenon (Section Mode Collapse and Failure Modes).
Practical Guidelines: The DCGAN Recipe
Radford et al. [3] identified a set of architectural guidelines that stabilise GAN training for images:
| Guideline | Rationale |
| Replace pooling with strided convolutions | Learnable downsampling |
| Use batch normalisation in and | Stabilises gradient flow |
| Remove fully connected layers | All-convolutional |
| ReLU in generator (except output: tanh) | Avoids saturation |
| LeakyReLU in discriminator | Gradient flow for negatives |
| Use Adam with , | Tuned for stability |
Remark 5 (Spectral Normalisation).
A later influential technique is spectral normalisation [4], which constrains the Lipschitz constant of the discriminator by normalising each weight matrix by its largest singular value. This provides a more principled form of regularisation than batch norm and is now widely used.
Mode Collapse and Failure Modes
Despite their impressive results, GANs suffer from several well-known failure modes.
Mode Collapse
Definition 6 (Mode Collapse).
Mode collapse occurs when the generator maps all (or most) latent codes to a small region of data space, producing only a few types of outputs rather than the full diversity of the data distribution.
Example 5 (Mode Collapse in 1D).
Consider (two well-separated modes). If the generator parameterisation is (a single learnable scalar, ignoring ), then , a point mass. The generator can match one mode ( or ) but never both.
Even with a more expressive generator, mode collapse can occur dynamically: the generator βchasesβ the discriminator's weak points, cycling between modes without ever covering all of them.
Why GANs Are Hard to Train
Key Idea.
GAN training is a minimax (saddle-point) problem, not a minimisation problem. Standard gradient descent finds minima, but there is no guarantee it finds saddle points. This is the root cause of GAN training instability.
Example 6 (Failure of Simultaneous GD on a Bilinear Game).
Consider the simplest minimax game: , with Nash equilibrium at . Simultaneous gradient descent gives updates: This is a rotation: orbits the origin with increasing radius, diverging from the equilibrium! The eigenvalues of the update matrix have magnitude , confirming divergence.
The main failure modes in practice are:
Mode collapse (Section Mode Collapse): generator produces limited variety.
Training oscillation: generator and discriminator losses oscillate without converging.
Discriminator domination: becomes too strong too early, providing no useful gradient to .
Generator collapse: produces nonsensical outputs (noise, artefacts).
Remark 6 (Mitigation Strategies).
Numerous techniques have been proposed to stabilise GAN training:
Wasserstein distance instead of JSD (the next chapter).
Gradient penalty [5]: regularise the discriminator's gradient norm.
Spectral normalisation [4]: control the Lipschitz constant.
Progressive growing: start at low resolution and gradually increase.
Two-timescale updates: use different learning rates for and .
The Wasserstein GAN, discussed in the next chapter, addresses the most fundamental issue: the JSD's behaviour when supports are disjoint.
Evaluating GANs
Since GANs do not provide a tractable density , we cannot evaluate them via log-likelihood. Several surrogate metrics have been developed.
Definition 7 (FrΓ©chet Inception Distance (FID)).
The FID [6] compares the statistics of real and generated images in the feature space of a pretrained Inception network. Let and be the means and covariances of Inception features for real and generated images. Then: (FID) Lower FID = better quality. FID iff the feature distributions are identical.
Remark 7 (FID Is a Wasserstein Distance).
The FID is exactly the squared -Wasserstein distance between two Gaussians fitted to the Inception features. This connects GAN evaluation to the optimal transport theory of Chapter 4.
Definition 8 (Inception Score (IS)).
The Inception Score [7] measures: (IS) where is the Inception classifier's label distribution. Higher IS = samples are individually distinctive (low entropy ) and collectively diverse (high entropy ).
Caution.
Both FID and IS have significant limitations:
They depend on a pretrained Inception network (ImageNet features may not suit all domains).
IS does not compare to real data, only measures internal diversity.
Neither captures fine-grained artefacts that humans notice.
FID can be gamed by memorising training data.
Use multiple metrics and always include visual inspection when evaluating generative models.
Remark 8 (Precision and Recall for Generative Models).
KynkÀÀnniemi et al. proposed separating GAN quality into precision (are generated samples realistic?) and recall (does the generator cover all modes of the data?). A model with high precision but low recall exhibits mode collapse; a model with low precision but high recall produces diverse but poor-quality samples.
The GAN Landscape
Since the original 2014 paper, GANs have evolved rapidly:
Remark 9 (Conditional GANs).
All the above GANs generate images unconditionally. Conditional GANs [8] augment both and with a conditioning variable (e.g. a class label): This enables controllable generation: βgenerate a catβ, βgenerate a house with this floor planβ, etc. Conditional GANs underpin pix2pix (image-to-image translation) and many other applications discussed in later chapters.
Chapter Summary
A GAN consists of a generator and discriminator playing a minimax game.
The optimal discriminator is a density ratio estimator; substituting it into the objective reveals that GANs minimise the JensenβShannon divergence.
The -GAN framework generalises GANs to any -divergence via the Fenchel conjugate trick.
In practice, the non-saturating loss replaces the minimax generator loss to avoid vanishing gradients.
GANs suffer from mode collapse, training oscillation, and convergence difficulties inherent to minimax optimisation.
Evaluation relies on surrogate metrics (FID, IS) since GANs have no tractable likelihood.
The JSD's saturation when supports are disjoint motivates the Wasserstein GAN (next chapter).
Exercises
Exercise 1 (Optimal Discriminator Derivation).
Exercise 2 (GAN Objective and JSD).
After substituting into the objective, show that , and conclude with equality iff .
Exercise 3 (Non-Saturating Loss Analysis).
- (a)
Show that when , the gradient of the saturating loss is near zero.
- (b)
Show that the NS loss has strong gradients in the same regime.
- (c)
Show that under the optimal discriminator, the NS generator loss corresponds to minimising .
- (d)
Explain why the reverse KL term can lead to mode collapse.
Exercise 4 (Mode Collapse Scenario).
Let and (constant).
- (a)
Show the generator can never capture both modes.
- (b)
Describe qualitatively how evolves during training.
- (c)
Suggest a generator architecture that can capture both modes.
Exercise 5 (-Divergence Variants).
- (a)
Show that gives the KL divergence. Compute its Fenchel conjugate .
- (b)
Show that gives the reverse KL. Compute .
- (c)
For the Pearson divergence (), show that .
- (d)
Write the resulting GAN training objective for each case.
Exercise 6 (GAN Training Dynamics).
- (a)
Explain why discriminator steps per generator step can be beneficial. What happens if is too large?
- (b)
If is optimal at every step, show minimises .
- (c)
For the bilinear game , show that simultaneous gradient descent diverges from the Nash equilibrium .
Exercise 7 (FID Computation).
Suppose Inception features for real images have , , and for generated images , .
- (a)
Compute .
- (b)
Compute and the trace term in the FID formula.
- (c)
What is the total FID?
- (d)
How does FID change if we double all variances?
Exercise 8 (Density Ratio Interpretation).
- (a)
Show that the optimal discriminator satisfies .
- (b)
Explain how this means training a discriminator is equivalent to estimating the log-density ratio.
- (c)
If we had access to the exact density ratio, how could we compute the KL divergence without knowing either density explicitly?
Exercise 9 (Comparing GAN and VAE).
- (a)
List three advantages of GANs over VAEs.
- (b)
List three advantages of VAEs over GANs.
- (c)
A VAE generates blurry images; a GAN generates sharp but less diverse images. Explain this in terms of the divergences each model minimises (forward KL vs. reverse KL / JSD).
- (d)
Can the two approaches be combined? Describe one hybrid model.
Exercise 10 (Conditional GAN Objective).
- (a)
Write the minimax objective for a conditional GAN with class label .
- (b)
Derive the optimal conditional discriminator .
- (c)
Show that the conditional GAN minimises for each .
Exercise 11 (JSD Saturation and WGAN Motivation).
Let and for small .
- (a)
For what values of are and βessentially disjointβ (overlap )?
- (b)
Show that when and are disjoint, regardless of the distance between them.
- (c)
In contrast, show that the Wasserstein distance varies smoothly with .
- (d)
Explain why this makes JSD unsuitable as a GAN training signal when and live on low-dimensional manifolds.
Exercise 12 (Inception Score from Scratch).
A toy βInception classifierβ has 3 classes and produces the following label distributions for 4 generated images:
| Image | |||
| 1 | 0.9 | 0.05 | 0.05 |
| 2 | 0.1 | 0.8 | 0.1 |
| 3 | 0.9 | 0.05 | 0.05 |
| 4 | 0.05 | 0.1 | 0.85 |
- (a)
Compute the marginal .
- (b)
Compute for each image.
- (c)
Compute the Inception Score.
- (d)
What IS would you get if all images had the same label distribution ? Explain why.
References
-
Generative Adversarial Networks
BibTeX
@inproceedings{goodfellow2014generative, title={Generative Adversarial Networks}, author={Goodfellow, Ian and Pouget-Abadie, Jean and Mirza, Mehdi and Xu, Bing and Warde-Farley, David and Ozair, Sherjil and Courville, Aaron and Bengio, Yoshua}, booktitle={Advances in Neural Information Processing Systems}, volume={27}, year={2014} }Conference paper
-
A Style-Based Generator Architecture for Generative Adversarial Networks
BibTeX
@inproceedings{karras2019style, title={A Style-Based Generator Architecture for Generative Adversarial Networks}, author={Karras, Tero and Laine, Samuli and Aila, Timo}, booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition}, pages={4401--4410}, year={2019} }Conference paper
-
Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks
BibTeX
@inproceedings{radford2015unsupervised, title={Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks}, author={Radford, Alec and Metz, Luke and Chintala, Soumith}, booktitle={International Conference on Learning Representations}, year={2016} }Conference paper
-
Spectral normalization for generative adversarial networks
BibTeX
@inproceedings{miyato2018spectral, title={Spectral normalization for generative adversarial networks}, author={Miyato, Takeru and Kataoka, Toshiki and Koyama, Masanori and Yoshida, Yuichi}, booktitle={Proceedings of the International Conference on Learning Representations}, year={2018}, url={https://openreview.net/forum?id=B1QRgziT-} }Conference paper
-
Improved training of Wasserstein GANs
BibTeX
@inproceedings{gulrajani2017improved, title={Improved training of Wasserstein GANs}, author={Gulrajani, Ishaan and Ahmed, Faruk and Arjovsky, Martin and Dumoulin, Vincent and Courville, Aaron C}, booktitle={Advances in neural information processing systems}, pages={5767--5777}, year={2017} }Conference paper
-
Gans trained by a two time-scale update rule converge to a local nash equilibrium
BibTeX
@inproceedings{heusel2017gans, title={Gans trained by a two time-scale update rule converge to a local nash equilibrium}, author={Heusel, Martin and Ramsauer, Hubertus and Unterthiner, Thomas and Nessler, Bernhard and Hochreiter, Sepp}, booktitle={Advances in Neural Information Processing Systems}, pages={6626--6637}, year={2017} }Conference paper
-
Improved techniques for training GANs
BibTeX
@inproceedings{salimans2016improved, title={Improved techniques for training GANs}, author={Salimans, Tim and Goodfellow, Ian and Zaremba, Wojciech and Cheung, Vicki and Radford, Alec and Chen, Xi}, booktitle={Advances in Neural Information Processing Systems}, pages={2234--2242}, year={2016} }Conference paper
-
Conditional Generative Adversarial Nets
BibTeX
@misc{mirza2014conditional, title={Conditional Generative Adversarial Nets}, author={Mehdi Mirza and Simon Osindero}, year={2014}, eprint={1411.1784}, archivePrefix={arXiv}, primaryClass={cs.LG} }Reference
-
f-GAN: Training generative neural samplers using variational divergence minimization
BibTeX
@inproceedings{nowozin2016fgan, title={f-GAN: Training generative neural samplers using variational divergence minimization}, author={Nowozin, Sebastian and Cseke, Botond and Tomioka, Ryota}, booktitle={Advances in Neural Information Processing Systems}, volume={29}, year={2016} }Conference paper