4.5.3 · D2Generative Models

Visual walkthrough — Variational Autoencoders (VAE) theory

1,982 words9 min readBack to topic

This page rebuilds the single most important equation of the VAE — the Evidence Lower Bound — from absolutely nothing. No symbol appears before we draw it. If you have never seen an integral or a logarithm's tricks, start at line one and keep going.


Step 1 — What we actually want: the likelihood of the data

WHAT. Every generative model has a hidden dial we call — the latent code. Think of as a short list of numbers that means something ("a 7", "tilted left", "thick strokes"). The model turns into a picture . We write:

Read every symbol:

  • — one real data point (an image), a fixed thing we were handed.
  • — the hidden code, a point we are free to choose.
  • — the prior: how likely each code is before seeing any data. We pick this to be a bell curve, (see Latent Variable Models).
  • — the decoder: "given code , how likely is picture ?" The subscript is the bag of neural-network weights we will train.
  • — the integral: "add up over every possible code."

WHY the integral. A picture could have come from many codes. To score how likely is overall, we must weigh every code by how probable it is () times how well it explains (), then sum. That summing-over-all-possibilities is exactly what means.

PICTURE. The curve is . At each a red bar shows the product . The total shaded area under the red is .

Figure — Variational Autoencoders (VAE) theory

Step 2 — Why we cannot just compute it

WHAT. is not one number — it is a list of, say, 64 numbers. To do the integral by brute force we would chop each of the 64 axes into (say) 100 slices and add up boxes.

WHY it is hopeless. is more boxes than atoms in the universe. For all but toy problems the integral is intractable — we cannot evaluate it, and worse, we cannot differentiate it to train .

PICTURE. One axis of is easy to grid. Two axes: a grid of dots. The caption reminds you the real space has dozens of axes — the red dot is one lonely sample lost in an exponential ocean.

Figure — Variational Autoencoders (VAE) theory

Step 3 — Smuggle the encoder in without changing anything

WHAT. Start from the log of what we want. We take the logarithm because it turns the product-and-integral into sums, which are far easier to handle:

Now multiply the inside by , which equals :

Term by term:

  • — the encoder / guide: a bell curve over codes, centred on the good region for this particular . is its own set of weights.
  • Multiplying by changes nothing mathematically — but it lets us read the integral as an average with respect to .

WHY. "" is precisely the definition of an average of where each code is weighted by how often the guide would pick it. We write that average as :

The bracket is called the ratio . We will use in the next step.

PICTURE. Two curves: the wide prior in black, the narrow guide in red, hugging the region that explains . Instead of sampling everywhere, we will now sample only under the red hump.

Figure — Variational Autoencoders (VAE) theory

Step 4 — The one inequality: Jensen

WHAT. We have of an average, . We want to move the inside, giving . These are not equal — but there is a guaranteed direction:

(The subscript is essential: both averages are taken with respect to the encoder's distribution — that is the whole reason we smuggled it in.)

WHY log, and why this direction. The graph of is concave — it bends downward like a dome. For any dome-shaped function, the height above the average input is at least the average of the heights. That is Jensen's inequality. We use specifically because (a) it is concave so Jensen applies, and (b) it splits products into sums (Step 5). Applying it:

The right-hand side is a floor under . We name it — the Evidence Lower Bound, or ELBO.

PICTURE. The red dome is . Pick two points on the curve; the straight red chord between them lies below the curve. The chord's midpoint height = ; the curve's height above the mean input = . The gap between them is the slack in Jensen.

Figure — Variational Autoencoders (VAE) theory

Step 5 — Split the log into two meaningful jobs

WHAT. Use to break the ELBO apart:

The second average is, by definition, the negative KL divergence between guide and prior:

Term by term:

  • Reconstruction — sample a code from the guide, decode it, ask "did we get back?" High when the decoder is faithful.
  • Regulariser measures how far the guide's bell curve has drifted from the standard prior. Subtracting it punishes drift, keeping the latent space smooth and samplable (see Evidence Lower Bound (ELBO)).

WHY this split matters. These two terms pull against each other. Perfect reconstruction wants sharp, spread-out codes; the KL wants every code squashed toward the origin. VAE training is the tug-of-war between them. (When the KL wins too hard, the code is ignored — that failure is Posterior Collapse.)

PICTURE. A balance beam: left pan "reconstruction ↑", right pan "KL to prior ↓", the red pivot is we maximise.

Figure — Variational Autoencoders (VAE) theory

Step 6 — Exactly how big is the gap? (the degenerate/tight case)

WHAT. Jensen gave "" but hid by how much. We now name the gap precisely. Claim:

Derivation. Start from the KL between the guide and the true posterior , and use Bayes' rule :

Because has no in it, its average is itself, and the remaining three terms are exactly . So:

WHY this closes the loop. KL is never negative. So — a second, airtight proof that is a lower bound. And the gap vanishes exactly when the guide equals the true posterior, : the degenerate case where the bound is tight and we lose nothing.

PICTURE. A vertical stack: top level = (the ceiling), bottom level = (our floor), and the red bracket between them labelled . When the red bracket shrinks to zero, floor touches ceiling.

Figure — Variational Autoencoders (VAE) theory

Step 7 — The KL term has a clean closed form (so we can actually train)

WHAT. For a diagonal-Gaussian guide against prior in dimensions, the KL is a plain formula — no integral left:

Term by term for a single dimension:

  • — punishes the mean for straying from .
  • — punishes the spread for being wider than the prior's .
  • — the baseline so a perfect match costs .
  • — punishes the spread for collapsing to (too sharp).

WHY it matters. Because this is elementary arithmetic on the encoder's outputs , we can differentiate it and let gradients flow. Paired with the Reparameterization Trick for the reconstruction term, the whole ELBO becomes trainable by ordinary backprop.

Sanity / degenerate check. Set (guide equals prior): each summand is . Zero cost, as it must be.

PICTURE. The KL cost of one dimension as a red bowl over ; the bottom of the bowl sits exactly at with value , and the walls rise as you drift or collapse.

Figure — Variational Autoencoders (VAE) theory

The one-picture summary

The whole story on one canvas: the impossible ceiling , the guide we hired, Jensen giving us a floor , the floor splitting into reconstruction minus KL, and the red gap that shrinks to zero when the guide matches the true posterior.

Figure — Variational Autoencoders (VAE) theory
Recall Feynman retelling — say it like you mean it

We wanted the probability the model gives to a real picture. To get it you'd have to check every possible hidden code — there are astronomically many, so it's impossible. Trick: hire an encoder that, for each picture, points straight at the few codes that matter. Take a logarithm (turns messy products into sums and, being dome-shaped, lets us use Jensen's inequality). Jensen hands us a floor underneath the impossible number — the ELBO. Break the ELBO into two honest jobs: "rebuild the picture from its code" minus "keep the code's distribution close to a plain bell curve." Those two fight each other, and training balances them. Finally we proved the exact size of the gap between the impossible ceiling and our floor: it's the KL from our encoder to the true best guide, and it's zero when they match. Since KL can't be negative, our floor is always a real floor — so pushing it up drags the true likelihood up with it. And because the KL-to-the-prior part has a tiny closed-form formula, the whole thing trains with plain calculus.

Recall

What is intractable in ? ::: The integral over all high-dimensional codes — exponentially many to sum. Which property of powers Jensen's inequality here? ::: It is concave (dome-shaped), so . Name the two terms of the ELBO. ::: Reconstruction (expected ) minus KL divergence of the encoder from the prior. Exactly how big is the gap between and the ELBO? ::: , which is and zero when the guide equals the true posterior. What is the closed-form KL for vs per dimension? ::: .


Related: Reparameterization Trick · Kullback-Leibler Divergence · Evidence Lower Bound (ELBO) · Posterior Collapse · Conditional VAE · Normalizing Flows · Generative Adversarial Networks (GANs) · Disentangled Representations