Exercises — ELBO objective and KL term
This page is a self-test. Each problem states the setup cleanly, then hides a full worked solution inside a collapsible callout. Work it first, then reveal. Problems climb from "can you spot the definition" up to "can you invent a variant". Everything you need was built in the parent ELBO objective and KL term note; if a symbol appears here you have not met, it is defined in that note or below.
Before we start, one shared cheat-sheet of the objects you will keep reusing.
The picture below is the mental model behind every exercise on this page: the true evidence is a ceiling we cannot reach, the ELBO is a tower we build from below, and the space between them is a KL "gap". Keep glancing back at it — L1–L3 raise the tower, L4 measures the gap, L5 shrinks it.

Figure s01 walkthrough. Read the figure like this: the magenta bar is the number you actually compute and maximise (); the violet slab stacked on top of it is the gap you can never see directly; the navy horizontal line is the unreachable ceiling . The two coloured pieces always sum to the navy line — that is the whole point. So the orange arrow ("build tower up") and the violet arrow ("gap shrinks") describe one motion, not two: pushing the magenta bar higher must thin the violet slab, because their total is pinned to the ceiling. That single fact is exactly what Exercise 4.1 proves algebraically.
Level 1 — Recognition
Exercise 1.1
Which of these is the ELBO decomposition, and which term is a penalty (subtracted)? Here is the KL from the encoder to the prior .
Recall Solution
(b) is the ELBO. The KL term is subtracted, so it is the penalty. Reason: we want the encoder blob to stay close to the prior ; the larger the KL (the more it drifts), the more we shave off the objective. Choice (a) would reward drifting, which is backwards. Answer ::: (b), and is the subtracted penalty.
Exercise 1.2
State whether each is always true: (i) ; (ii) ; (iii) .
Recall Solution
(i) True — Gibbs' inequality; KL is a "wasted-bits" count, never negative. (ii) False — KL is not symmetric. VAEs use the reverse KL because we take expectations under , the distribution we can sample from. (iii) True — that is why it is called a lower bound. The parent note proves , and the extra KL is .
Level 2 — Application
Exercise 2.1
Encoder outputs , . Compute against .
Recall Solution
Per dimension: . Sum over 2 dims and halve: . Value ::: This is the only configuration giving KL : the encoder blob equals the prior exactly.
Exercise 2.2
Encoder outputs , . Compute .
Recall Solution
Dim 1: . (Here .) Dim 2: . Sum , halve . Value :::
Exercise 2.3
Same encoder as 2.2. Recall the decoder model stated in the cheat-sheet: , a unit-variance Gaussian whose mean is the network's reconstruction of . A sample decodes to , and the true data is . Compute the reconstruction term (drop the additive constant), then the single-sample ELBO estimate.
Recall Solution
Why squared error? For a unit-variance Gaussian, . The first piece is a constant (does not depend on ), so we drop it, leaving — plain negative squared reconstruction error. . Reconstruction . ELBO estimate . ELBO :::
Level 3 — Analysis
Exercise 3.1
For a single latent dimension with fixed , which minimises the KL? Show it, explain the shape, and describe what happens numerically as .
Recall Solution
. Differentiate w.r.t. : . Set . Second derivative , and at this is : a minimum. At , KL . Shape: a valley — squeezing () blows up via , spreading () blows up via . The prior's own width is the sweet spot. Figure s02 (below) plots exactly this valley; check that your minimum lands on the orange dashed line at . Edge case : the term , so the KL diverges — a spike-like encoder is infinitely penalised. Numerically this is dangerous: as , overflows and (in the gradient) blows up, causing NaNs. This is exactly why implementations parametrise the network to output (or ) directly instead of — the log stays finite and differentiable, and can never hit zero. Optimal sigma ::: , KL there ; as the KL .

Figure s02 walkthrough. The magenta curve is the single-dimension KL as a function of the encoder width (with ). Trace it left-to-right: on the left the curve rockets upward — that is the term punishing an over-narrow ("spike") encoder; on the right it climbs again — that is the term punishing an over-wide one. The violet dot sits at the bottom of the valley, and the orange dashed vertical line marks : the KL is exactly there, confirming algebraically that the prior's own width is the unique minimiser.
Exercise 3.2
The total loss is . Suppose we scale the KL by (a "-VAE"): loss . Argue what happens as , and connect it to a failure mode named in the parent note.
Recall Solution
As the KL term dominates. The cheapest way to drive KL to is for every — the encoder ignores the input entirely. Then carries no information about , the decoder must reconstruct from noise, and reconstruction rots. This is Posterior Collapse: independent of . The KL term, meant as a gentle regulariser, becomes a wrecking ball when over-weighted. One-line answer ::: encoder collapses to the prior for all — posterior collapse.
Level 4 — Synthesis
Exercise 4.1
Prove the gap identity starting from Bayes' rule, and use it to argue why maximising over tightens the bound.
Recall Solution
Bayes: . Take logs and solve for the evidence: This holds for every ; take of both sides (the left side has no , so it is unchanged): \log p_\theta(x)=\mathbb{E}_q[\log p_\theta(x|z)]+\mathbb{E}_q[\log p(z)]-\mathbb{E}_q[\log p_\theta(z|x)].\tag{1} The strategic goal (WHY the next move). We are aiming to reshape line (1) into "ELBO + gap". Look at what each target piece needs: the ELBO contains , and the gap . Both need an term — one with a minus sign, one with a plus. But line (1) contains no at all. The only way to conjure a and a without changing the value is to add zero in the form . That is not a trick pulled from a hat — it is the forced consequence of wanting two KL divergences to appear. Now hand the first to the prior term, and the second to the posterior term: Group A: , so group A is exactly the ELBO . Group B: , the gap. Hence Since is fixed w.r.t. , and the gap , pushing up must push the gap down — so optimising makes approach the true posterior. This is exactly Variational Inference.
Exercise 4.2
The Expectation Maximization algorithm alternates an E-step and M-step. Frame both as operations on .
Recall Solution
- E-step = maximise over (the approximation) with fixed. With a flexible this sets , closing the gap so .
- M-step = maximise over (the model) with fixed, raising the tightened bound. VAEs do a soft version: instead of an exact E-step they train an amortised encoder by gradient ascent, jointly with .
Level 5 — Mastery
Exercise 5.1
Show that averaging the single-sample ELBO over importance samples, , is never looser than the ordinary ELBO (). Justify each step of the variance-to-tightness link. Name the model this defines.
Recall Solution
Let , i.i.d. with (so each weight is an unbiased estimator of the evidence). Define the -sample average , so for every , and . Step 1 — every is a valid bound. By Jensen for the concave : . So every lies below the same ceiling. Step 2 — monotone in (the crux). Take samples . A short combinatorial fact: the full average equals the average of the leave-one-out -sample averages, Because is concave, "log of an average average of the logs" gives Take of both sides; each leave-one-out term is a genuine -sample estimator, so its expectation is , and the right side averages copies of down to itself. Hence . Step 3 — the variance-to-tightness link, finished. The only looseness in Step 1 is the slack in Jensen's inequality, and that slack is governed entirely by how spread is around its fixed mean : for a fixed mean, the more concentrated the random variable, the smaller the gap between and — with equality exactly when is a constant. Averaging i.i.d. copies shrinks the variance, , so (a constant) and the Jensen slack . Therefore the bound rises all the way to the ceiling as : . Step 2 already made this rise monotone at every step rather than merely asymptotic, so the two arguments together give the full chain Name the model. This tighter multi-sample bound is the objective of the Importance Weighted Autoencoder (IWAE); see Importance Weighted Autoencoders. Ordinary VAE training is the special case . Name ::: Importance Weighted Autoencoder (IWAE) — the -sample ELBO bound; VAE is the case.
Exercise 5.2
Design a KL term for a non-Gaussian posterior. Suppose you replace the Gaussian encoder with a normalizing flow , . Write the ELBO's KL/entropy piece in terms of the flow's Jacobian, being explicit about normalization constants.
Recall Solution
The ELBO always equals . The second term is the negative entropy of . For a flow, the change-of-variables rule gives the exact log-density (constants kept, since here they depend on through and cannot be dropped): Note the contrast with Exercise 2.3: there we dropped the decoder's constant because it is independent of the parameters we optimise; here the Gaussian constant sits inside the entropy of and, while itself is still constant, the surrounding log-Jacobian is not, so we carry the full expression to avoid mistakes. Substituting, where . The closed-form Gaussian KL is replaced by this log-Jacobian entropy. Benefit: can now bend into non-Gaussian shapes, shrinking the gap KL against the true posterior — richer approximation, same ELBO scaffold. (If one only needs gradients, the constant may be dropped — but state that choice, do not assume it.)
Recall Self-test summary
What single configuration makes the Gaussian-vs-unit-prior KL exactly zero? ::: in every dimension. What quantity is the gap between ELBO and ? ::: — KL to the true posterior. As in a -VAE, what fails? ::: Posterior collapse — the encoder ignores . Does using importance samples change the target? ::: No — it only tightens the same lower bound on ; the model is the IWAE. Why parametrise the network to output instead of ? ::: To keep and avoid blow-ups / NaNs as .