Part XIII — Generative Models: VAEs, Flow Matching, Score Models & Diffusion Transformers · Chapter 2

Variational Autoencoders (VAE)

Hook

An ordinary autoencoder compresses an input down to one latent point and decodes it back — the same input always produces the exact same output. What if the bottleneck held a whole neighborhood of possible points instead of just one, so decoding could generate variations you never actually saw?

Intuition

The same input, encoded to the same distribution, decoded through a different noise draw each time. One of those draws reproduces the input exactly — the rest land nearby, at points the network never directly trained on.

Formalize

A variational autoencoder encodes xx to a mean μ\mu and log-variance logσ2\log\sigma^2 instead of a single point, then samples via the reparameterization trick:

z=μ+σε,εN(0,1)z = \mu + \sigma \cdot \varepsilon, \qquad \varepsilon \sim \mathcal{N}(0, 1)
  • zz — the sampled latent vector, passed to the decoder.
  • μ\mu — the mean of the encoded distribution, output by the encoder.
  • σ\sigma — the standard deviation of the encoded distribution (derived from logσ2\log\sigma^2).
  • ε\varepsilon — an external noise term, drawn from a standard normal and independent of the network's parameters.
  1. Sampling becomes a deterministic function of noise

    Writing sampling as a function of μ\mu, σ\sigma, and an external noise term ε\varepsilon keeps the whole pipeline differentiable, instead of asking gradients to pass through a random draw directly.

  2. Gradients still reach the encoder

    Gradients flow through μ\mu and σ\sigma even though zz itself is random — the randomness is isolated entirely in ε\varepsilon, which needs no gradient at all.

A second loss term, the KL divergence — short for Kullback–Leibler divergence — to the standard normal, keeps every input's distribution from collapsing to a single point or drifting off to some far corner of latent space:

DKL(N(μ,σ2)N(0,1))=12(σ2+μ21logσ2)D_{KL}\big(\mathcal{N}(\mu,\sigma^2) \,\|\, \mathcal{N}(0,1)\big) = \tfrac{1}{2}\left(\sigma^2 + \mu^2 - 1 - \log\sigma^2\right)
  • DKL(N(μ,σ2)N(0,1))D_{KL}\big(\mathcal{N}(\mu,\sigma^2)\,\|\,\mathcal{N}(0,1)\big) — the KL divergence between the encoded distribution and the standard normal target; the regularization term added to the reconstruction loss.
Play

Four different noise draws, one shared input. The mean alone decodes back to exactly the original value — but every other draw lands at a distinct nearby point, none of them ever explicitly trained on. That spread is the entire point: the decoder has learned a neighborhood, not a single memorized answer.

Worked example

Encoding x=2x=2 to μ=1\mu=1, logσ2=1.8\log\sigma^2=-1.8 (so σ0.4066\sigma\approx 0.4066), with a fixed sequence of stand-in noise values ε{0,1,1,2}\varepsilon \in \{0, 1, -1, 2\}:

  1. No noise reproduces the input exactly

    ε=0\varepsilon=0 gives z=μ=1z=\mu=1, and decoding zz back through x^=2z\hat{x}=2z gives exactly 22 — the original input, recovered perfectly when there's no randomness at all.

  2. Every other draw lands somewhere new
    • ε=1\varepsilon=1: z=1+0.4066(1)1.4066x^=2z2.8131z=1+0.4066(1)\approx1.4066\Rightarrow\hat x=2z\approx2.8131
    • ε=1\varepsilon=-1: z=1+0.4066(1)0.5934x^1.1869z=1+0.4066(-1)\approx0.5934\Rightarrow\hat x\approx1.1869
    • ε=2\varepsilon=2: z=1+0.4066(2)1.8131x^3.6263z=1+0.4066(2)\approx1.8131\Rightarrow\hat x\approx3.6263

    Four genuinely different outputs from one input, one encoder, one decoder.

  3. KL divergence penalizes distributions that stray from standard normal

    Plugging into DKL=12(σ2+μ21logσ2)D_{KL}=\tfrac12(\sigma^2+\mu^2-1-\log\sigma^2):

    • Encoding x=2x=2: μ=1\mu=1, σ2=e1.80.1653\sigma^2=e^{-1.8}\approx0.1653, so DKL=12(0.1653+11(1.8))=12(1.9653)0.983D_{KL}=\tfrac12(0.1653+1-1-(-1.8))=\tfrac12(1.9653)\approx0.983.
    • Encoding x=0x=0 instead: μ=0\mu=0, logσ2=2\log\sigma^2=-2, σ2=e20.1353\sigma^2=e^{-2}\approx0.1353, so DKL=12(0.1353+01(2))=12(1.1353)0.568D_{KL}=\tfrac12(0.1353+0-1-(-2))=\tfrac12(1.1353)\approx0.568.

    The second is a smaller KL divergence, closer to μ=0,σ=1\mu{=}0,\sigma{=}1, the one distribution where this penalty vanishes entirely.

Checkpoint

Find the input, among the three candidates, whose encoded distribution sits closest to the standard normal (lowest KL divergence).

Pick an input to try it
Summary
z=μ+σε,L=reconstruction loss+DKL(N(μ,σ2)N(0,1))z = \mu + \sigma\varepsilon, \qquad \mathcal{L} = \text{reconstruction loss} + D_{KL}\big(\mathcal{N}(\mu,\sigma^2)\,\|\,\mathcal{N}(0,1)\big)

Replacing a single latent point with a distribution — and regularizing that distribution toward the standard normal — turns an autoencoder from a compression scheme into a generative one. Sample a zz from anywhere reasonable in that latent space, even a point no training input ever mapped to, and the decoder still produces something plausible. The next chapter applies a very different training signal to sequences: masking out pieces of the input and asking a model to reconstruct exactly what's missing.