Generative Adversarial Networks
A generative adversarial network (GAN; Goodfellow et al., 2014) trains two networks with opposite goals. The generator maps a random latent vector , usually drawn from , to an image . The discriminator is a binary classifier that looks at one image and outputs a probability. tries to synthesize fake images that fool the best , and tries to identify the fakes. In the lecture’s convention outputs the probability that its input is fake, and the objective is
where is a real image from the training data. The original paper lets output the probability that the input is real. Replacing by gives the standard form
The inner makes the best classifier of real against fake for the current . The outer then moves against that best . Training alternates stochastic gradient steps. One step improves on a batch of real and generated images, and the next improves through the gradient of . From ‘s point of view, is a learned loss function, a loss that is trained from data along with . The game is solved when the generated distribution equals the data distribution, and then outputs for every image. Sampling takes one forward pass, . Moving along a line moves smoothly through realistic images. A conditional GAN feeds an input image to both networks and lets judge (input, output) pairs, as in 8.4. Pix2Pix and 8.5. CycleGAN. Known failure modes are unstable training and mode collapse, where produces only a few kinds of output.
Two Conventions for the Discriminator Output
The lecture slides show answering “fake (0.9)” for a generated image and “real (0.1)” for a real one. Their is therefore the probability of fake. Most papers and libraries use the probability of real. Under the swap the two objectives are identical, but the terms trade places. The lecture also writes the generator input as (an input image) and the real image as when it moves to image-to-image translation. Before using a formula, check which output reports.
Intuition
Hand-designed losses fail for image synthesis. An L2 loss to a target image averages all plausible answers. Colorization with L2 gives brownish, desaturated images, and a one-step denoiser trained with L2 on pure noise outputs the mean digit. A good loss would say “this does not look like a real photo.” Nobody can write that rule by hand, but a classifier can learn it from examples. A GAN trains such a classifier on real and generated images and trains the generator to beat it. The word adversarial refers to these opposite goals.
The order of the min and the max matters. is judged against the best discriminator for its current outputs, so any visible flaw in the fakes becomes a feature that learns to detect. As removes one flaw, looks for subtler ones. The loss therefore keeps adapting to the generator’s current mistakes. The figure shows one round of the game in the lecture’s convention.
\usepackage{tikz}
\usepackage{amsmath}
\begin{document}
\definecolor{hot}{HTML}{D9483B}
\definecolor{cold}{HTML}{2F80ED}
\definecolor{mid}{HTML}{8A8A8A}
\begin{tikzpicture}[>=latex,
net/.style={draw,thick,minimum width=1.4cm,minimum height=1.0cm,align=center},
img/.style={draw=mid,minimum width=1.5cm,minimum height=1.0cm,align=center}]
\node (z) at (0,0) {$z \sim \mathcal{N}(0, I)$};
\node[net,cold,fill=cold!10] (G) at (2.4,0) {$G$};
\node[img] (fake) at (4.6,0) {fake\\$G(z)$};
\node[img] (real) at (4.6,-2.0) {real $x$\\from data};
\node[net,hot,fill=hot!10] (D) at (7.2,-1.0) {$D$};
\node[align=left] (out) at (10.0,-1.0) {$D(\cdot) =$ probability\\the input is fake};
\draw[->,thick] (z) -- (G);
\draw[->,thick] (G) -- (fake);
\draw[->,thick] (fake.east) -- (D.west);
\draw[->,thick] (real.east) -- (D.west);
\draw[->,thick] (D) -- (out);
\draw[->,dashed,cold,thick] (D.north) to[out=90,in=90,looseness=0.9] node[pos=0.5,above=2pt,align=center,fill=white,inner sep=1pt] {gradient of $\log D(G(z))$ flows back through $D$:\\$G$ moves to fool $D$} (G.north);
\node[hot,align=center] at (9.9,-2.1) {$D$ is trained to label\\fakes $1$ and reals $0$};
\end{tikzpicture}
\end{document}Properties
(Optimal Discriminator) For a fixed generator, the best discriminator compares the two densities at each image.
Let be the density of real images and the density of generated ones. In the standard convention, maximizes . Each can be optimized on its own. Setting the derivative to zero, with and , gives
In the lecture’s convention the best discriminator is . When , everywhere and the value of the game is . For any other the value with the best is larger, so the generator’s best move is to match the data distribution. In practice is never optimal, since it gets only a few gradient steps for each generator step.
(Learned Loss) The generator’s training signal is the gradient of the discriminator’s verdict, which penalizes any flaw that can detect.
In the generator step, is held fixed and the gradient of (lecture convention) flows back through into by the chain rule. Blur is easy to detect, because real photos are sharp, so is pushed toward sharp detail. An L2 loss has no such pressure and rewards the blurry average of plausible answers. Image-to-image models such as 8.4. Pix2Pix combine the two. The GAN term asks for realistic texture, and an L1 term keeps the output close to the paired target.
(Walking on the Manifold) Straight lines in latent space map to smooth paths of realistic images.
The latent space is a simple Gaussian, and maps it onto the natural image manifold. Interpolating and decoding each gives a sequence of realistic images that changes gradually from to (BigGAN, Brock et al., 2018). Linear interpolation in pixel space instead gives a cross-dissolve with ghosting, as discussed in 3.5. Image Morphing.
(Alternating Updates) Training switches between a classification step for and a fooling step for , and neither loss alone shows progress.
The discriminator step draws a batch of real images and a batch of fakes from the current . It trains as a binary classifier with the log loss of logistic regression. In the lecture’s convention the labels are for fake and for real. The generator step draws new latent vectors and updates only , with frozen. Each loss depends on the other network’s current weights, so the loss values oscillate and do not show whether the images are improving. Practitioners judge training by looking at samples.
(Failure Modes) GAN training can oscillate instead of converging, and the generator can cover only part of the data.
Gradient steps on a two-player game do not always settle. Each network keeps reacting to the other’s last update, and the parameters can cycle around a solution without reaching it. In mode collapse, maps many latent vectors to a few outputs that the current accepts. then learns to reject those outputs, and moves to a different small set. Flow and diffusion models train by plain regression and do not have this two-player instability. They pay for it with many network evaluations per sample instead of one.
(GANs and Texture Statistics) The discriminator may learn the statistics that matter for natural images instead of having them chosen by hand.
Parametric texture synthesis matches hand-picked statistics, such as filter-response histograms or Gram matrices of CNN features, as in 8.1. Parametric Texture Synthesis. A GAN discriminator learns its own features for telling real from fake. The lecture presents as a conjecture that GANs might be learning the “right” features to match for natural images.
Examples
(Easy) Reading the Discriminator's Outputs
In the lecture’s example, answers “fake (0.9)” for a generated image and “real (0.1)” for a real photo . Use the lecture’s convention, where outputs the probability of fake.
Evaluate the objective for this pair. How does it change when improves so that ? What are the numbers in the standard convention? The objective is . Its maximum is , reached when is certain and correct, so this is doing well. The generator minimizes the first term. If improves until , the objective drops to . At the equilibrium both outputs are and the objective is . In the standard convention reports for the fake and for the real image. The objective has the same value, .
(Medium) The Best Discriminator for One Pixel
Consider a world of one-pixel images. After normalization, real brightness values follow . The current generator is too bright, and its outputs follow . Use the standard convention, where outputs the probability of real.
Find the best discriminator and evaluate it at . Compute the value of the game with and say which way the generator should move. The log-ratio of the two Gaussian densities is . Hence a logistic function with its decision boundary at , halfway between the two means. It gives , and . Integrating numerically, the value of the game with is , which is above the minimum . The generator wants its outputs labeled real, and rises as falls. The gradient through therefore pushes toward darker outputs, which moves toward . At the best discriminator is everywhere, and the value reaches .
See Also
- 8.4. Pix2Pix, a conditional GAN for paired image-to-image translation.
- 8.5. CycleGAN, which trains two GANs with a cycle-consistency loss on unpaired data.
- 10.3.2. Flow Matching, a generator trained by regression that needs many steps per sample.
- 6.4. U-Net, the generator architecture in pix2pix.
- 8.3. Image Colorization, where a hand-designed L2 loss gives desaturated results.