GANs as a two-player game
A generator made from noise, a discriminator that classifies, and the minimax game between them: the optimal discriminator, the link to the Jensen–Shannon divergence, the non-saturating loss, and why training still breaks.
Drop the likelihood
The VAE learns by maximizing a bound on the likelihood, and with a Gaussian decoder that bound contains a squared error (see Latent variables and the ELBO). A squared error is minimized by an average. When one code fits many sharp images, the decoder outputs their mean — a blur. A weight on the KL or a discrete code (see β-VAE and VQ-VAE) changes the code, not this term.
A generative adversarial network, or GAN (Goodfellow et al., 2014), never writes a likelihood down. Its generator is a network that turns noise into a sample, the same noise-transforming idea as in What generative modeling is:
The samples have some distribution , but nothing in the model computes its density. Such a model is implicit: we can sample from it, but we cannot ask how likely a given is.
Without a likelihood, the generator needs some other signal that its samples are wrong. A GAN gets that signal from a second network.
The discriminator and the game
The second network, the discriminator , is a binary classifier. It receives either a real point , labelled , or a generated one , labelled , and outputs the probability that its input is real. A sigmoid at the end keeps inside .
The generator never sees the data. Everything it learns arrives as gradients that flow back through the discriminator.
A classifier is trained with binary cross-entropy, . For a real point it is , for a fake . Average over real and fake points in equal numbers, so each kind has weight , and flip the sign. The result is half of the value function, and the factor does not move the maximizer:
The discriminator maximizes , which is just training it as a classifier: high output on data, low on fakes. The generator minimizes . It can only touch the second term, and that term falls when rises — when fakes pass for real. Together they play a two-player minimax game:
The players take turns
Nobody solves the inner maximization exactly. The two players take turns, each on a minibatch of size (Goodfellow et al., 2014, Algorithm 1):
Discriminator step. Fix . Take real points and fakes made from fresh noise, and take a gradient ascent step on the minibatch estimate of with respect to .
Generator step. Fix . Draw new noise and take a gradient descent step with respect to on
Only the second term of appears here. The first contains real data and alone, so its gradient with respect to is zero.
The discriminator step may run times per generator step; the original paper used .
On images, the architecture decides whether this loop works at all. DCGAN (Radford, Metz and Chintala, 2016) set the defaults: strided convolutions instead of pooling, transposed convolutions to upsample in the generator, batch normalization in both networks, a Tanh at the generator's output for images scaled to , LeakyReLU with slope in the discriminator, and Adam with learning rate and . None of these is a theorem. Most of them keep gradients dense, and the generator lives on nothing but gradients.
The best discriminator
What would a perfect discriminator do? Fix the generator, and let be any function of , not a particular network. A fake is distributed as ; suppose for now that has a density , and write for the density of . Then both expectations become one integral:
The value can be chosen separately at every , so maximize the integrand pointwise. With , and ,
The single maximum gives the optimal discriminator:
It is close to where only data live, close to where only fakes live, and exactly where the two densities are equal. A discriminator trained to convergence is a density-ratio estimator: .
Jensen–Shannon in disguise
Now put back into . Since , the best value the discriminator can reach is
The denominators almost form the mixture from Entropy, cross-entropy and KL, whose density is . Divide each denominator by and pay for it: . Each expectation becomes a KL divergence to :
That is the promise from Entropy, cross-entropy and KL, kept. Against an optimal discriminator, the generator minimizes the Jensen–Shannon divergence. JS is zero only when , so the game's only global optimum is a generator that reproduces the data, and there everywhere: the discriminator can do no better than a coin. For discrete distributions the same derivation goes through with sums.
The same argument covers a conditional GAN (Mirza and Osindero, 2014). Both networks receive a label : the generator produces and the discriminator judges pairs . This is the original game played on pairs , so its optimum matches the joint distribution of images and labels.
The non-saturating loss
Early in training the fakes are poor, and the discriminator rejects them confidently: . Look at the generator's loss through the logit , where and . That logit is where the gradient enters the networks:
The minimax loss saturates: its gradient vanishes exactly when the generator is worst. The original paper already proposed a fix. Train the generator to maximize instead, that is, minimize the non-saturating loss
Both losses fall as grows, so they push fakes the same way, and with an optimal discriminator they share the fixed point . But the new loss is no longer the minimax game. With computed for the current generator and held fixed, its expected gradient equals the gradient of (Arjovsky and Bottou, 2017). The reverse KL in it punishes fake-looking samples hard and missing parts of the data only lightly.
In code, the non-saturating loss is the binary cross-entropy of the fakes against the label "real". Compute every term from logits: and . Taking the sigmoid first fails wherever the discriminator is confident. In 32-bit floats rounds to exactly once exceeds about 17, so the discriminator's fake term becomes where softplus gives about . At the other end underflows to once falls below about , and the generator's becomes . PyTorch has the stable form:
bce = F.binary_cross_entropy_with_logits
real, fake = D(x), D(G(z).detach()) # discriminator step
loss_D = bce(real, torch.ones_like(real)) + bce(fake, torch.zeros_like(fake))
fake = D(G(z)) # generator step
loss_G = bce(fake, torch.ones_like(fake)) # the label "real"Mode collapse and instability
The theory assumed a discriminator that reaches its optimum after every generator step and two networks that can represent any function. Real GANs are two finite networks trained a few steps at a time, and two failures dominate.
Mode collapse. The generator maps most of its noise to a few outputs that the current discriminator accepts — a model of digits that draws only crisp ones and sevens. The discriminator judges each sample on its own, so its gradient points the same way for all similar samples, and nothing asks the outputs to differ (Salimans et al., 2016). Against a fixed discriminator, the best generator would put all its mass on the single point the discriminator likes most. When the discriminator learns to reject that mode, the generator moves to another, and the two can chase each other indefinitely. The reverse KL of the non-saturating loss adds to this: dropping a mode costs it little.
Instability. Gradient steps that take turns in a minimax game need not converge; they can circle around the equilibrium. And the players must be balanced. A discriminator that is too strong saturates, and the generator's gradients shrink; one that is too weak points the generator in poor directions. The knobs are , the two learning rates and the capacity of each network. The two losses move against each other and say little about sample quality, so the samples themselves have to be watched.
The usual remedies each attack one part of the mechanism. Minibatch discrimination lets the discriminator compare samples within a batch and spot a collapsed one. A replay buffer of old fakes keeps it from forgetting modes the generator visited before. One-sided label smoothing trains it toward instead of on real data, so it never becomes fully confident.
When JS stops helping
One problem survives all of these, and the JS identity explains it. In Entropy, cross-entropy and KL, reached its maximum, , exactly when the two distributions do not overlap. Then on the data, on the fakes, and
wherever the generator sits, as long as it stays off the data. Slide its samples from far away to just next to the data: does not move.
The gradient fares no better. Backpropagation sends the generator at the fakes, and with disjoint supports there is an optimal discriminator that equals on a whole neighbourhood of them. Its gradient there is zero, and the generator learns nothing (Arjovsky and Bottou, 2017). The non-saturating loss does not cure this; with a near-optimal discriminator its updates become unstable instead.
This is the typical case, not an exotic one. A generator maps a -dimensional into image space. When is smaller than the number of pixel values, all its samples lie on a surface of dimension at most . Then has no density at all, and the assumption of the optimal-discriminator block fails. Natural images are believed to concentrate near a low-dimensional surface too. After an arbitrarily small shift, two such surfaces meet, if at all, in a set of even lower dimension that carries no probability mass, the way two curves in three-dimensional space miss each other. A perfect discriminator exists, and JS sits at however close the generator gets.
What the generator needs is a distance that shrinks smoothly as approaches , even when they do not overlap — one that measures how far the mass has to move. That is the Wasserstein distance, the subject of the next topic, Wasserstein GANs and Lipschitz constraints.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.