4.5.5 · D2Generative Models

Visual walkthrough — ELBO objective and KL term

2,203 words10 min readBack to topic

This page rebuilds the ELBO one drawing at a time. We start with nothing but the idea of a probability and end holding the full training loss of a VAE. Every symbol is earned before it is used. If you have never seen an integral or the letter , start at line one — you will be fine.


Step 0 — The characters and their pictures

Before any equation, meet the cast. A distribution is just a shape that says "how likely is each value?" — a hill over a number line, where taller = more likely, and the total area under the hill is exactly (something must happen).

The whole game: we want to make the data likely, but the true code-belief is out of reach, so we hire a stand-in .

Figure — ELBO objective and KL term

Step 1 — The ceiling we cannot reach

WHAT. We want to push up , the log-probability the model assigns to the real data. Why the ? Because probabilities of a whole dataset multiply, and turns products into sums — sums are what optimizers like.

WHY it's hard. The model only defines through a code: to get alone we must add up every possible code, The is "sum over all codes " — a continuous sum. For a neural decoder there is no formula for this sum, and there are infinitely many codes. So is a real number we can name but never compute.

PICTURE. A ceiling drawn as a dashed line high above. We can point at it but not touch it — so we will build a tower underneath and read the tower's height instead.

Figure — ELBO objective and KL term

Step 2 — Smuggle in the encoder (multiply by 1)

WHAT. Inside the sum we multiply by — which equals , so nothing changes: Here is the joint — the decoder and prior glued together.

WHY. A sum of "(shape ) times (something)" is exactly the definition of an average over . So the right side is We chose because it is a shape we built and can sample from — that is what makes the average estimable later. This is the heart of variational inference.

PICTURE. The ratio is a bumpy curve; sprinkling sample codes from and reading the curve at each dot approximates the average — we could never do that with the raw integral of Step 1.

Figure — ELBO objective and KL term

Step 3 — Jensen's inequality: slide the log inside

WHAT. We move from outside the average to inside:

WHY this tool and not another. is concave — its graph bulges upward like a dome, every chord lies below the curve. For any concave curve, "the curve read at the average input" sits above "the average of the curve's outputs". That is Jensen's inequality, and it is the one tool that lets a jump inside an while promising the result only ever gets smaller. Smaller is exactly what we want: a guaranteed lower bound.

PICTURE. Two points on the log-dome; the straight chord between them is the "average of the logs", the dome above the chord's midpoint is the "log of the average". The dome is higher — so , the gap is the bulge.

Figure — ELBO objective and KL term

The right side is our tower — the ELBO:


Step 4 — Split the tower into Reconstruction and KL

WHAT. Expand and regroup:

WHY. Two jobs fall out cleanly:

  • (A) rewards codes that let the decoder rebuild — high when the reconstruction is faithful.
  • (B) is the KL divergence: the average extra surprise of using 's codes while pretending they came from the prior . It pulls back toward the bell curve, the regularizer.

PICTURE. A tug-of-war rope: reconstruction pulls toward wherever the data-specific code lives; KL pulls it back toward the prior at . Their balance point is the trained encoder.

Figure — ELBO objective and KL term

Step 5 — What the gap actually is

WHAT. How far below the ceiling does the tower stop? We track it in four small, honest moves. Start from Bayes' rule (the definition of the true posterior from Step 0), take logs:

Move 1 — average both sides over the encoder. The left side has no in it, so averaging leaves it untouched; the right side becomes three expectations: Why average? Because our whole objective already lives as an average over ; putting the identity in the same currency lets us match terms.

Move 2 — add and subtract . This adds zero, so the value is unchanged — but it plants the we need to build two KL divergences (each KL is a difference of two logs averaged over ).

Move 3 — group the two KL-shaped pieces. The middle three terms are exactly , and the last two are :

Move 4 — read it.

WHY it matters. The missing height is exactly the KL between our stand-in and the true posterior . Because KL is never negative, the tower is never above the ceiling — the bound is honest. And it names our two levers precisely.

PICTURE. Ceiling = ; tower top = ; the shaded strip between them = the gap-KL. Squeeze that strip to zero and tower meets ceiling.

Figure — ELBO objective and KL term

Step 6 — Making the terms concrete: Gaussian encoder and decoder

WHAT. To turn everything into numbers we must pick actual shapes for all three players:

  • Prior: the plain bell curve .
  • Encoder: a bell curve shifted and stretched per data point, . The network reads and outputs two lists: (where each code-axis is centred) and (how wide/uncertain each axis is). "" means axes are independent, so the KL is a sum over axes.
  • Decoder: a bell curve over the -dimensional data, centred at the network output with unit variance, .

WHY the decoder shape matters (this fixes the reconstruction term). A -dimensional unit-variance Gaussian has log-density The first piece is negative squared reconstruction error (rewards a faithful rebuild); the second piece is a constant — it does not depend on or , so it never changes the gradient, which is why it is often dropped in code. But it is part of the true log-likelihood, so when we report an actual ELBO number we must keep it.

WHY the Gaussian KL has a closed form. With two bell curves the KL integral solves exactly — no sampling. Per axis , starting from and using and : Read the terms: punishes drifting from the prior's centre ; punishes being wider than the prior; explodes as , forbidding a razor-thin encoder; the makes the whole thing exactly when (encoder equals prior).

PICTURE. Each axis draws two bells: fixed prior (centre , width ) versus encoder (, ). The KL is the total mismatch; it bottoms out at zero only when the two bells coincide.

Figure — ELBO objective and KL term

Now the edge cases the reader might hit:

  • Perfect encoder (): gap-KL , tower touches ceiling, . The dream case.
  • No KL term at all (drop (B)): reconstruction pushes each to its own private, spiky code; the latent space becomes discontinuous — useless for sampling.
  • KL dominates ( for every ): KL but codes carry no information about — the decoder ignores , another face of posterior collapse. This is the information-bottleneck pinched shut.
  • Loose bound (big gap-KL): still valid but far below ; tighten with richer — e.g. flows or importance-weighted bounds.
  • (encoder certain): , KL explodes — the objective forbids a zero-variance encoder, a built-in safety rail.

Worked numbers (matching the parent)


The one-picture summary

Figure — ELBO objective and KL term
Recall Feynman retelling — say it back in plain words

We wanted to make the data likely, but the honest score needs an impossible sum over every hidden code. So we hired a stand-in encoder we can sample from, snuck it into the sum as a disguised , and used Jensen's rule (log-dome sits above its chords) to slide the log inside — turning the impossible score into a computable tower, the ELBO, guaranteed to sit below the true ceiling. The tower splits into two forces: reconstruction pulling codes toward whatever rebuilds (a Gaussian decoder makes this negative squared error plus a fixed constant), and KL pulling them back toward the bell-curve prior. When both encoder and prior are Gaussians the KL becomes a tidy sum . The height still missing from the ceiling is exactly how wrong our stand-in is — the KL to the true posterior . Close that gap and the tower kisses the ceiling. Kill the KL and codes go feral; let KL win and codes forget the data; drive and the KL screams infinity — so the math itself keeps us in the sweet middle.

Recall Quick checks

Which inequality lets log jump inside the average, and why does it point down? ::: Jensen's; is concave so — a lower bound. The gap between ELBO and equals what? ::: . Which force stops each from grabbing a private spiky code? ::: The KL term pulling toward the prior . In the Gaussian KL formula, which term blows up as ? ::: . What shape did we assume for the decoder, and what does its log-density become? ::: ; its log-density is .