Simple Algorithm
Artificial Intelligence · Hidden Layer · 24 min

Generative Models

VAEs, GANs and diffusion: learning to create.

A classifier only has to learn what separates a cat from a dog. A model that can draw a new cat must capture the whole distribution of cat images, down to the last pixel. Four families do this in different ways: autoregressive models, VAEs, GANs and diffusion models. Each one buys something (exact likelihoods, a latent space, one-shot sampling, stable training) by giving something else up.

What you’ll learn

  • Compare the generative families by what they offer: exact or bounded likelihood, sampling cost, and a latent space.
  • Explain how masked convolutions make a PixelCNN's exact likelihood trainable in one pass while sampling stays sequential.
  • Derive the VAE's reparameterized gradient and the optimal GAN discriminator.
  • Derive the closed form of the diffusion forward process, write DDPM training and sampling as pseudocode, and relate noise prediction to the score.
  • Compute FID for Gaussian features and state its main caveats.

Three jobs: density, sampling, latents

A generative model pθp_\theta is fitted to samples x1,…,xn\mathbf x_1,\dots,\mathbf x_n from an unknown pdatap_{\text{data}}. You might want three different things from it:

  • Density: evaluate pθ(x)p_\theta(\mathbf x) for a given x\mathbf x, for compression, anomaly detection or comparing models on held-out data.
  • Sampling: draw new x∼pθ\mathbf x\sim p_\theta quickly.
  • Latents: a code z\mathbf z to interpolate, edit or condition on, which makes the model useful as a representation.

No family does all three well, and the trade-offs organize the field.

The default way to fit a density is maximum likelihood: maximize 1n∑ilog⁡pθ(xi)\frac1n\sum_i\log p_\theta(\mathbf x_i). In expectation that is the same as minimizing the forward KL divergence DKL(pdata ∥ pθ)\KL(p_{\text{data}}\,\|\,p_\theta), a mass-covering objective that punishes any data the model misses (Information Theory for AI). It requires computing pθ(x)p_\theta(\mathbf x). The families differ in how they deal with that: they make the likelihood tractable (autoregressive models, flows), bound it (VAEs, diffusion), or give it up (GANs).

Familypθ(x)p_\theta(\mathbf x)Cost of one sampleLatent codeTypical weakness
Autoregressive (PixelCNN, language models)exactdd sequential network passesnoneslow sampling; a fixed ordering
Normalizing flowexactone passz\mathbf z of the same dimension as x\mathbf xinvertible architectures only
VAElower bound (ELBO)one passlow-dimensional z\mathbf z with an encoderblurry samples
GANnoneone passz\mathbf z, but no encoderunstable training, mode collapse
Diffusionlower bound; exact through an ODEtens to a thousand passesnoise of the same dimension (through the ODE)slow sampling, much reduced by better samplers
Flows (exact likelihood by change of variables) are covered in Density Estimation. This lesson takes the other four in turn.
Check your understanding

You need to rank several trained models by the probability they assign to a held-out test image. Which model cannot report that number, even approximately?

  1. A PixelCNN
  2. A normalizing flow
  3. A VAE
  4. A GAN
Show the answer

D. A GAN

A GAN's generator pushes noise through a network and defines no density at all; typically its samples even lie on a low-dimensional surface, where a density does not exist. PixelCNNs and flows give exact log-likelihoods, and a VAE gives a lower bound that importance sampling can tighten.

Autoregressive models: exact likelihood, one pixel at a time

The chain rule of probability holds for every distribution and every ordering of the coordinates. Model each conditional with a network and the log-likelihood is exact: a sum of dd cross-entropies, as in a language model (From N-grams to Embeddings).

pθ(x)=∏i=1dpθ(xi∣x1,…,xi−1),bits per dimension=−log⁡2pθ(x)dp_\theta(\mathbf x)=\prod_{i=1}^{d}p_\theta(x_i\mid x_1,\dots,x_{i-1}),\qquad \text{bits per dimension}=\frac{-\log_2p_\theta(\mathbf x)}{d}
Bits per dimension is the standard likelihood score for images: lower is better, and 8 bits is what storing raw 8-bit pixels costs.

PixelCNN (van den Oord, Kalchbrenner & Kavukcuoglu, 2016) orders the pixels in raster order and the color channels R, G, B within each pixel, and predicts each subpixel with a 256-way softmax. The trick that makes training fast is the masked convolution: every kernel weight at or after the current position in that order is zeroed. Mask A, in the first layer, also hides the current subpixel; mask B, in later layers, lets a position see its own features. No output can see its own value or any later one, so a single forward pass over a real image computes all dd conditionals at once.

Stacked masked convolutions leave a "blind spot" of pixels above and to the right that never reach the output; the gated PixelCNN (van den Oord et al., 2016) removed it with separate vertical and horizontal stacks.

text
PIXELCNN-SAMPLE(network f, height H, width W)
  x ← array of zeros, shape H × W × 3
  for i = 1..H:
      for j = 1..W:
          for c in (R, G, B):
              probs ← softmax(f(x)[i, j, c])     // 256 values; depends only on earlier subpixels
              x[i, j, c] ← a draw from probs
  return x

Training runs ff once per image, because the real image supplies every input. Sampling runs it once per subpixel, because each input is generated by the previous step. Caching activations reduces the cost of each step but not the number of sequential steps.

Predict before you look

How many sequential network evaluations does PIXELCNN-SAMPLE need for one 32×3232\times32 RGB image?

Show the answer

3072

One per subpixel: 32×32×3=307232\times32\times3=3072. Training on the same image takes a single parallel pass. The same asymmetry, parallel training and sequential sampling, holds for WaveNet audio (Speech & Audio) and for every autoregressive language model.

Latent variables: the variational autoencoder

A latent-variable model draws a code first and then the data: z∼p(z)=N(0,I)\mathbf z\sim p(\mathbf z)=\mathcal N(\mathbf 0,I), then x∼pθ(x∣z)\mathbf x\sim p_\theta(\mathbf x\mid\mathbf z), with a neural decoder. Its likelihood pθ(x)=∫pθ(x∣z) p(z) dzp_\theta(\mathbf x)=\int p_\theta(\mathbf x\mid\mathbf z)\,p(\mathbf z)\,d\mathbf z is an intractable integral. The variational autoencoder (Kingma & Welling, 2014; Rezende, Mohamed & Wierstra, 2014) adds an encoder qϕ(z∣x)=N(μϕ(x),diag⁡σϕ2(x))q_\phi(\mathbf z\mid\mathbf x)=\mathcal N\big(\boldsymbol\mu_\phi(\mathbf x),\diag\boldsymbol\sigma^2_\phi(\mathbf x)\big) and maximizes the evidence lower bound, derived with Jensen's inequality in Information Theory for AI:

log⁡pθ(x) ≥ Eqϕ(z∣x)[log⁡pθ(x∣z)]⏟reconstruction−DKL(qϕ(z∣x) ∥ p(z))⏟= 12∑j(μj2+σj2−1−log⁡σj2)\log p_\theta(\mathbf x)\ \ge\ \underbrace{\E_{q_\phi(\mathbf z\mid\mathbf x)}\big[\log p_\theta(\mathbf x\mid\mathbf z)\big]}_{\text{reconstruction}}-\underbrace{\KL\big(q_\phi(\mathbf z\mid\mathbf x)\,\|\,p(\mathbf z)\big)}_{=\ \frac12\sum_j\left(\mu_j^2+\sigma_j^2-1-\log\sigma_j^2\right)}
The gap is DKL(qϕ(z∣x) ∥ pθ(z∣x))\KL\big(q_\phi(\mathbf z\mid\mathbf x)\,\|\,p_\theta(\mathbf z\mid\mathbf x)\big), so raising the bound also improves the encoder. The KL to a standard normal has the closed form shown, which pulls the codes toward the prior, so that decoding a random z∼N(0,I)\mathbf z\sim\mathcal N(\mathbf 0,I) gives a plausible sample.
DerivationThe reparameterization trick
  1. z=μϕ(x)+σϕ(x)⊙ε,ε∼N(0,I)\mathbf z=\boldsymbol\mu_\phi(\mathbf x)+\boldsymbol\sigma_\phi(\mathbf x)\odot\boldsymbol\varepsilon,\qquad \boldsymbol\varepsilon\sim\mathcal N(\mathbf 0,I)

    The reconstruction term is an expectation over a distribution that depends on ϕ\phi, so you cannot simply backpropagate through "draw z\mathbf z". Write the draw as a deterministic function of ϕ\phi and parameter-free noise:

  2. ∇ϕ Eqϕ[f(z)]=∇ϕ Eε[f(μϕ+σϕ⊙ε)]=Eε[∇ϕf(μϕ+σϕ⊙ε)]\nabla_\phi\,\E_{q_\phi}\big[f(\mathbf z)\big]=\nabla_\phi\,\E_{\boldsymbol\varepsilon}\big[f(\boldsymbol\mu_\phi+\boldsymbol\sigma_\phi\odot\boldsymbol\varepsilon)\big]=\E_{\boldsymbol\varepsilon}\big[\nabla_\phi f(\boldsymbol\mu_\phi+\boldsymbol\sigma_\phi\odot\boldsymbol\varepsilon)\big]

    Now the distribution being averaged over no longer depends on ϕ\phi, and the gradient moves inside the expectation (under mild regularity conditions):

  3. ∇ϕ^=∇ϕf(μϕ(x)+σϕ(x)⊙ε),E[∇ϕ^]=∇ϕ Eqϕ[f]\widehat{\nabla_\phi}=\nabla_\phi f\big(\boldsymbol\mu_\phi(\mathbf x)+\boldsymbol\sigma_\phi(\mathbf x)\odot\boldsymbol\varepsilon\big),\qquad \E\big[\widehat{\nabla_\phi}\big]=\nabla_\phi\,\E_{q_\phi}[f]

    A single draw of ε\boldsymbol\varepsilon gives an unbiased estimate, computed by ordinary backpropagation through ff, μϕ\boldsymbol\mu_\phi and σϕ\boldsymbol\sigma_\phi. Its variance is usually far lower than that of the score-function (REINFORCE) estimator f(z)∇ϕlog⁡qϕ(z)f(\mathbf z)\nabla_\phi\log q_\phi(\mathbf z), which uses only the value of ff and not its slope.

python
def vae_loss(x, encoder, decoder):
    mu, log_var = encoder(x)                      # parameters of q(z | x)
    eps = torch.randn_like(mu)
    z = mu + torch.exp(0.5 * log_var) * eps       # reparameterized draw
    x_hat = decoder(z)                            # mean of a Gaussian p(x | z)
    recon = ((x - x_hat) ** 2).sum(dim=1)         # -log p(x | z) up to scale and a constant
    kl = 0.5 * (mu**2 + log_var.exp() - 1 - log_var).sum(dim=1)
    return (recon + kl).mean()                    # negative ELBO, averaged over the batch

With a fixed-variance Gaussian decoder the reconstruction term is a squared error. That is why VAE samples tend to be blurry: under squared error the best output is the average of all images consistent with z\mathbf z. The Deep Layer lesson treats posterior collapse and the β\beta-VAE.

GANs: a generator, a critic and a game

A generative adversarial network (Goodfellow et al., 2014) gives up the likelihood altogether. A generator GG maps noise z∼p(z)\mathbf z\sim p(\mathbf z) to a sample G(z)G(\mathbf z), which defines a distribution pgp_g only implicitly. A discriminator D(x)∈(0,1)D(\mathbf x)\in(0,1) is trained to tell real samples from generated ones, and the generator is trained to fool it. The two play a zero-sum game:

min⁡Gmax⁡D  V(D,G)=Ex∼pdata[log⁡D(x)]+Ez∼p(z)[log⁡(1−D(G(z)))]\min_G\max_D\;V(D,G)=\E_{\mathbf x\sim p_{\text{data}}}\big[\log D(\mathbf x)\big]+\E_{\mathbf z\sim p(\mathbf z)}\Big[\log\big(1-D(G(\mathbf z))\big)\Big]
VV is the log-likelihood of a binary classifier that labels real data 1 and fakes 0. The discriminator maximizes it; the generator minimizes it.
DerivationThe optimal discriminator
  1. V(D,G)=∫[pdata(x)log⁡D(x)+pg(x)log⁡(1−D(x))] dxV(D,G)=\int\Big[p_{\text{data}}(\mathbf x)\log D(\mathbf x)+p_g(\mathbf x)\log\big(1-D(\mathbf x)\big)\Big]\,d\mathbf x

    Fix GG and write both expectations as integrals over x\mathbf x, with pgp_g the distribution of G(z)G(\mathbf z):

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

    DD can be chosen separately at each x\mathbf x. For a,b>0a,b>0, the function y↦alog⁡y+blog⁡(1−y)y\mapsto a\log y+b\log(1-y) is concave on (0,1)(0,1) with derivative a/y−b/(1−y)a/y-b/(1-y), which vanishes at y=a/(a+b)y=a/(a+b):

  3. max⁡DV(D,G)=2 JS(pdata ∥ pg)−log⁡4\max_D V(D,G)=2\,\mathrm{JS}\big(p_{\text{data}}\,\|\,p_g\big)-\log4

    Substituting DG∗D^*_G back gives the generator's objective. The Generative Adversarial Networks lesson proves that it is a Jensen–Shannon divergence, so its unique minimum is pg=pdatap_g=p_{\text{data}}, where D∗≡12D^*\equiv\tfrac12:

In practice DD and GG are networks updated in alternation by stochastic gradient steps, and the generator usually minimizes −log⁡D(G(z))-\log D(G(\mathbf z)) instead of log⁡(1−D(G(z)))\log\big(1-D(G(\mathbf z))\big). Both reward fooling DD, but early in training, when DD easily rejects fakes, the original loss gives almost no gradient. GANs sample in one forward pass and produce sharp images: StyleGAN (Karras, Laine & Aila, 2019) generated photorealistic 1024×10241024\times1024 faces. But training can oscillate instead of converging, and the generator can suffer mode collapse, covering a few modes of the data and ignoring the rest.

Predict before you look

Write the discriminator's output on a fake as D=σ(a)D=\sigma(a) with logit aa. Early in training, D(G(z))=0.01D(G(\mathbf z))=0.01. How many times larger is the gradient magnitude ∣∂/∂a∣\abs{\partial/\partial a} of the non-saturating loss −log⁡σ(a)-\log\sigma(a) than that of the original loss log⁡(1−σ(a))\log\big(1-\sigma(a)\big)?

Show the answer

99

ddalog⁡(1−σ(a))=−σ(a)=−0.01\frac{d}{da}\log\big(1-\sigma(a)\big)=-\sigma(a)=-0.01, while dda(−log⁡σ(a))=−(1−σ(a))=−0.99\frac{d}{da}\big(-\log\sigma(a)\big)=-\big(1-\sigma(a)\big)=-0.99: a ratio of 9999. The original loss saturates exactly when the generator is worst, which is when it most needs a signal.

Diffusion: learning to undo noise

A diffusion model (Sohl-Dickstein et al., 2015; Ho, Jain & Abbeel, 2020) fixes a forward process that destroys data gradually: a Markov chain that adds a little Gaussian noise at each of TT steps. DDPM used T=1000T=1000, with βt\beta_t rising linearly from 10−410^{-4} to 0.020.02.

q(xt∣xt−1)=N(1−βt xt−1, βtI),αt=1−βt,αˉt=∏s≤tαs.q(\mathbf x_t\mid\mathbf x_{t-1})=\mathcal N\big(\sqrt{1-\beta_t}\,\mathbf x_{t-1},\ \beta_tI\big),\qquad \alpha_t=1-\beta_t,\quad \bar\alpha_t=\prod_{s\le t}\alpha_s.

Nothing in the forward process is learned. The model learns the reverse: how to remove one step of noise.

DerivationJumping straight to step t
  1. xt=αt xt−1+1−αt εt=αtαt−1 xt−2+αt(1−αt−1) εt−1+1−αt εt\mathbf x_t=\sqrt{\alpha_t}\,\mathbf x_{t-1}+\sqrt{1-\alpha_t}\,\boldsymbol\varepsilon_t=\sqrt{\alpha_t\alpha_{t-1}}\,\mathbf x_{t-2}+\sqrt{\alpha_t(1-\alpha_{t-1})}\,\boldsymbol\varepsilon_{t-1}+\sqrt{1-\alpha_t}\,\boldsymbol\varepsilon_t

    Write one step with fresh noise εt∼N(0,I)\boldsymbol\varepsilon_t\sim\mathcal N(\mathbf 0,I), and unroll two steps:

  2. αt(1−αt−1)+(1−αt)=1−αtαt−1\alpha_t(1-\alpha_{t-1})+(1-\alpha_t)=1-\alpha_t\alpha_{t-1}

    A sum of independent zero-mean Gaussians is Gaussian, and the variances add:

  3. q(xt∣x0)=N(αˉt x0, (1−αˉt)I),xt=αˉt x0+1−αˉt εq(\mathbf x_t\mid\mathbf x_0)=\mathcal N\big(\sqrt{\bar\alpha_t}\,\mathbf x_0,\ (1-\bar\alpha_t)I\big),\qquad \mathbf x_t=\sqrt{\bar\alpha_t}\,\mathbf x_0+\sqrt{1-\bar\alpha_t}\,\boldsymbol\varepsilon

    So two steps act like one step with αtαt−1\alpha_t\alpha_{t-1}. By induction on tt:

  4. αˉT≈0⟹q(xT∣x0)≈N(0,I)\bar\alpha_T\approx0\quad\Longrightarrow\quad q(\mathbf x_T\mid\mathbf x_0)\approx\mathcal N(\mathbf 0,I)

    Training never has to simulate the chain: pick a random tt and noise the data in one shot. As αˉT→0\bar\alpha_T\to0, xT\mathbf x_T forgets x0\mathbf x_0; with DDPM's schedule, αˉ1000≈4×10−5\bar\alpha_{1000}\approx4\times10^{-5}.

A network εθ(xt,t)\boldsymbol\varepsilon_\theta(\mathbf x_t,t) is trained to predict the noise that was added, with a plain squared error. Why that is the right target: the best possible noise predictor is ε∗(xt,t)=−1−αˉt ∇log⁡qt(xt)\boldsymbol\varepsilon^*(\mathbf x_t,t)=-\sqrt{1-\bar\alpha_t}\,\nabla\log q_t(\mathbf x_t), a rescaled score of the noised data distribution qtq_t. That is Tweedie's formula, proved in Autoencoders & Latent Spaces: denoising and score estimation are the same problem. Training and sampling, as in Ho et al.'s Algorithms 1 and 2:

text
DDPM-TRAIN
  repeat
      x0 ← a training example;   t ← uniform on {1, …, T};   ε ← N(0, I)
      take a gradient step on  ‖ε − ε_θ(√ᾱ_t · x0 + √(1 − ᾱ_t) · ε, t)‖²
  until converged

DDPM-SAMPLE
  x ← a draw from N(0, I)                                   // x_T
  for t = T, T − 1, …, 1:
      z ← a draw from N(0, I) if t > 1, else 0
      x ← (x − β_t / √(1 − ᾱ_t) · ε_θ(x, t)) / √α_t + σ_t · z
  return x                                                  // x_0

σt2\sigma_t^2 is set to βt\beta_t or to β~t=βt(1−αˉt−1)/(1−αˉt)\tilde\beta_t=\beta_t(1-\bar\alpha_{t-1})/(1-\bar\alpha_t); Ho et al. found the two gave similar results. Sampling costs TT network evaluations, the price of diffusion. The last step adds no noise.

Think first Why not train one network to map pure noise to data in a single step, T=1T=1, with the same squared-error loss?

Trained with squared error, a one-step denoiser outputs E[x0∣xT]\E[\mathbf x_0\mid\mathbf x_T]. Since xT\mathbf x_T carries almost no information about x0\mathbf x_0, that is close to the mean of the whole dataset: one blurry average for every input. Small steps keep each reverse conditional q(xt−1∣xt)q(\mathbf x_{t-1}\mid\mathbf x_t) close to a Gaussian, so a Gaussian step with a learned mean is accurate. Large steps make the true conditional broad and multimodal, which is what the 5-step samples suffered from. Better samplers and distillation cut the number of steps to tens or even one or two (Diffusion Models).

Measuring quality, and open questions

How good is a generative model? Held-out likelihood (bits per dimension) is principled but available only for some families, and it can disagree with sample quality in both directions (Theis, van den Oord & Bethge, 2016). The standard sample-based score for images is the Fréchet Inception Distance (FID; Heusel et al., 2017). Embed real and generated images with an ImageNet-trained Inception-v3 network (2,048 features each), fit a Gaussian to each set of features, and compute

FID=∥μr−μg∥2+Tr⁡(Σr+Σg−2(ΣrΣg)1/2)\mathrm{FID}=\norm{\boldsymbol\mu_r-\boldsymbol\mu_g}^2+\Tr\Big(\Sigma_r+\Sigma_g-2\big(\Sigma_r\Sigma_g\big)^{1/2}\Big)
The squared 2-Wasserstein (Fréchet) distance between the two Gaussians (Dowson & Landau, 1982). Lower is better; identical feature statistics give 0.
Check your understanding

In one dimension FID reduces to (μr−μg)2+(σr−σg)2(\mu_r-\mu_g)^2+(\sigma_r-\sigma_g)^2. Real features are N(0,1)\mathcal N(0,1); a generator's features are N(0,0.25)\mathcal N(0,0.25), centered correctly but with half the standard deviation. What is the FID?

  1. 00
  2. 0.750.75
  3. 0.250.25
  4. 0.56250.5625
Show the answer

C. 0.250.25

σg=0.25=0.5\sigma_g=\sqrt{0.25}=0.5, so FID =0+(1−0.5)2=0.25=0+(1-0.5)^2=0.25. A generator that output the same feature every time (σg=0\sigma_g=0) would score 11: FID penalizes missing diversity, not only wrong averages.

Key takeaways

  • Generative models trade exact or bounded likelihood, sampling cost and latent structure against each other: autoregressive models and flows give exact likelihoods, VAEs and diffusion models give bounds, GANs give none.
  • Autoregressive models factor p(x)=∏ip(xi∣x<i)p(\mathbf x)=\prod_ip(x_i\mid x_{<i}); masked convolutions make PixelCNN's training one parallel pass, but sampling takes dd sequential steps.
  • VAEs maximize the ELBO with an encoder, the reparameterization z=μ+σ⊙ε\mathbf z=\boldsymbol\mu+\boldsymbol\sigma\odot\boldsymbol\varepsilon and a closed-form Gaussian KL. GANs play a minimax game whose optimal discriminator is pdata/(pdata+pg)p_{\text{data}}/(p_{\text{data}}+p_g).
  • Diffusion noises data with q(xt∣x0)=N(αˉtx0,(1−αˉt)I)q(\mathbf x_t\mid\mathbf x_0)=\mathcal N(\sqrt{\bar\alpha_t}\mathbf x_0,(1-\bar\alpha_t)I) and learns to predict the noise, which amounts to learning the score; sampling reverses the chain in many small steps.
  • FID is the 2-Wasserstein distance between Gaussian fits to Inception features. It depends on the number of samples and on implementation details, and mixes fidelity with diversity.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

In a DDPM, αˉt=0.36\bar\alpha_t=0.36 at step tt. How is xt\mathbf x_t generated from a data point x0\mathbf x_0 and fresh noise ε∼N(0,I)\boldsymbol\varepsilon\sim\mathcal N(\mathbf 0,I)?

  1. xt=0.36 x0+0.64 ε\mathbf x_t=0.36\,\mathbf x_0+0.64\,\boldsymbol\varepsilon
  2. xt=0.6 x0+0.4 ε\mathbf x_t=0.6\,\mathbf x_0+0.4\,\boldsymbol\varepsilon
  3. xt=0.6 x0+0.8 ε\mathbf x_t=0.6\,\mathbf x_0+0.8\,\boldsymbol\varepsilon
  4. xt=x0+0.8 ε\mathbf x_t=\mathbf x_0+0.8\,\boldsymbol\varepsilon
Show the answer

C. xt=0.6 x0+0.8 ε\mathbf x_t=0.6\,\mathbf x_0+0.8\,\boldsymbol\varepsilon

q(xt∣x0)=N(αˉt x0,(1−αˉt)I)q(\mathbf x_t\mid\mathbf x_0)=\mathcal N\big(\sqrt{\bar\alpha_t}\,\mathbf x_0,(1-\bar\alpha_t)I\big), so the coefficients are 0.36=0.6\sqrt{0.36}=0.6 and 0.64=0.8\sqrt{0.64}=0.8. Their squares add to 11, which keeps the variance at 11 for unit-variance data: the process is variance preserving.

Question 2

At some point x\mathbf x, the data density is three times the generator's: pdata(x)=3 pg(x)p_{\text{data}}(\mathbf x)=3\,p_g(\mathbf x). What does the optimal discriminator output there?

  1. 1/31/3
  2. 3/43/4
  3. 1/21/2
  4. 11
Show the answer

B. 3/43/4

D∗(x)=pdata(x)pdata(x)+pg(x)=33+1=34D^*(\mathbf x)=\dfrac{p_{\text{data}}(\mathbf x)}{p_{\text{data}}(\mathbf x)+p_g(\mathbf x)}=\dfrac{3}{3+1}=\dfrac34: it is the posterior probability that x\mathbf x is real when real and fake samples are equally common.

Question 3

Why does the VAE write z=μϕ(x)+σϕ(x)⊙ε\mathbf z=\boldsymbol\mu_\phi(\mathbf x)+\boldsymbol\sigma_\phi(\mathbf x)\odot\boldsymbol\varepsilon instead of drawing z∼qϕ(z∣x)\mathbf z\sim q_\phi(\mathbf z\mid\mathbf x) directly?

  1. It makes the encoder deterministic, so no sampling happens during training
  2. It turns the sample into a differentiable function of ϕ\phi, so backpropagation yields an unbiased, low-variance gradient of the ELBO
  3. It makes the KL term vanish
  4. It is needed to evaluate pθ(x)p_\theta(\mathbf x) exactly
Show the answer

B. It turns the sample into a differentiable function of ϕ\phi, so backpropagation yields an unbiased, low-variance gradient of the ELBO

The randomness moves into ε\boldsymbol\varepsilon, whose distribution does not depend on ϕ\phi, so ∇ϕE[f(z)]=E[∇ϕf(μϕ+σϕ⊙ε)]\nabla_\phi\E[f(\mathbf z)]=\E[\nabla_\phi f(\boldsymbol\mu_\phi+\boldsymbol\sigma_\phi\odot\boldsymbol\varepsilon)], which one sample estimates without bias.

Question 4

A PixelCNN evaluates the likelihood of a 32×3232\times32 RGB training image in one forward pass, yet needs 3,072 passes to sample one. What makes training parallel?

  1. Training uses a smaller network
  2. The masked convolutions make each output depend only on earlier subpixels, and during training all of those are available from the real image
  3. Training ignores the dependence between pixels
  4. The softmax over 256 values is computed in closed form
Show the answer

B. The masked convolutions make each output depend only on earlier subpixels, and during training all of those are available from the real image

Every conditional p(xi∣x<i)p(x_i\mid x_{<i}) needs only earlier subpixels, which the training image supplies, so the masks let one pass compute all of them (teacher forcing). At sampling time each earlier subpixel must be generated first.

Question 5

Paper A reports FID 12 computed from 5,000 samples; paper B reports FID 10 from 50,000 samples, on the same dataset. What can you conclude?

  1. Model B is better, because its FID is lower
  2. Model A is better, because it reached a good score with fewer samples
  3. Little: FID's finite-sample bias depends on the number of samples (fewer samples inflate it), so the numbers are not comparable
  4. The models are equivalent, since the scores differ by less than 20%
Show the answer

C. Little: FID's finite-sample bias depends on the number of samples (fewer samples inflate it), so the numbers are not comparable

Estimating means and covariances from nn samples biases FID upward by an amount that shrinks with nn, and preprocessing details also shift scores. Comparisons need the same nn, the same reference statistics and the same implementation.

Go deeper

Further reading

  • I. Goodfellow, Y. Bengio & A. Courville, Deep Learning, ch. 20 "Deep Generative Models" (MIT Press, 2016).
  • A. van den Oord, N. Kalchbrenner & K. Kavukcuoglu, "Pixel recurrent neural networks," ICML (2016).
  • M. Heusel, H. Ramsauer, T. Unterthiner, B. Nessler & S. Hochreiter, "GANs trained by a two time-scale update rule converge to a local Nash equilibrium," NeurIPS (2017).
  • K. P. Murphy, Probabilistic Machine Learning: Advanced Topics, part IV "Generation" (MIT Press, 2023).