4.5.5 · D3Generative Models

Worked examples — ELBO objective and KL term

1,869 words8 min readBack to topic

This page is a workshop. The parent note built the theory: the ELBO is a lower bound on , it splits into a reconstruction reward minus a KL penalty, and the gap to the true log-likelihood is itself a KL. Here we use those results on every kind of input the topic can hand you — normal cases, zero cases, degenerate cases, limits, a real word problem, and an exam twist.

Recall Symbols we will reuse (from the parent)
  • ::: the observed data point (e.g. a small vector)
  • ::: the hidden "latent code" a VAE invents to explain
  • ::: the encoder — our guessed distribution over given
  • ::: the prior — the "default" shape we want to follow
  • ::: the decoder — rebuilds from
  • ::: the ELBO
  • ::: a bell curve (Gaussian) centred at with spread

Everything below leans on one workhorse formula from the parent, so let us re-anchor it before spending it.


The scenario matrix

Every ELBO exercise you will meet is one of these cells. The examples afterward tick each cell.

# Cell class What makes it special Covered by
A Perfect match → KL should be Ex 1
B Off-centre, right width → only cost Ex 2
C Centred, wrong width → only spread cost Ex 3
D Degenerate collapse → KL (limit) Ex 4
E Full single-point ELBO reconstruction and KL together Ex 5
F Reverse-vs-forward KL asymmetry Ex 6
G Real-world word problem choosing , bits interpretation Ex 7
H Exam twist: the gap recover from ELBO + posterior KL Ex 8

Example 1 — Cell A: the perfect match

Forecast: guess the number before reading on. If is the prior, how many "extra bits" do you pay? Zero — write that down.

  1. Plug into the closed form. Each dimension contributes . Why this step? The formula is exact for diagonal Gaussians; no sampling needed.
  2. Evaluate one dimension. gives . Why this step? , so all four pieces cancel — this is the " was calibrated for exactly this case" moment.
  3. Sum over . .

Verify: the parent's property " iff a.e." demands exactly this.


Example 2 — Cell B: off-centre, correct width

Forecast: width is perfect, so the -and- pieces should self-cancel like in Ex 1. Only the terms survive. Guess: about .

  1. Kill the width terms. With : . Why this step? Isolates the pure "off-centre" penalty so we see it cleanly.
  2. Keep the mean terms. . Why this step? is the only surviving cost — it grows quadratically with drift, so being twice as far off costs four times as much.
  3. Compute. .

Verify: must be since ; and it equals half the squared distance of the mean from origin,


Example 3 — Cell C: centred but wrong width

Forecast: which is punished more — twice too wide, or twice too narrow? Guess before computing. (Hint: look at the term.)

Figure — ELBO objective and KL term
  1. Too wide, one dim. . Why this step? dominates; the gives back only a little.
  2. Two dims wide. .
  3. Too narrow, one dim. : . Why this step? Now is tiny but is large and positive — the narrowness penalty takes over.
  4. Two dims narrow. .

Verify: the two costs differ () — the KL is not symmetric in "wide vs narrow". Look at the amber curve in the figure: it is steeper on the narrow () side asymptotically but rises faster on the wide side for moderate deviations. Both


Example 4 — Cell D: degenerate collapse ()

Forecast: a deterministic encoder pins to a single point. Does the model like this? Guess whether KL stays finite.

  1. Isolate the dangerous term. Per dimension the KL contains . Why this step? As , , so .
  2. Take the limit. . Why this step? The finite pieces and are swamped; the term dominates.
  3. Interpret. The KL penalty forbids infinite sharpness. This is the mathematical guardrail against one failure mode — but note it does not prevent Posterior Collapse, which is the opposite pathology ( so the code carries no info).

Verify (numerically): at , , KL-per-dim ; at , , KL-per-dim — growing without bound.


Example 5 — Cell E: full single-point ELBO

Forecast: reconstruction is near-perfect (decoded point sits almost on ), so that term is . The KL is a genuine positive cost. Guess: ELBO .

  1. Reconstruction term. For a unit-variance Gaussian decoder, (plus a constant we drop). , so term . Why this step? The decoder's log-density is negative squared error for identity covariance — good reconstruction near-zero cost.
  2. KL term, dim 1. : . Why this step? because (slight narrowness cost).
  3. KL term, dim 2. : . Why this step? Here , so refunds some cost.
  4. Assemble KL. .
  5. ELBO. .

Verify: ELBO must be . It is a small negative number dominated by the KL, as forecast (sign right, magnitude reasonable). Recompute KL directly:


Example 6 — Cell F: reverse vs forward KL

Forecast: the parent said this is asymmetric. Guess whether swapping the arguments changes the number — yes — and which is bigger.

Use the general two-Gaussian formula (derive once, reuse):

  1. Forward . : . Why this step? This is the direction the ELBO actually optimises (expectation under the encoder ).
  2. Reverse . Swap: : . Why this step? Same two distributions, opposite roles — shows the numbers genuinely differ.

Verify: , confirming asymmetry. The reverse KL here is larger, which is why Variational Inference picks the mode-seeking forward-KL : expectations are taken under (which we can sample), giving the "zero-forcing" behaviour the parent described. See also Importance Weighted Autoencoders and Normalizing Flows for tighter alternatives.


Example 7 — Cell G: real-world word problem

Forecast: more = more pressure toward the prior = fewer bits used. Guess whether the objective goes up or down.

  1. State the objective. -VAE maximises . Why this step? The scalar re-weights the information-bottleneck trade-off between fidelity and compression.
  2. Nats → bits. nat bits, so nats bits. Why this step? KL measures "extra bits/nats" — the parent's coding interpretation. Reporting in bits makes the channel capacity legible.
  3. Weighted objective. . Why this step? This is the number gradient descent actually optimises; more negative because amplifies the penalty.

Verify: units — reconstruction and KL are both in nats, so the objective is in nats: nats. The bit figure satisfies nats.


Example 8 — Cell H: the exam twist (recover the true log-likelihood)

Forecast: the parent's gap identity is . Guess the log-likelihood before computing.

  1. Apply the gap identity. . Why this step? This is the exact decomposition from the parent — the gap is always a KL, hence , hence ELBO .
  2. Reconciliation check for part (b). New state: , gap , so . Why this step? True log-likelihood rose from to , and the approximation gap shrank from to .
  3. Answer. Yes — both (generative quality, higher ) and (tighter bound, smaller gap) improved. This is the dual role Expectation Maximization and VAE training play simultaneously.

Verify: consistency — in both states : and . The gap is non-negative in both, so no impossibility.