Skip to content
BIASED

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:

z∼p(z)=N(0,I),x=Gθ(z).z \sim p(z) = \mathcal N(0, I), \qquad x = G_\theta(z).

The samples Gθ(z)G_\theta(z) have some distribution QθQ_\theta, 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 xx 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 Dψ(x)D_\psi(x), is a binary classifier. It receives either a real point x∼Px \sim P, labelled y=1y = 1, or a generated one Gθ(z)G_\theta(z), labelled y=0y = 0, and outputs the probability that its input is real. A sigmoid at the end keeps Dψ(x)D_\psi(x) inside (0,1)(0, 1).

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, −[yln⁡y^+(1−y)ln⁡(1−y^)]-\big[y\ln\hat y + (1 - y)\ln(1 - \hat y)\big]. For a real point it is −ln⁡Dψ(x)-\ln D_\psi(x), for a fake −ln⁡(1−Dψ(Gθ(z)))-\ln\big(1 - D_\psi(G_\theta(z))\big). Average over real and fake points in equal numbers, so each kind has weight 12\tfrac12, and flip the sign. The result is half of the value function, and the factor 12\tfrac12 does not move the maximizer:

V(θ,ψ)=Ex∼P[ln⁡Dψ(x)]+Ez∼p(z)[ln⁡(1−Dψ(Gθ(z)))].V(\theta, \psi) = \mathbb E_{x \sim P}\big[\ln D_\psi(x)\big] + \mathbb E_{z \sim p(z)}\Big[\ln\big(1 - D_\psi(G_\theta(z))\big)\Big].

The discriminator maximizes VV, which is just training it as a classifier: high output on data, low on fakes. The generator minimizes VV. It can only touch the second term, and that term falls when Dψ(Gθ(z))D_\psi(G_\theta(z)) rises — when fakes pass for real. Together they play a two-player minimax game:

min⁡θmax⁡ψV(θ,ψ).\min_\theta \max_\psi V(\theta, \psi).

The players take turns

Nobody solves the inner maximization exactly. The two players take turns, each on a minibatch of size mm (Goodfellow et al., 2014, Algorithm 1):

Discriminator step. Fix θ\theta. Take mm real points and mm fakes made from fresh noise, and take a gradient ascent step on the minibatch estimate of VV with respect to ψ\psi.

Generator step. Fix ψ\psi. Draw new noise and take a gradient descent step with respect to θ\theta on

1m∑i=1mln⁡(1−Dψ(Gθ(z(i)))).\frac1m \sum_{i=1}^{m} \ln\Big(1 - D_\psi\big(G_\theta(z^{(i)})\big)\Big).

Only the second term of VV appears here. The first contains real data and ψ\psi alone, so its gradient with respect to θ\theta is zero.

The discriminator step may run kk times per generator step; the original paper used k=1k = 1.

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 [−1,1][-1, 1], LeakyReLU with slope 0.20.2 in the discriminator, and Adam with learning rate 2⋅10−42 \cdot 10^{-4} and β1=0.5\beta_1 = 0.5. 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 DD be any function of xx, not a particular network. A fake x=Gθ(z)x = G_\theta(z) is distributed as QθQ_\theta; suppose for now that QθQ_\theta has a density qθq_\theta, and write pdatap_{\text{data}} for the density of PP. Then both expectations become one integral:

V(θ,D)=∫[ pdata(x)ln⁡D(x)+qθ(x)ln⁡(1−D(x))] dx.V(\theta, D) = \int \Big[\, p_{\text{data}}(x)\ln D(x) + q_\theta(x)\ln\big(1 - D(x)\big) \Big]\, dx .

The value D(x)D(x) can be chosen separately at every xx, so maximize the integrand pointwise. With a=pdata(x)a = p_{\text{data}}(x), b=qθ(x)b = q_\theta(x) and s=D(x)s = D(x),

f(s)=aln⁡s+bln⁡(1−s),f′(s)=as−b1−s=0,f′′(s)<0.f(s) = a\ln s + b\ln(1 - s), \qquad f'(s) = \frac{a}{s} - \frac{b}{1 - s} = 0, \qquad f''(s) < 0 .

The single maximum gives the optimal discriminator:

D∗(x)=pdata(x)pdata(x)+qθ(x).D^*(x) = \frac{p_{\text{data}}(x)}{p_{\text{data}}(x) + q_\theta(x)} .

It is close to 11 where only data live, close to 00 where only fakes live, and exactly 12\tfrac12 where the two densities are equal. A discriminator trained to convergence is a density-ratio estimator: D∗/(1−D∗)=pdata/qθD^*/(1 - D^*) = p_{\text{data}}/q_\theta.

Jensen–Shannon in disguise

Now put D∗D^* back into VV. Since 1−D∗=qθ/(pdata+qθ)1 - D^* = q_\theta/(p_{\text{data}} + q_\theta), the best value the discriminator can reach is

C(θ)=max⁡DV(θ,D)=Ex∼Pln⁡pdata(x)pdata(x)+qθ(x)+Ex∼Qθln⁡qθ(x)pdata(x)+qθ(x).C(\theta) = \max_D V(\theta, D) = \mathbb E_{x \sim P}\ln\frac{p_{\text{data}}(x)}{p_{\text{data}}(x) + q_\theta(x)} + \mathbb E_{x \sim Q_\theta}\ln\frac{q_\theta(x)}{p_{\text{data}}(x) + q_\theta(x)} .

The denominators almost form the mixture M=12(P+Qθ)M = \tfrac12(P + Q_\theta) from Entropy, cross-entropy and KL, whose density is m=12(pdata+qθ)m = \tfrac12(p_{\text{data}} + q_\theta). Divide each denominator by 22 and pay ln⁡2\ln 2 for it: ln⁡pp+q=ln⁡pm−ln⁡2\ln\frac{p}{p + q} = \ln\frac{p}{m} - \ln 2. Each expectation becomes a KL divergence to MM:

C(θ)=DKL(P ∥ M)+DKL(Qθ ∥ M)−2ln⁡2=2 DJS(P ∥ Qθ)−2ln⁡2.C(\theta) = D_{\mathrm{KL}}(P \,\|\, M) + D_{\mathrm{KL}}(Q_\theta \,\|\, M) - 2\ln 2 = 2\,D_{\mathrm{JS}}(P \,\|\, Q_\theta) - 2\ln 2 .

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 Qθ=PQ_\theta = P, so the game's only global optimum is a generator that reproduces the data, and there D∗=12D^* = \tfrac12 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 cc: the generator produces Gθ(z,c)G_\theta(z, c) and the discriminator judges pairs Dψ(x,c)D_\psi(x, c). This is the original game played on pairs (x,c)(x, c), 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: d=Dψ(Gθ(z))≈0d = D_\psi(G_\theta(z)) \approx 0. Look at the generator's loss through the logit ℓ\ell, where d=σ(ℓ)d = \sigma(\ell) and ∂d/∂ℓ=d(1−d)\partial d/\partial \ell = d(1 - d). That logit is where the gradient enters the networks:

∂∂ℓln⁡(1−σ(ℓ))=−d  →  0.\frac{\partial}{\partial \ell}\ln\big(1 - \sigma(\ell)\big) = -d \;\to\; 0 .

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 ln⁡Dψ(Gθ(z))\ln D_\psi(G_\theta(z)) instead, that is, minimize the non-saturating loss

LG=−Ezln⁡Dψ(Gθ(z)),∂∂ℓ(−ln⁡σ(ℓ))=−(1−d)  →  −1.\mathcal L_G = -\mathbb E_{z}\ln D_\psi\big(G_\theta(z)\big), \qquad \frac{\partial}{\partial \ell}\big(-\ln\sigma(\ell)\big) = -(1 - d) \;\to\; -1 .

Both losses fall as dd grows, so they push fakes the same way, and with an optimal discriminator they share the fixed point Qθ=PQ_\theta = P. But the new loss is no longer the minimax game. With D=D∗D = D^* computed for the current generator and held fixed, its expected gradient equals the gradient of DKL(Qθ ∥ P)−2 DJS(P ∥ Qθ)D_{\mathrm{KL}}(Q_\theta \,\|\, P) - 2\,D_{\mathrm{JS}}(P \,\|\, Q_\theta) (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: ln⁡σ(ℓ)=−softplus⁡(−ℓ)\ln\sigma(\ell) = -\operatorname{softplus}(-\ell) and ln⁡(1−σ(ℓ))=−softplus⁡(ℓ)\ln(1 - \sigma(\ell)) = -\operatorname{softplus}(\ell). Taking the sigmoid first fails wherever the discriminator is confident. In 32-bit floats σ(ℓ)\sigma(\ell) rounds to exactly 11 once ℓ\ell exceeds about 17, so the discriminator's fake term ln⁡(1−σ(ℓ))\ln(1 - \sigma(\ell)) becomes −∞-\infty where softplus gives about −17-17. At the other end σ(ℓ)\sigma(\ell) underflows to 00 once ℓ\ell falls below about −88-88, and the generator's −ln⁡σ(ℓ)-\ln\sigma(\ell) becomes +∞+\infty. 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 kk, 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 0.90.9 instead of 11 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, DJSD_{\mathrm{JS}} reached its maximum, ln⁡2\ln 2, exactly when the two distributions do not overlap. Then D∗=1D^* = 1 on the data, D∗=0D^* = 0 on the fakes, and

C(θ)=2ln⁡2−2ln⁡2=0C(\theta) = 2\ln 2 - 2\ln 2 = 0

wherever the generator sits, as long as it stays off the data. Slide its samples from far away to just next to the data: CC does not move.

The gradient fares no better. Backpropagation sends the generator ∇xD\nabla_x D at the fakes, and with disjoint supports there is an optimal discriminator that equals 00 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 dd-dimensional zz into image space. When dd is smaller than the number of pixel values, all its samples lie on a surface of dimension at most dd. Then QθQ_\theta has no density qθq_\theta 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 ln⁡2\ln 2 however close the generator gets.

What the generator needs is a distance that shrinks smoothly as QθQ_\theta approaches PP, 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.

Full notes

Answers are saved on the server and tied to this browser until you sign in.