What a GAN is
A generative adversarial network learns to make new samples that look like its training data without ever writing down their probability. Two networks are trained against each other: a generator turns a vector of random numbers into an image, and a discriminator looks at an image and outputs its probability that the image is real. is rewarded for telling real images from generated ones, for fooling .
The networks on this page are a small DCGAN trained here on the 60,000 MNIST training digits, with the labels unused. The generator turns 64 standard normal numbers into a 28 × 28 digit through three transposed convolutions (533,889 parameters); the discriminator is a three-layer convolutional network (138,561). Both were trained with Adam at learning rate and , batches of 64, for 15 epochs (14,055 alternating steps), seed 0.
The adversarial game
Goodfellow et al. wrote the game as one value that maximises and minimises:
For a fixed generator the best discriminator is , and against it the generator minimises , which is smallest when the generated distribution equals the data's. There everywhere and the discriminator's loss, , is : the dotted line on the Losses chart.
Early in training rejects fakes easily, is flat and gives almost no gradient, so the generator here minimises instead, the non-saturating loss the same paper proposed. Over the last 500 steps the mean on real digits is 0.637 and 0.364: neither network wins.
The generator: transposed convolutions
A convolution with stride 2 halves a map; a transposed convolution with stride 2 doubles it. Each input pixel stamps a learned kernel, scaled by its value, onto the output, two pixels along from its neighbour's stamp, and where stamps overlap they add. With kernel , stride and padding , a side of pixels becomes
so the generator goes from 1 to 7 (kernel 7), then 7 → 14 → 28 (kernel 4, stride 2, padding 1), with 128 and 64 channels, batch norm and ReLU between, and a tanh at the end that puts the pixels in . The One output pixel panel lists the stamps that cover the selected pixel, each a 4 × 4 kernel already summed over the 64 channels, and adds them and the bias up to the recorded value before the tanh. Uneven overlap of the stamps is a known cause of checkerboard patterns in generated images.
The discriminator and DCGAN's recipe
The discriminator is an ordinary convolutional network like the CNN tab's, with one output. Two stride-2 convolutions with leaky ReLU (slope 0.2) take the digit to 64 × 14 × 14 and then 128 × 7 × 7, and a 7 × 7 convolution gives one logit, so . Radford, Metz and Chintala found a recipe that made convolutional GANs train reliably, and this page follows it: strided convolutions instead of pooling, batch norm in the generator, ReLU in the generator and leaky ReLU in the discriminator, a tanh output, Adam at with , and weights drawn from . The panel headed The discriminator after step shows 's scores on 10,000 real test digits and 10,000 generated ones at each checkpoint.
One alternating update
Each training step updates the two networks in turn. First the discriminator takes one Adam step on a batch of 64 real digits and 64 fakes, pushing up and down. Then the generator takes one step on the same noise, scored by the updated discriminator, pushing back up. The One alternating update panel records this at steps 200 and 7,000: the scores before, after 's step and after 's, and the fakes before and after 's step with the change between them, which is small, since one step moves the generator only a little. The recorded step matches PyTorch's binary cross-entropy and Adam to .
Mode collapse
A generator can lower its loss by producing only the few kinds of image that currently fool the discriminator, a failure called mode collapse. The Mode collapse? panel has the CNN tab's trained network read 10,000 samples at each checkpoint. At step 0 every sample is read as a 4. At step 50, 6,492 of them read as 8. By the last checkpoint every digit has more than 3 % of the samples and the most common, 5, has 1,495, against about 1,000 per digit in the real test set. The CNN is confident (p > 0.9) on 63.6 % of the final samples, and their mean pairwise distance, 10.18, is close to the real digits' 9.90. Salimans et al. described mode collapse and several tricks against it.
A walk in noise space, and where GANs came from
The generator is a smooth function of , so a straight line between two noise vectors, , gives a smooth path of images: the walk panel goes in eleven steps from a sample the CNN reads as 9 to one it reads as 6.
Schmidhuber's predictability minimisation (1992) had already set networks against each other, one learning codes that the others could not predict. Goodfellow et al. (2014) introduced the GAN game, and DCGAN (2015) made it work with convolutions and found meaningful directions in . Later GANs, with Wasserstein losses, progressive growing and style-based generators, reached photographic faces, until diffusion models, which train on a plain regression loss with no adversary, took over image generation: the Diffusion Explainer starts there.
Papers
- Generative Adversarial Networks The GAN game, its optimal discriminator and the non-saturating generator loss.
- Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks DCGAN: the convolutional architecture and training recipe this tab follows.
- Improved Techniques for Training GANs Mode collapse and tricks against it, such as feature matching and minibatch discrimination.
- Towards Principled Methods for Training Generative Adversarial Networks Why the original generator loss saturates once the discriminator is good, and why training is unstable.
- Learning Factorial Codes by Predictability Minimization An earlier adversarial setup: code units trained against predictors that try to guess them from each other.
- Deconvolution and Checkerboard Artifacts How overlapping stamps of transposed convolutions leave checkerboard patterns, and how to avoid them.
- NIPS 2016 Tutorial: Generative Adversarial Networks A tutorial survey of GANs: the game, its losses, DCGAN and the open problems.