Course contents 14 / 34

14 / 34 · GANs

Generative adversarial networks

How to train a sampler that has no density: a classifier learns to tell its samples from real data, the generator learns to fool it, and the game minimizes the Jensen–Shannon divergence.

On this page 12

Take a network that turns Gaussian noise into 64×6464 \times 64 images. You can draw samples and score them, but not compute the probability of a training image: that is an integral over all noise vectors that map near it, with no closed form. How do you train it without maximum likelihood?

A two-sample test decides from two sets of samples whether they come from the same distribution. In high dimension no fixed statistic is sharp enough, so learn one: a classifier that tells real images from generated ones. Where it beats chance shows where the generator is wrong; if the generator matches the data, no classifier beats a coin. A generative adversarial network (GAN) trains the sampler against such a classifier (Goodfellow et al., 2014).

Likelihood and sample quality can disagree anyway. A model that puts weight 0.010.01 on the true density and 0.990.99 on noise loses at most ln⁡100≈4.6\ln 100 \approx 4.6 nats per image: the logarithm is increasing, so ln⁡(0.01 p+0.99 q)≥ln⁡(0.01 p)=ln⁡p−ln⁡100\ln(0.01\,p + 0.99\,q) \ge \ln(0.01\,p) = \ln p - \ln 100. The log-likelihood of an image grows with its number of pixels, so in high dimension this loss is negligible, yet 99 samples in 100 are noise (CS236, lecture 9).

The setup#

Sample noise zz from a simple prior pzp_z, such as a standard Gaussian, and pass it through the generator GG, a network with parameters θg\theta_g. The outputs G(z)G(z) have some distribution pgp_g on image space. The discriminator DD, with parameters θd\theta_d, receives either a generated sample, labelled y=0y = 0, or a data point x∼pdatax \sim p_{\text{data}}, labelled y=1y = 1, and outputs D(x)∈(0,1)D(x) \in (0, 1), the probability that its input is real.

Figure

The GAN game as a picture

Generator and discriminator data flowOn the left, a noise node z drawn from p_z(z), sketched as a broad bell curve, feeds upward into a box G with parameters theta g. G outputs a node G(z), the generated data, whose density p_g(x) is sketched as a two-humped curve. An arrow labelled y equals 0, fake, goes from G(z) to the discriminator node D with parameters theta d. From below, the original data node x, drawn from p_data(x) and sketched as another two-humped curve, sends an arrow labelled y equals 1, real, into D. D outputs P(input is original).pg(x)xpz(z)zpdata(x)xznoisez ~ pz(z)GθgG(z)generated datay = 0fakeDθdP(input is original)xoriginal datax ~ pdata(x)y = 1realGenerator and discriminator data flowA vertical chain. At the top, the noise node z drawn from p_z(z), sketched as a bell curve, feeds into the box G with parameters theta g, which outputs G(z), the generated data with density p_g(x). An arrow labelled y equals 0, fake, goes down to the discriminator D with parameters theta d. From the bottom, the original data node x drawn from p_data(x) sends an arrow labelled y equals 1, real, up into D. D outputs P(input is original), written to its left.pz(z)zpg(x)xpdata(x)xznoisez ~ pzGθgG(z)generateddatay = 0fakeDθdP(input isoriginal)y = 1realxoriginal datax ~ pdata
Noise z passes through the generator G; its outputs G(z), with distribution p_g, reach the discriminator D with the label y = 0, and real data x ~ p_data with the label y = 1. D outputs the probability that its input is real. The small plots sketch the noise, generator and data densities.

The generator never sees a training image; its only signal is the gradient of the discriminator’s output. The model is implicit: it defines pgp_g by a sampling procedure and never writes down a density.

A minimax game#

The discriminator wants D(x)D(x) near 1 on real data and D(G(z))D(G(z)) near 0 on fakes. The generator wants the opposite on its fakes. Both goals are measured by one value function

V(G,D)=Ex∼pdata[ln⁡D(x)]+Ez∼pz[ln⁡(1−D(G(z)))],\begin{aligned} V(G, D) ={}& \mathbb E_{x\sim p_{\text{data}}}\big[\ln D(x)\big] \\ &+ \mathbb E_{z\sim p_z}\big[\ln\big(1 - D(G(z))\big)\big], \end{aligned}

which the discriminator maximizes and the generator minimizes: the game is min⁡Gmax⁡DV(G,D)\min_G \max_D V(G, D).

Where the value function comes from: binary cross-entropy#

The discriminator is a binary classifier, so its natural loss is the binary cross-entropy between a label yy and a prediction y^\hat y:

H(y,y^)=−[yln⁡y^+(1−y)ln⁡(1−y^)].H(y, \hat y) = -\big[y\ln\hat y + (1 - y)\ln(1 - \hat y)\big] .

A real sample has y=1y = 1 and costs −ln⁡D(x)-\ln D(x). A fake has y=0y = 0 and costs −ln⁡(1−D(G(z)))-\ln\big(1 - D(G(z))\big). In a stream with as many real samples as fakes, each kind has weight 12\tfrac12, so by linearity of expectation the mean loss is

Hˉ=12 Ex[−ln⁡D(x)]+12 Ez[−ln⁡(1−D(G(z)))]=−12 V(G,D).\begin{aligned} \bar H &= \tfrac12\,\mathbb E_x\big[-\ln D(x)\big] \\ &\quad + \tfrac12\,\mathbb E_z\big[-\ln\big(1 - D(G(z))\big)\big] \\ &= -\tfrac12\,V(G, D) . \end{aligned}

The factor 12\tfrac12 does not move the maximizer, so maximizing VV over DD is ordinary classifier training with class prior 12\tfrac12.

How to optimize: alternate the players#

The two networks take turns, each step on a minibatch of size mm (Goodfellow et al., 2014, Algorithm 1).

  1. Fix GG. Draw real samples x(1),…,x(m)x^{(1)}, \dots, x^{(m)} and noise z(1),…,z(m)z^{(1)}, \dots, z^{(m)}, and take a gradient ascent step on θd\theta_d along
∇θd 1m∑i=1m[ln⁡D(x(i))+ln⁡(1−D(G(z(i))))].\nabla_{\theta_d}\,\frac1m\sum_{i=1}^{m}\Big[\ln D\big(x^{(i)}\big) + \ln\Big(1 - D\big(G(z^{(i)})\big)\Big)\Big].
  1. Fix DD. Draw fresh noise and take a gradient descent step on θg\theta_g along
∇θg 1m∑i=1mln⁡(1−D(G(z(i)))).\nabla_{\theta_g}\,\frac1m\sum_{i=1}^{m}\ln\Big(1 - D\big(G(z^{(i)})\big)\Big).

The generator’s step drops the first term because ln⁡D(x)\ln D(x) on real data does not depend on θg\theta_g. Step 1 can be repeated kk times per generator step; the original paper used k=1k = 1 (see the balance section).

Global optimality: the generator matches the data#

Why should this game end with pg=pdatap_g = p_{\text{data}}? The argument has two steps. It assumes that both distributions have densities on the same space and that DD can be any function (Goodfellow et al., 2014, Section 4).

Step 1: the optimal discriminator for a fixed generator#

The generated samples x=G(z)x = G(z) have density pgp_g, so the second expectation can be taken over x∼pgx \sim p_g:

V(G,D)=∫[pdata(x)ln⁡D(x)+pg(x)ln⁡(1−D(x))] dx.\begin{aligned} V(G, D) = \int \Big[&p_{\text{data}}(x)\ln D(x) \\ &+ p_g(x)\ln\big(1 - D(x)\big)\Big]\,dx . \end{aligned}

DD can take any value at every xx, so we maximize the integrand pointwise. Write a=pdata(x)a = p_{\text{data}}(x), b=pg(x)b = p_g(x) and s=D(x)s = D(x). For a,b>0a, b > 0,

f(s)=aln⁡s+bln⁡(1−s),f′(s)=as−b1−s=0,s∗=aa+b.\begin{aligned} f(s) &= a\ln s + b\ln(1 - s), \\ f'(s) &= \frac{a}{s} - \frac{b}{1 - s} = 0 , \\ s^* &= \frac{a}{a + b} . \end{aligned}

The second derivative, −a/s2−b/(1−s)2-a/s^2 - b/(1 - s)^2, is negative, so s∗s^* is the unique maximum. Where only one density is positive, ff is monotone and the same formula gives the endpoint, s∗=1s^* = 1 or 00; where both vanish, DD does not matter.

Figure

Each point x is a one-variable maximization

a ln s plus b ln(1 minus s) over s between 0 and 1Two concave curves on s from 0 to 1, both falling to minus infinity at the ends. The solid green curve for a = 0.6 and b = 0.3 peaks at s = 2/3 with value −0.573. The dashed curve for a = b = 0.45 peaks at s = 1/2 with value −0.624. Dotted vertical lines drop from the two peaks to the horizontal axis, which is s = D(x). The vertical axis shows a ln s + b ln(1 − s).0−1−20½1⅔s = D(x)a ln s + b ln(1 − s)maximum ats* = a/(a+b)a = b ⇒ s* = ½a ln s plus b ln(1 minus s) over s between 0 and 1Two concave curves on s from 0 to 1, both falling to minus infinity at the ends. The solid green curve for a = 0.6 and b = 0.3 peaks at s = 2/3 with value −0.573. The dashed curve for a = b = 0.45 peaks at s = 1/2 with value −0.624. Dotted vertical lines drop from the two peaks to the horizontal axis, which is s = D(x). The vertical axis shows a ln s + b ln(1 − s).0−1−20½1⅔s = D(x)a ln s + b ln(1 − s)maximum ats* = a/(a+b)a = b ⇒ s* = ½
With a = p_data(x) and b = p_g(x) fixed, a ln s + b ln(1 − s) is concave in s = D(x) and peaks at s* = a / (a + b). Solid: a = 0.6, b = 0.3, so s* = 2/3 and the maximum is −0.573. Dashed: a = b = 0.45, so s* = ½ and the maximum is −0.624.

For a fixed generator the best discriminator is therefore

DG∗(x)=pdata(x)pdata(x)+pg(x).D^*_G(x) = \frac{p_{\text{data}}(x)}{p_{\text{data}}(x) + p_g(x)} .

It is the posterior probability of “real” when real and fake samples are equally likely a priori.

Figure

The optimal discriminator compares the two densities

Two densities and the optimal discriminatorTop panel: the data density p_data, red, with a tall mode at minus 1.5 and a smaller one at 0.6, and the generator density p_g, blue, a single bell at 1.8. They cross at x about 0.90. Bottom panel: D star of x, green, equals almost 1 on the left where only data has mass, falls through one half exactly at the crossing point, and approaches 0 on the right where only the generator has mass. A dashed vertical line links the crossing in both panels.densitypdata(x)pg(x)pdata = pg0½1−4−2024xD*(x)D* = ½ where pdata = pg≈ 1: only real data≈ 0: only generatedTwo densities and the optimal discriminatorTop panel: the data density p_data, red, with a tall mode at minus 1.5 and a smaller one at 0.6, and the generator density p_g, blue, a single bell at 1.8. They cross at x about 0.90. Bottom panel: D star of x, green, equals almost 1 on the left where only data has mass, falls through one half exactly at the crossing point, and approaches 0 on the right where only the generator has mass. A dashed vertical line links the crossing in both panels.densitypdata(x)pg(x)pdata = pg0½1−4−2024xD*(x)D* = ½ wherepdata = pg≈ 1: onlyreal data≈ 0: only generated
D*(x) = p_data(x) / (p_data(x) + p_g(x)) is close to 1 where only real data live, close to 0 where only generated samples live, and ½ where the densities are equal (in the plotted range, at x ≈ 0.90). Densities: p_data = 0.6·N(−1.5, 0.6²) + 0.4·N(0.6, 0.5²), p_g = N(1.8, 0.85²).

Step 2: substitute the optimal discriminator and recognize JS#

Put DG∗D^*_G back into VV, using 1−DG∗=pg/(pdata+pg)1 - D^*_G = p_g / (p_{\text{data}} + p_g):

C(G)=max⁡DV(G,D)=Epdataln⁡pdatapdata+pg+Epgln⁡pgpdata+pg.\begin{aligned} C(G) &= \max_D V(G, D) \\ &= \mathbb E_{p_{\text{data}}}\ln\frac{p_{\text{data}}}{p_{\text{data}} + p_g} \\ &\quad + \mathbb E_{p_g}\ln\frac{p_g}{p_{\text{data}} + p_g} . \end{aligned}

The Jensen–Shannon divergence compares each distribution with the mixture m=12(pdata+pg)m = \tfrac12(p_{\text{data}} + p_g). To bring it in, write each denominator as 2m2m and pull out the 2, which costs ln⁡2\ln 2 per expectation:

C(G)=Epdataln⁡pdatam+Epgln⁡pgm−2ln⁡2=DKL(pdata ∥ m)+DKL(pg ∥ m)−2ln⁡2=2 DJS(pdata ∥ pg)−2ln⁡2.\begin{aligned} C(G) &= \mathbb E_{p_{\text{data}}}\ln\frac{p_{\text{data}}}{m} + \mathbb E_{p_g}\ln\frac{p_g}{m} - 2\ln 2 \\ &= D_{\mathrm{KL}}(p_{\text{data}} \,\|\, m) + D_{\mathrm{KL}}(p_g \,\|\, m) - 2\ln 2 \\ &= 2\,D_{\mathrm{JS}}(p_{\text{data}} \,\|\, p_g) - 2\ln 2 . \end{aligned}

The second line is the definition of KL, and the third the definition of JS, DJS(p ∥ q)=12DKL(p ∥ m)+12DKL(q ∥ m)D_{\mathrm{JS}}(p \,\|\, q) = \tfrac12 D_{\mathrm{KL}}(p \,\|\, m) + \tfrac12 D_{\mathrm{KL}}(q \,\|\, m), multiplied by 2.

Against an optimal discriminator, the generator minimizes the Jensen–Shannon divergence. DJS≥0D_{\mathrm{JS}} \ge 0, with equality only when the two densities coincide, so the unique minimum is

min⁡GC(G)=−2ln⁡2≈−1.386at pg=pdata,\min_G C(G) = -2\ln 2 \approx -1.386 \quad \text{at } p_g = p_{\text{data}},

where D∗=12D^* = \tfrac12 everywhere. For the two densities of the figure, DJS≈0.430D_{\mathrm{JS}} \approx 0.430 nats and C(G)≈−0.526C(G) \approx -0.526.

Where it breaks: supports that do not overlap#

Let pdatap_{\text{data}} and pgp_g live on disjoint sets. Then D∗=1D^* = 1 on the data and 00 on the fakes, DJS=ln⁡2D_{\mathrm{JS}} = \ln 2 and C(G)=0C(G) = 0, wherever pgp_g sits. Moving the generator closer to the data, without overlap, does not change CC at all.

The generator’s gradient is ∇xD\nabla_x D at its samples, sent back through GG. Disjoint compact supports lie a distance δ>0\delta > 0 apart. A smooth function equal to 1 within δ/3\delta/3 of the data and 0 within δ/3\delta/3 of the fakes makes both logarithms in VV zero, their maximum, so it is an optimal discriminator; it is constant around every fake, so its gradient there is zero, and so is the generator’s (Arjovsky and Bottou, 2017, Theorem 2.1; Theorem 2.2 extends it to manifolds that do not align).

This is the usual case. A generator built from affine maps and ReLU-type or smooth increasing activations maps its noise into a countable union of manifolds of dimension at most dim⁡z\dim z, which has zero volume when dim⁡z\dim z is below the number of pixels (Arjovsky and Bottou, 2017, Lemma 1). Natural images are believed to concentrate near a low-dimensional set too. After an arbitrarily small random shift, two such sets meet, if at all, in a lower-dimensional set that carries no mass (Lemmas 2 and 3), as two curves in three-dimensional space miss each other. The fitting note saw the same saturation of JS in one dimension; the Wasserstein GAN replaces JS with a distance that keeps a slope.

Training as a picture#

Figure

How the two players move during training

Four stages of GAN trainingFour panels with the same red two-mode data density. Panel 1, at the beginning: the blue generator density is a narrow bell far to the right and the green discriminator curve is an arbitrary wiggle around 0.6. Panel 2, after updating the discriminator parameters: the green curve is the optimal discriminator, near 1 over the data and dropping to 0 over the generator bell. Panel 3, after updating the generator parameters: the blue density has moved left onto the data; the old bell is drawn dashed, with an arrow pointing left from it, while the green curve is the same as in panel 2. Panel 4, finally: the blue density coincides with the red one and the green discriminator is the flat line one half.1. At the beginningD(x)pdatapg2. After updating θdD ≈ D*pdatapg3. After updating θgD unchangedpdatapg4. FinallyD(x) = ½pg = pdataFour stages of GAN trainingFour panels with the same red two-mode data density. Panel 1, at the beginning: the blue generator density is a narrow bell far to the right and the green discriminator curve is an arbitrary wiggle around 0.6. Panel 2, after updating the discriminator parameters: the green curve is the optimal discriminator, near 1 over the data and dropping to 0 over the generator bell. Panel 3, after updating the generator parameters: the blue density has moved left onto the data; the old bell is drawn dashed, with an arrow pointing left from it, while the green curve is the same as in panel 2. Panel 4, finally: the blue density coincides with the red one and the green discriminator is the flat line one half.1. At the beginningD(x)pdatapg2. After updating θdD ≈ D*pdatapg3. After updating θgD unchangedpdatapg4. FinallyD(x) = ½pg = pdata
Red: data density p_data; blue: generator density p_g; green: discriminator output D(x). A discriminator step moves D towards D* = p_data / (p_data + p_g); a generator step moves p_g towards where D is high (dashed blue in panel 3: p_g before the step). At equilibrium p_g = p_data and D = ½. Curves computed from explicit densities.

Watch the discriminator curve. Each DD step makes it high over the data and low over the fakes; each GG step pushes the fakes up its slope. When the two densities coincide, the curve is flat at 12\tfrac12.

The picture assumes an optimal DD before every generator step and unlimited capacity. Real networks, updated a few steps at a time, can oscillate or settle on a few modes (Goodfellow, 2016).

Which generator loss is used in practice?#

The minimax generator loss Ezln⁡(1−D(G(z)))\mathbb E_z\ln\big(1 - D(G(z))\big) is rarely used. Already the original paper trains the generator to maximize ln⁡D(G(z))\ln D(G(z)) instead, that is, to minimize the non-saturating loss

LG=−Ezln⁡D(G(z)).\mathcal L_G = -\mathbb E_z\ln D\big(G(z)\big) .

Why change it? Early in training DD rejects fakes confidently, d=D(G(z))≈0d = D(G(z)) \approx 0. Compare the slopes of the two losses in dd:

∂∂dln⁡(1−d)=−11−d≈−1,∂∂d(−ln⁡d)=−1d→−∞.\begin{aligned} \frac{\partial}{\partial d}\ln(1 - d) &= -\frac{1}{1 - d} \approx -1, \\ \frac{\partial}{\partial d}\big(-\ln d\big) &= -\frac1d \to -\infty . \end{aligned}

At d=0.05d = 0.05 the slopes are −1.05-1.05 and −20-20. Through the logit ℓ\ell, with d=σ(ℓ)d = \sigma(\ell) and ∂d/∂ℓ=d(1−d)\partial d/\partial\ell = d(1 - d), the minimax loss has slope −d-d, which vanishes as DD grows confident, and the non-saturating loss −(1−d)-(1 - d), largest there: the minimax loss gives the weakest signal to the worst generator.

Figure

Saturating and non-saturating generator losses

The two generator losses against d and against the discriminator logitTwo stacked panels. Top: the horizontal axis is d = D(G(z)) from 0 to 1, with the region near 0 shaded and labelled D says fake. The amber curve ln(1 − d) starts at 0 for d = 0 and falls to minus infinity as d approaches 1; at d = 0.05 its tangent has slope about −1.05, labelled shallow. The blue curve minus ln d starts at plus infinity near d = 0 and falls to 0 at d = 1; at d = 0.05 its tangent has slope −20, labelled steep. Bottom: the horizontal axis is the logit ℓ from −6 to 3. The amber curve ln(1 − σ(ℓ)) is almost flat at 0 for very negative logits and bends down for positive ones; its slope at the logit of 0.05 is −0.05, labelled saturated. The blue curve −ln σ(ℓ) is almost a straight line with slope −1 for negative logits and flattens towards 0 for positive ones; its slope there is −0.95.D says“fake”−4−202400.250.50.751d = D(G(z))generator losssteep: slope −1/d = −20shallow: slope −1.05−ln D(G(z))non-saturating (used in code)ln(1 − D(G(z)))minimax / saturating−20246−6−4−202logit ℓ, d = σ(ℓ)generator losssaturated: slope −d = −0.05→ 0 as D grows confidentD says “fake”linear: slope −(1 − d) = −0.95−ln σ(ℓ)ln(1 − σ(ℓ))The two generator losses against d and against the discriminator logitTwo stacked panels. Top: the horizontal axis is d = D(G(z)) from 0 to 1, with the region near 0 shaded and labelled D says fake. The amber curve ln(1 − d) starts at 0 for d = 0 and falls to minus infinity as d approaches 1; at d = 0.05 its tangent has slope about −1.05, labelled shallow. The blue curve minus ln d starts at plus infinity near d = 0 and falls to 0 at d = 1; at d = 0.05 its tangent has slope −20, labelled steep. Bottom: the horizontal axis is the logit ℓ from −6 to 3. The amber curve ln(1 − σ(ℓ)) is almost flat at 0 for very negative logits and bends down for positive ones; its slope at the logit of 0.05 is −0.05, labelled saturated. The blue curve −ln σ(ℓ) is almost a straight line with slope −1 for negative logits and flattens towards 0 for positive ones; its slope there is −0.95.D says“fake”−4−202400.250.50.751d = D(G(z))generator losssteep: slope −1/d = −20shallow: slope −1.05−ln D(G(z))non-saturating (used in code)ln(1 − D(G(z)))minimax / saturating−20246−6−4−202logit ℓ, d = σ(ℓ)generator losssaturated: slope −d = −0.05D says “fake”linear: slope −(1 − d) = −0.95−ln σ(ℓ)ln(1 − σ(ℓ))
Top: both losses fall as d = D(G(z)) grows. At d = 0.05 the minimax loss ln(1 − d) has slope −1.05 and −ln d has slope −20. Bottom: the same losses against the logit ℓ, d = σ(ℓ), the path the gradient takes into the networks: ln(1 − σ(ℓ)) saturates with slope −d = −0.05, while −ln σ(ℓ) keeps slope −(1 − d) = −0.95.

Both losses share the fixed point pg=pdatap_g = p_{\text{data}}, but the non-saturating loss is no longer the minimax game. With D=D∗D = D^* computed for the current generator and held fixed, its expected gradient is the gradient of DKL(pg ∥ pdata)−2 DJS(pdata ∥ pg)D_{\mathrm{KL}}(p_g \,\|\, p_{\text{data}}) - 2\,D_{\mathrm{JS}}(p_{\text{data}} \,\|\, p_g) (Arjovsky and Bottou, 2017, Theorem 2.5), derived below. The first term, a reverse KL, punishes fakes where the data have little mass and barely punishes missed data modes, one reason for dropped modes.

Derivation

Let pθp_\theta be the generator’s density, θ0\theta_0 the current parameters and D∗=pdata/(pdata+pθ0)D^* = p_{\text{data}}/(p_{\text{data}} + p_{\theta_0}), frozen. Assume pθp_\theta is differentiable in θ\theta, that ∇θ\nabla_\theta and ∫\int can be swapped, and that pdata>0p_{\text{data}} > 0 wherever pθ>0p_\theta > 0. Two facts do the work. For a fixed function hh, Ezh(Gθ(z))=∫pθh\mathbb E_z h(G_\theta(z)) = \int p_\theta h, so its gradient is ∫∇pθ h\int \nabla p_\theta\, h. And ∫∇pθ=∇1=0\int \nabla p_\theta = \nabla 1 = 0, so constants added to hh drop out.

Each divergence is ∫ϕ(pθ(x)) dx\int \phi(p_\theta(x))\,dx for a function ϕ\phi of the value p=pθ(x)p = p_\theta(x), so its gradient is ∫ϕ′(pθ) ∇pθ dx\int \phi'(p_\theta)\,\nabla p_\theta\,dx by the chain rule. With a=pdata(x)a = p_{\text{data}}(x):

ϕKL(p)=pln⁡pa,ϕKL′(p)=ln⁡pa+1,ϕJS(p)=aln⁡2aa+p+pln⁡2pa+p,ϕJS′(p)=−aa+p+ln⁡2pa+p+1−pa+p=ln⁡2+ln⁡pa+p.\begin{aligned} \phi_{\mathrm{KL}}(p) &= p\ln\frac{p}{a}, \\ \phi_{\mathrm{KL}}'(p) &= \ln\frac{p}{a} + 1, \\ \phi_{\mathrm{JS}}(p) &= a\ln\frac{2a}{a + p} + p\ln\frac{2p}{a + p}, \\ \phi_{\mathrm{JS}}'(p) &= -\frac{a}{a + p} + \ln\frac{2p}{a + p} + 1 - \frac{p}{a + p} \\ &= \ln 2 + \ln\frac{p}{a + p} . \end{aligned}

Here ϕJS\phi_{\mathrm{JS}} is the integrand of 2DJS2D_{\mathrm{JS}}. At θ0\theta_0, p/a=(1−D∗)/D∗p/a = (1 - D^*)/D^* and p/(a+p)=1−D∗p/(a + p) = 1 - D^*. Dropping the constants 1 and ln⁡2\ln 2:

∇DKL=∫∇pθ [ln⁡(1−D∗)−ln⁡D∗],∇ 2DJS=∫∇pθ ln⁡(1−D∗),∇(DKL−2DJS)=∫∇pθ (−ln⁡D∗)=∇θ Ez[−ln⁡D∗(Gθ(z))].\begin{aligned} \nabla D_{\mathrm{KL}} &= \int \nabla p_\theta\,\big[\ln(1 - D^*) - \ln D^*\big] , \\ \nabla\, 2D_{\mathrm{JS}} &= \int \nabla p_\theta\,\ln(1 - D^*) , \\ \nabla\big(D_{\mathrm{KL}} - 2D_{\mathrm{JS}}\big) &= \int \nabla p_\theta\,\big(-\ln D^*\big) \\ &= \nabla_\theta\,\mathbb E_z\big[-\ln D^*(G_\theta(z))\big] . \end{aligned}

The last line is the first fact read backwards. The second line also shows that the minimax loss Ezln⁡(1−D∗(Gθ(z)))\mathbb E_z\ln(1 - D^*(G_\theta(z))) has the gradient of 2DJS2D_{\mathrm{JS}}, as Step 2 says.

The non-saturating loss also leaves the disjoint-support problem in place. Near a perfect discriminator D(G(z))≈0D(G(z)) \approx 0, and the gradient −∇xD/D-\nabla_x D / D divides one small error by another. If the errors of DD and of ∇xD\nabla_x D are independent zero-mean Gaussian noise, the ratio has a Cauchy distribution, which has no mean and no variance (Arjovsky and Bottou, 2017, Theorem 2.6). The updates become noisy instead of vanishing.

Computing the losses from logits#

Both logarithms are functions of the logit that never need σ(ℓ)\sigma(\ell):

ln⁡D=ln⁡σ(ℓ)=−softplus⁡(−ℓ),ln⁡(1−D)=ln⁡σ(−ℓ)=−softplus⁡(ℓ),\begin{aligned} \ln D &= \ln\sigma(\ell) = -\operatorname{softplus}(-\ell), \\ \ln(1 - D) &= \ln\sigma(-\ell) = -\operatorname{softplus}(\ell), \end{aligned}

where softplus⁡(t)=ln⁡(1+et)\operatorname{softplus}(t) = \ln(1 + e^{t}) is evaluated stably as max⁡(t,0)+ln⁡(1+e−∣t∣)\max(t, 0) + \ln(1 + e^{-\lvert t\rvert}), the two-term log-sum-exp. The discriminator then minimizes Exsoftplus⁡(−ℓ(x))+Ezsoftplus⁡(ℓ(G(z)))\mathbb E_x\operatorname{softplus}(-\ell(x)) + \mathbb E_z\operatorname{softplus}(\ell(G(z))) and the generator Ezsoftplus⁡(−ℓ(G(z)))\mathbb E_z\operatorname{softplus}(-\ell(G(z))).

Computing σ\sigma first fails where the discriminator is confident: in 32-bit floating point σ(ℓ)\sigma(\ell) rounds to 1 once ℓ\ell exceeds about 17, so ln⁡(1−σ(ℓ))=−∞\ln(1 - \sigma(\ell)) = -\infty where softplus gives about −17-17. PyTorch provides the stable form as binary_cross_entropy_with_logits.

Training GANs in practice#

Real training depends on empirical rules; none is a theorem, so each comes with the reason it helps.

Architecture: the DCGAN guidelines#

The first convolutional GANs that trained reliably on images followed five rules (Radford, Metz and Chintala, 2016):

  1. strided convolutions instead of pooling, transposed convolutions to upsample;
  2. batch normalization in both networks, except at the generator’s output and the discriminator’s input;
  3. no fully connected hidden layers: the noise is reshaped into a 4×44 \times 4 map with 1024 channels;
  4. ReLU in the generator, Tanh at its output, images scaled to [−1,1][-1, 1];
  5. LeakyReLU with slope 0.20.2 in the discriminator.

Training used Adam with learning rate 2⋅10−42\cdot10^{-4} and β1=0.5\beta_1 = 0.5 (the usual 0.90.9 oscillated), minibatches of 128 and weights from N(0,0.022)\mathcal N(0, 0.02^2).

The rules keep the gradient signal dense: max-pooling passes gradient to one input per window and ReLU none for negative inputs, while strided convolutions and LeakyReLU pass some everywhere. Batch normalization let deep generators start learning without collapsing all samples to one point. Later models upsample by resizing and a convolution, which avoids checkerboard artifacts.

Sampling z∼N(0,0.72I)z \sim \mathcal N(0, 0.7^2 I) instead of N(0,I)\mathcal N(0, I) trades recall for precision; an FID of such samples must report the scale. BigGAN’s truncation trick is the principled version.

Balancing the two players#

The discriminator is the generator’s only source of gradient, so the two must improve at comparable speeds, tuned by kk, the learning rates and the capacities. A discriminator that rejects every fake leaves the generator vanishing or noisy gradients: weaken it. One that lags points the generator in poor directions: update it more often. The WGAN makes a strong critic help instead.

The losses say little about sample quality: track samples from fixed noise vectors and an FID, where a collapse shows first. Gradient-norm clipping guards against explosive updates.

Label smoothing and label flipping#

One-sided label smoothing trains the discriminator toward 0.90.9 instead of 1 on real samples (Salimans et al., 2016). Why only the real side? With targets α\alpha for real and β\beta for fake samples, the pointwise integrand of Step 1 becomes Aln⁡s+Bln⁡(1−s)A\ln s + B\ln(1 - s) with A=αpdata+βpgA = \alpha p_{\text{data}} + \beta p_g and B=(1−α)pdata+(1−β)pgB = (1 - \alpha)p_{\text{data}} + (1 - \beta)p_g, maximized at A/(A+B)A/(A + B):

D∗(x)=α pdata(x)+β pg(x)pdata(x)+pg(x).D^*(x) = \frac{\alpha\,p_{\text{data}}(x) + \beta\,p_g(x)}{p_{\text{data}}(x) + p_g(x)} .

Look at a region where the generator puts much more mass than the data, with r=pdata/pgr = p_{\text{data}}/p_g small. To first order in rr, D∗≈β+(α−β)rD^* \approx \beta + (\alpha - \beta)r, and the non-saturating loss is

−ln⁡D∗≈−ln⁡α−ln⁡rβ=0,−ln⁡D∗≈−ln⁡β−α−ββ rβ>0.\begin{aligned} -\ln D^* &\approx -\ln\alpha - \ln r && \beta = 0, \\ -\ln D^* &\approx -\ln\beta - \frac{\alpha - \beta}{\beta}\,r && \beta > 0 . \end{aligned}

With β=0\beta = 0 the loss follows the log density ratio, and its gradient does not fade as rr shrinks. With β>0\beta > 0 the gradient carries the factor rr, so fakes far from the data barely move. Hence only the real label is smoothed.

Random label flipping of a fraction ε\varepsilon of the labels is, in expectation, two-sided smoothing with α=1−ε\alpha = 1 - \varepsilon, β=ε\beta = \varepsilon (cross-entropy is linear in the target), so it is used sparingly.

Mode collapse and how to fight it#

In mode collapse the generator maps most of its noise to a few outputs the current discriminator accepts. The discriminator judges each sample on its own, so nothing tells the outputs to differ (Salimans et al., 2016); when it rejects the collapsed mode, the generator moves to another. The reverse-KL term makes dropping a mode cheap.

Take data from eight Gaussians of standard deviation 0.020.02 on a circle. A standard GAN puts almost all its mass on one mode at a time and rotates from mode to mode, never covering all eight (Metz et al., 2017). Nearly every sample lies on a real mode while one mode in eight is covered: precision near 1, recall near 1/81/8 (toy values). FID penalizes the missing modes; each sample alone looks perfect.

Minibatch discrimination lets the discriminator compare samples within a batch (Salimans et al., 2016). A learned tensor maps a feature vector f(xi)f(x_i) to a matrix MiM_i with rows Mi,bM_{i,b}, giving the features

cb(xi,xj)=exp⁡(−∥Mi,b−Mj,b∥1),o(xi)b=∑jcb(xi,xj),b=1,…,B.\begin{aligned} c_b(x_i, x_j) &= \exp\big(-\lVert M_{i,b} - M_{j,b}\rVert_1\big), \\ o(x_i)_b &= \sum_{j} c_b(x_i, x_j), \quad b = 1, \dots, B . \end{aligned}

The vector o(xi)o(x_i) is appended to f(xi)f(x_i); a collapsed batch has large oo, which the discriminator learns to reject.

Unrolled GANs let the generator anticipate the discriminator’s reply (Metz et al., 2017). Starting from the current θD\theta_D, take KK ascent steps of the discriminator and score the generator against the result:

θD0=θD,θDk+1=θDk+η ∇θDV(θG,θDk),VK(θG,θD)=V(θG,θDK(θG,θD)).\begin{aligned} \theta_D^{0} &= \theta_D, \\ \theta_D^{k+1} &= \theta_D^{k} + \eta\,\nabla_{\theta_D} V\big(\theta_G, \theta_D^{k}\big), \\ V_K(\theta_G, \theta_D) &= V\big(\theta_G, \theta_D^{K}(\theta_G, \theta_D)\big) . \end{aligned}

The generator descends VKV_K through the KK steps; the discriminator is updated with VV. By the chain rule,

dVKdθG=∂V∂θG+∂V∂θDK dθDKdθG.\frac{dV_K}{d\theta_G} = \frac{\partial V}{\partial \theta_G} + \frac{\partial V}{\partial \theta_D^{K}}\,\frac{d\theta_D^{K}}{d\theta_G} .

The first term alone favours putting all mass on the point DD likes most; the second sees that the discriminator would reject such a collapse. K=0K = 0 is the standard GAN; with K=10K = 10 the eight-Gaussian generator covers all modes. A buffer of earlier fakes, cheaper, keeps an abandoned mode punished.

Conditional GAN#

A conditional GAN (Mirza and Osindero, 2014) gives both networks a label cc, such as a class. The generator produces G(z,c)G(z, c) and the discriminator judges pairs D(x,c)D(x, c):

Vcond=E(x,c)∼pdata[ln⁡D(x,c)]+Ec∼pc, z∼pz[ln⁡(1−D(G(z,c),c))],\begin{aligned} V_{\text{cond}} ={}& \mathbb E_{(x, c)\sim p_{\text{data}}}\big[\ln D(x, c)\big] \\ &+ \mathbb E_{c \sim p_c,\, z \sim p_z}\big[\ln\big(1 - D(G(z, c), c)\big)\big], \end{aligned}

where pcp_c is the label distribution of the data. Generated pairs have the joint pc(c) pg(x∣c)p_c(c)\,p_g(x \mid c), and the optimality argument applied to pairs gives pg(x,c)=pdata(x,c)p_g(x, c) = p_{\text{data}}(x, c): a realistic image with the wrong label can be rejected, so the generator follows the label, as in the conditional VAE.

f-GAN#

Optional depth: the game for any f-divergence. The rest of the note does not depend on it.

The GAN minimizes JS. An f-GAN (Nowozin, Cseke and Tomioka, 2016) plays the same game for any f-divergence

Df(P ∥ Q)=∫q(x) f ⁣(p(x)q(x))dx,D_f(P \,\|\, Q) = \int q(x)\, f\!\left(\frac{p(x)}{q(x)}\right) dx ,

with ff convex, lower semicontinuous and f(1)=0f(1) = 0; f(u)=uln⁡uf(u) = u\ln u gives DKL(P ∥ Q)D_{\mathrm{KL}}(P \,\|\, Q). The tool is the convex conjugate f∗(t)=sup⁡u{ut−f(u)}f^*(t) = \sup_u \{ut - f(u)\}. By definition f(u)≥tu−f∗(t)f(u) \ge tu - f^*(t) for every tt: each line of slope tt and intercept −f∗(t)-f^*(t) lies below ff. For a convex, lower semicontinuous ff these lines recover it, f(u)=sup⁡t{tu−f∗(t)}f(u) = \sup_t\{tu - f^*(t)\}, the Fenchel–Moreau theorem (Boyd and Vandenberghe, 2004, Section 3.3.2 and Exercise 3.39). For the differentiable ff of the table below, two lines prove it. The inequality gives sup⁡t{tu−f∗(t)}≤f(u)\sup_t\{tu - f^*(t)\} \le f(u). The tangent at uu, of slope tu=f′(u)t_u = f'(u), lies below ff and touches it at uu, so the supremum defining f∗(tu)f^*(t_u) is attained at uu: f∗(tu)=tuu−f(u)f^*(t_u) = t_u u - f(u), and the slope tut_u reaches f(u)f(u). For example, f(u)=12(u−1)2f(u) = \tfrac12(u - 1)^2 has its supremum at u=1+tu = 1 + t, so f∗(t)=t+12t2f^*(t) = t + \tfrac12 t^2.

Figure

The convex conjugate read off a tangent line

Geometric meaning of the convex conjugateA convex parabola f of u with minimum 0 at u equals 1. An amber straight line with slope t equals 1 touches the parabola at the single point u star equals 2, height one half, and crosses the vertical axis at minus 1.5, labelled minus f star of t. A dashed line y equals t u passes through the origin; at u star the vertical gap between the dashed line and the parabola is 1.5, marked by a green double arrow labelled f star of t, the largest gap. A small slope triangle on the tangent line shows rise t over run 1.uy0f(u)y = t uy = t u − f*(t)1tu*slope-t line touches fat one point: f′(u*) = tf*(t) = largest gapbetween t u and f(u)−f*(t)Geometric meaning of the convex conjugateA convex parabola f of u with minimum 0 at u equals 1. An amber straight line with slope t equals 1 touches the parabola at the single point u star equals 2, height one half, and crosses the vertical axis at minus 1.5, labelled minus f star of t. A dashed line y equals t u passes through the origin; at u star the vertical gap between the dashed line and the parabola is 1.5, marked by a green double arrow labelled f star of t, the largest gap. A small slope triangle on the tangent line shows rise t over run 1.uy0f(u)y = t uy = t u − f*(t)1tu*f*(t)−f*(t)
A convex f(u) = ½(u − 1)² (blue) and slope t = 1. The tangent with slope t (amber) touches f at u* = 2, where f′(u*) = t; its intercept is −f*(t) = −1.5. Equivalently, f*(t) is the largest vertical gap between the line t·u (dashed) and f, reached at the same u* (green).

Substitute this at every point, with u=p(x)/q(x)u = p(x)/q(x) and a slope T(x)T(x) that may depend on xx:

Df(P ∥ Q)=∫q(x)sup⁡t{t u(x)−f∗(t)} dx=sup⁡T∫[p(x) T(x)−q(x) f∗(T(x))] dx≥sup⁡T∈T{EP[T(x)]−EQ[f∗(T(x))]}.\begin{aligned} D_f(P \,\|\, Q) &= \int q(x) \sup_t \big\{t\,u(x) - f^*(t)\big\}\,dx \\ &= \sup_T \int \big[p(x)\,T(x) - q(x)\,f^*(T(x))\big]\,dx \\ &\ge \sup_{T \in \mathcal T} \Big\{\mathbb E_{P}\big[T(x)\big] - \mathbb E_{Q}\big[f^*(T(x))\big]\Big\} . \end{aligned}

The second line chooses the slope at each point; nothing is lost, because T(x)=f′(u(x))T(x) = f'(u(x)) attains every pointwise supremum at once (Nguyen, Wainwright and Jordan, 2010). The inequality comes from restricting TT to a network family T\mathcal T. With P=pdataP = p_{\text{data}} and Q=pgQ = p_g, the last line is a two-player game that needs only samples.

A fixed increasing output activation gfg_f maps the network’s unconstrained output v(x)v(x) into the domain of f∗f^*, T(x)=gf(v(x))T(x) = g_f(v(x)). The usual choices, with PP the data:

Divergencef(u)f(u)gf(v)g_f(v)f∗(t)f^*(t)
DKL(P∥Q)D_{\mathrm{KL}}(P\Vert Q)uln⁡uu\ln uvvet−1e^{t-1}
DKL(Q∥P)D_{\mathrm{KL}}(Q\Vert P)−ln⁡u-\ln u−e−v-e^{-v}−1−ln⁡(−t)-1-\ln(-t), t<0t<0
Pearson χ2\chi^2(u−1)2(u-1)^2vvt+t2/4t + t^2/4
squared Hellinger(u−1)2(\sqrt u - 1)^21−e−v1 - e^{-v}t/(1−t)t/(1-t), t<1t<1
2DJS2D_{\mathrm{JS}}uln⁡u−(u+1)ln⁡u+12u\ln u - (u+1)\ln\frac{u+1}{2}ln⁡2−ln⁡(1+e−v)\ln 2 - \ln(1 + e^{-v})−ln⁡(2−et)-\ln(2 - e^{t}), t<ln⁡2t<\ln 2
GANuln⁡u−(u+1)ln⁡(u+1)u\ln u - (u+1)\ln(u+1)−ln⁡(1+e−v)-\ln(1 + e^{-v})−ln⁡(1−et)-\ln(1 - e^{t}), t<0t<0

The GAN row has f(1)=−ln⁡4f(1) = -\ln 4, so its divergence is 2DJS−ln⁡42D_{\mathrm{JS}} - \ln 4; with T=ln⁡DT = \ln D the bound is the value function V(G,D)V(G, D). The fifth row differs from it by a constant and trains the same way.

Evaluate the composition f∗(gf(v))f^*(g_f(v)) in one piece, as with softplus above. Row by row it is ev−1e^{v-1}, v−1v - 1, v+v2/4v + v^2/4, ev−1e^{v} - 1, softplus⁡(v)−ln⁡2\operatorname{softplus}(v) - \ln 2 and softplus⁡(v)\operatorname{softplus}(v).

Least-squares GAN#

Optional depth: a squared-error discriminator and the divergence it minimizes. The rest of the note does not depend on it.

The least-squares GAN (LSGAN) also repairs the gradient for fakes the discriminator already accepts that are still far from the data (Mao et al., 2017). Its discriminator regresses toward bb on real samples and aa on fakes, and the generator asks for the score cc:

min⁡D  12 Epdata[(D(x)−b)2]+12 Ez[(D(G(z))−a)2],min⁡G  12 Ez[(D(G(z))−c)2].\begin{aligned} \min_D\;& \tfrac12\,\mathbb E_{p_{\text{data}}}\big[(D(x) - b)^2\big] + \tfrac12\,\mathbb E_{z}\big[(D(G(z)) - a)^2\big], \\ \min_G\;& \tfrac12\,\mathbb E_{z}\big[(D(G(z)) - c)^2\big] . \end{aligned}

The common coding is a=0a = 0, b=c=1b = c = 1.

With a sigmoid cross-entropy, a fake far on the real side of the boundary has almost no gradient: the slope of −ln⁡σ(ℓ)-\ln\sigma(\ell) is −(1−σ(ℓ))→0-(1 - \sigma(\ell)) \to 0. The squared loss is flat only at D=cD = c, so a fake scored beyond cc is pulled back toward the level set D=cD = c, which helps if that set runs through the data.

Figure

Least squares pull far-away fakes back

How fakes move under BCE and under least squaresTop panel: the red data bell centred at 1 and the blue bell of fakes centred at minus 1; a dashed vertical line at x = 0 marks the decision boundary. Bottom panel, same x axis from minus 4 to 6: the amber BCE curve starts near 1.1 on the left, passes 1 at x = minus 1 and falls to zero to the right of 2. The green least-squares line falls from 2 at x = minus 4, crosses zero at x = 2 and turns negative to the right, reaching minus 1.33 at x = 6. Both curves are marked at x = 4: the amber one is almost at zero, the green one is about minus 0.67.fakes Qdata Pboundary, D = ½−1012−4−20246position of a fake xupdate (rel. to x = −1)BCEleast squaresD(x) = 10.0004−0.67right of 2: BCE almost 0,least squares pull backHow fakes move under BCE and under least squaresTop panel: the red data bell centred at 1 and the blue bell of fakes centred at minus 1; a dashed vertical line at x = 0 marks the decision boundary. Bottom panel, same x axis from minus 4 to 6: the amber BCE curve starts near 1.1 on the left, passes 1 at x = minus 1 and falls to zero to the right of 2. The green least-squares line falls from 2 at x = minus 4, crosses zero at x = 2 and turns negative to the right, reaching minus 1.33 at x = 6. Both curves are marked at x = 4: the amber one is almost at zero, the green one is about minus 0.67.fakes Qdata Pboundary, D = ½−1012−4−20246position of a fake xupdate (rel. to x = −1)BCEleast squaresD(x) = 10.0004−0.67
Top: real data N(1, 1), red, and fakes N(−1, 1), blue; a BCE discriminator with logit 2x and a least-squares one, D(x) = 0.25x + 0.5, share the boundary x = 0. Bottom: the update −∂L_G/∂x each generator loss gives a fake at x, scaled to 1 at x = −1. A fake at x = 4 gets 0.0004 from BCE and −0.67 from least squares, back toward x = 2.

The optimal discriminator. For a fixed generator the integrand 12pdata(D−b)2+12pg(D−a)2\tfrac12 p_{\text{data}}(D - b)^2 + \tfrac12 p_g(D - a)^2 is a parabola in DD, minimized at

D∗(x)=b pdata(x)+a pg(x)pdata(x)+pg(x).D^*(x) = \frac{b\,p_{\text{data}}(x) + a\,p_g(x)}{p_{\text{data}}(x) + p_g(x)} .

Add 12Epdata[(D(x)−c)2]\tfrac12\mathbb E_{p_{\text{data}}}[(D(x) - c)^2] to the generator’s loss; it does not depend on GG. At D=D∗D = D^* the extended loss is

2C(G)=∫(pdata+pg)(D∗−c)2 dx=∫((b−c)pdata+(a−c)pg)2pdata+pg dx.\begin{aligned} 2C(G) &= \int (p_{\text{data}} + p_g)(D^* - c)^2\,dx \\ &= \int \frac{\big((b - c)p_{\text{data}} + (a - c)p_g\big)^2}{p_{\text{data}} + p_g}\,dx . \end{aligned}

Choose b−c=1b - c = 1 and b−a=2b - a = 2, for example a=−1a = -1, b=1b = 1, c=0c = 0. The numerator becomes (pdata−pg)2=4(m−pg)2(p_{\text{data}} - p_g)^2 = 4(m - p_g)^2 and the denominator 2m2m, with m=12(pdata+pg)m = \tfrac12(p_{\text{data}} + p_g):

C(G)=∫(pg(x)−m(x))2m(x) dx=χPearson2(pg ∥ m).C(G) = \int \frac{\big(p_g(x) - m(x)\big)^2}{m(x)}\,dx = \chi^2_{\text{Pearson}}(p_g \,\|\, m) .

Like the JS identity, it compares the generator with the mixture, here through χ2\chi^2. The 0–1 coding misses these conditions, yet its 2C(G)=∫φ(pg(x)) dx2C(G) = \int \varphi(p_g(x))\,dx, with φ(q)=q2/(pdata(x)+q)\varphi(q) = q^2/(p_{\text{data}}(x) + q), is still minimized at pg=pdatap_g = p_{\text{data}} among densities. The constraint ∫pg=1\int p_g = 1 is needed: without it pg=0p_g = 0 would give 0. At each xx, φ′′(q)=2pdata2/(pdata+q)3>0\varphi''(q) = 2p_{\text{data}}^2/(p_{\text{data}} + q)^3 > 0, so φ\varphi is convex, and φ′(pdata)=34\varphi'(p_{\text{data}}) = \tfrac34 at every xx. The tangent bound then gives

∫φ(pg) dx≥∫[φ(pdata)+34 (pg−pdata)]dx=∫φ(pdata) dx,\begin{aligned} \int \varphi(p_g)\,dx &\ge \int \Big[\varphi(p_{\text{data}}) + \tfrac34\,(p_g - p_{\text{data}})\Big]dx \\ &= \int \varphi(p_{\text{data}})\,dx , \end{aligned}

because both densities integrate to 1.

What the game gives up#

A GAN samples with one forward pass, and nothing in its loss averages plausible images into a blur. The price is what the game never computes: without a density, models are compared on samples, with FID and precision and recall; without an encoder, the code of an image needs an optimization over zz or a second network, as in the autoencoding GANs.

For images, realism often counts for more than variety. For molecules in drug discovery, variety is the point, and a likelihood-trained model such as the VAE, whose loss charges every underweighted mode, is the safer choice (BMML lecture notes, 2026), though not a guarantee, as the next section shows.

Strengths and weaknesses#

VAEGAN
Objectivemaximize the ELBO, a lower bound on ln⁡pθ(x)\ln p_\theta(x)a minimax game; against D∗D^*, 2DJS−2ln⁡22D_{\mathrm{JS}} - 2\ln 2
Encoderqϕ(z∣x)q_\phi(z \mid x), trained with the decodernone; inversion needs an optimization or a second network
Densitya lower bound on ln⁡pθ(x)\ln p_\theta(x) for any imagenone, only samples
Typical failuresblurry samples, posterior collapsemode collapse, unstable training
Sampling costone decoder pass: z∼p(z)z \sim p(z), then x∼pθ(x∣z)x \sim p_\theta(x \mid z)one generator pass: z∼pzz \sim p_z, x=G(z)x = G(z)

The failures row is typical behaviour, not theorems. Blur comes from a fixed-variance Gaussian decoder: its squared error is minimized by the average of the images compatible with zz. Mode collapse is a failure of the optimization: at the global optimum of the game, pg=pdatap_g = p_{\text{data}} covers every mode.

Nor does a VAE guarantee coverage: its forward KL charges an underweighted mode only in proportion to the mode’s mass. Let the data put mass 0.050.05 on one mode and 0.950.95 on the rest, on disjoint regions, and the model fit each region exactly but give the rare mode 0.0050.005. The density ratio is constant on each region, and

DKL=0.05ln⁡0.050.005+0.95ln⁡0.950.995≈0.115−0.044=0.071 nats\begin{aligned} D_{\mathrm{KL}} &= 0.05\ln\frac{0.05}{0.005} + 0.95\ln\frac{0.95}{0.995} \\ &\approx 0.115 - 0.044 = 0.071 \text{ nats} \end{aligned}

per sample, invisible next to a log-likelihood of thousands of nats. The ELBO is also only a bound, so training maximizes the bound, not the likelihood.

What you should remember#

  • Against D∗=pdata/(pdata+pg)D^* = p_{\text{data}}/(p_{\text{data}} + p_g) the game value is 2DJS−2ln⁡22D_{\mathrm{JS}} - 2\ln 2, minimized only at pg=pdatap_g = p_{\text{data}}.
  • Disjoint supports: DJS=ln⁡2D_{\mathrm{JS}} = \ln 2 and no gradient.
  • Train GG with −ln⁡D(G(z))-\ln D(G(z)), from logits via softplus.
  • Smooth only the real label; mode collapse is high precision, low recall.
  • f-GAN: any f-divergence via f∗f^*; LSGAN: Pearson χ2\chi^2 to the mixture.

Notebook#

The GANs notebook of the HSE-LAMBDA Deep Generative Models course trains three models; cells are counted from 1, markdown included. It has no saved outputs, so it documents procedures only.

  • Unconditional GAN on MNIST zeros (cells 4–16: data in 4–6, the model and its training from 7). Images at 32×3232 \times 32, resize-convolution upsampling, BCE with logits, one-sided smoothing 0.90.9 (also the generator’s target, so its loss is minimized at D(G(z))=0.9D(G(z)) = 0.9), clipping at 1, one DD step per GG step; noise scaled by 0.70.7 at sampling.
  • Conditional GAN on CIFAR-100 (cells 17–26). The label enters each network as a learned extra channel; Adam with β1=0.5\beta_1 = 0.5 for both.
  • f-GAN (cells 27–33). The table’s activations and conjugates, trained with JS and applied one after the other with plain exp and log: in 32-bit floats a logit above about 17 rounds 2−et2 - e^{t} to 0 and the loss on that fake to +∞+\infty (at v=16v = 16 it already gives 15.25 instead of 15.31). The composed softplus forms avoid this.

Next#

JS gives no slope whenever the two distributions do not overlap, however close. The next note replaces it with the Wasserstein distance, which shrinks steadily as the samples approach the data, and asks what constraint the discriminator then needs.

References#