Before this lesson: the KL divergence and Jensen's inequality
(information theory, lesson 03); forward and reverse
KL (information theory, lesson 06);
Bayes' rule, maximum likelihood and the Gaussian density (the
probability and statistics track). Gaussian mixtures and EM appear in
the ML fundamentals track (not yet published), but this lesson explains what it needs. The ELBO is
also derived in information theory, lesson 09; here we use it to
train generative models. Time: about 75 minutes, plus the notebook.
Generative models produce images (Stable Diffusion), text (GPT), audio, protein structures and
weather forecasts (GenCast). They differ in architecture, but they share a few ideas, and this lesson
gives the first ones:
Maximum likelihood as a KL divergence. It explains why likelihood-trained models cover all the
data but may put mass between modes, and why GANs behave differently (lesson 05).
Latent variables. Gaussian mixtures, factor analysis, VAEs (lesson 02) and diffusion models
(lesson 07, with latents x1,…,xT) all build a complex pθ(x) from simple parts.
The ELBO. It is the training loss of VAEs, and the DDPM loss is a reweighted ELBO. The IWAE
bound is the standard way to report the test log-likelihood of a VAE.
The language of the track. "Exact likelihood", "a bound", "no likelihood": every later lesson
places its model in the family table of section 2.
We have data x1,…,xn, drawn from an unknown data distributionpdata. A
generative model is a distribution pθ(x) that should be close to pdata. We use it
in three ways:
Sample: draw new x∼pθ (new images, new weather fields).
Score: evaluate the density pθ(x) of a given x (to detect unusual inputs, to compare
models, to compress).
Represent and condition: map x to a code z, or sample x given a condition y
(text-to-image, a forecast given today's weather).
Not every family can do all three. The table below shows which can.
The standard way to fit pθ is maximum likelihood estimation (MLE): choose the θ
that makes the data most probable, maxθn1∑ilogpθ(xi). Section 3 proves
that this is the same as minimizing the forward KL divergenceKL(pdata∥pθ).
The other order, the reverse KLKL(pθ∥pdata), behaves very differently.
The picture first: fit one Gaussian to two bumps.
Interactive figure: loading…
Choose "forward KL", press "Find the best q", then do the same for "reverse KL". The blue curve
is the data density p=21N(−3,1)+21N(3,1); the orange
curve is one Gaussian q. Forward KL (what MLE minimizes) gives a wide q that covers both bumps and
puts mass in the empty gap. Reverse KL gives a narrow q on one bump. Then press "track toy ±1":
the bumps overlap, p has only one mode, and both fits are almost the same.
Why the difference? Write both divergences as averages:
Forward:KL(p∥q)=Ex∼p[logp(x)−logq(x)]. The average is over
the data. Wherever the data has mass and q is tiny, −logq(x) is huge. So q must put mass
on every region that has data. This is mode covering (also called mass covering).
Reverse:KL(q∥p)=Ex∼q[logq(x)−logp(x)]. The average is over q.
Regions that q ignores cost nothing, but mass in places where p is tiny costs a lot. So q
stays inside one mode. This is mode seeking.
The numbers for the two bumps at ±3: the forward fit is N(0,10), which is the mean and
the variance of p (moment matching, proved in section 3). The reverse fit is close to
N(2.98,1.022) (or its mirror image), with reverse KL 0.689≈log2 nats: it
captures one of two equal halves. The track's 1D toy, "two Gaussians"
21N(−1,1)+21N(1,1), has its means only 2 standard deviations apart,
so its density has a single flat-topped mode; there the two fits are nearly equal
(σ=1.41 and σ=1.39).
Quick check
A model is trained by maximum likelihood, but it is too simple to fit every mode of the data. What does the fit usually look like?
Why.
MLE minimizes the forward KL Epdata[logpdata−logpθ]. Any data region where pθ≈0 makes −logpθ huge, so the model must cover every mode, even at the price of mass in the gaps. Ignoring a mode is what reverse KL allows. "All mass at the mean" would give −logpθ=∞ at almost every data point.
The families of this track differ in what they give you. All of them can sample, but only some can
evaluate pθ(x):
Family
Density pθ(x)
Sampling cost
Latent space
Lesson
Autoregressive (PixelCNN, GPT)
exact
D sequential steps for D dimensions
none
03
Normalizing flow (RealNVP, Glow)
exact
one pass
z, same size as x
04
VAE
lower bound (ELBO)
one pass
z, usually smaller than x
01–02
GAN
none
one pass
z
05
Energy-based model
unnormalized: e−Eθ(x)/Zθ,Zθ unknown
MCMC, many steps
none
06
Diffusion and score models
a bound (ELBO); exact via an ODE, but costly
many denoising steps
noisy xt, same size as x
07–10
Interactive figure: loading…
Click each family, then drag the yellow probe. The data are the track's "8 Gaussians" (eight
bumps on a circle of radius 2). The figure shows what each family gives you: a
density (blue shading), samples (orange) and the
score∇xlogp(x) (violet arrows). The readout says what the family
can tell you about the probe point. All values are exact, computed from the known mixture; nothing
is trained here.
The track uses a few shared toy datasets, so that you can compare the families on the same data:
"two Gaussians" in 1D (this lesson), a 1D Gaussian N(0,σdata2) where every
diffusion formula has a closed form, "two points" x0∈{−1,+1}, "8 Gaussians" in 2D (the figure
above, standard deviation 0.15 per bump), and "two moons" from scikit-learn, used where a network is
trained.
A latent variable model has a hidden variable z and two simple parts:
a priorp(z), a simple distribution over the latent;
a likelihood (or decoder) pθ(x∣z), a simple distribution over x for each z.
To sample, draw z first, then x given z. This is ancestral sampling. The model's density is
the marginal, also called the evidence:
pθ(x)=z∑p(z)pθ(x∣z)orpθ(x)=∫p(z)pθ(x∣z)dz.
Running example: two Gaussians.z∈{1,2} with p(z=1)=p(z=2)=21, and
x∣z=k∼N(μk,1) with μ1=−1,μ2=+1. So
p(x)=21N(x;−1,1)+21N(x;1,1).
The posterior. After we see x, Bayes' rule gives the probability of each cause:
p(z=k∣x)=p(z=k)p(x∣z=k)/p(x). In our example the log-odds are simple:
logp(z=1∣x)p(z=2∣x)=2(x+1)2−(x−1)2=2x,
so p(z=2∣x)=σ(2x), where σ(u)=1/(1+e−u) is the logistic function.
Numbers at x=0.5.p(x,z=1)=21N(0.5;−1,1)=0.0648 and
p(x,z=2)=21N(0.5;1,1)=0.1760. So p(x)=0.2408,logp(x)=−1.4238, and the
posterior is (0.2689,0.7311)=(σ(−1),σ(1)).
Choose any distribution q(z∣x) over the latent; it is our guess for the posterior and is
called the variational distribution. Compute
ELBO(q)=Eq(z∣x)[logp(x,z)−logq(z∣x)].
Section 3 proves that this is a floor under the fixed ceiling logp(x), and that the distance
between them is KL(q(z∣x)∥p(z∣x)). Try it before you read the proof.
Interactive figure: loading…
Drag the observation x (yellow), then move the slider q(z=2). The bottom panel shows the
ELBO for every guess q under the ceiling logp(x).
The yellow segment is the gap; the readout shows that it equals the KL divergence from q to the
exact posterior (the blue bars). The gap closes only at q=p(z∣x), the vertical blue line. The
start is the hard assignment q(z=2)=1.
Quick check
Same model, but now you observe x=1. You use the hard assignment q(z=2)=1. How large is the gap logp(x)−ELBO(q)?
Why.
The gap is KL(q∥p(z∣x)). For a point mass on z=2 this is 1⋅logp(z=2∣x)1=−logσ(2x)=−logσ(2)=−log0.8808=0.127. A hard assignment is never exact while the other component has positive posterior mass. 0.313 is the gap at x=0.5, and log2 is the gap at x=0.
Notation.pdata is the data distribution; pθ(x) the model. In a latent variable
model, pθ(x,z)=p(z)pθ(x∣z). The KL divergence is
KL(q∥p)=Eq[log(q/p)]≥0, with equality only when q=p (Gibbs' inequality,
information theory, lesson 03). H(q)=−Eq[logq] is
the entropy. All logarithms are natural, so the unit is the nat.
The first term is −H(pdata) by the definition of entropy.
Move it to the other side. ■
With a finite training set, replace pdata by the empirical distribution (equal weight 1/n
on each xi): the average negative log-likelihood −n1∑ilogpθ(xi) is then the
cross-entropy between the empirical distribution and the model. For continuous data, H is a
differential entropy and can be negative, but it is still a constant in θ.
Corollary (a Gaussian fitted by MLE matches the moments). If q=N(μ,σ2) and p has
mean m and variance v, the forward KL KL(p∥q) is smallest at μ=m,σ2=v.
Proof of the corollary
Ep[−logq(x)]=21log(2πσ2)+Ep[(x−μ)2]/(2σ2).
Write x−μ=(x−m)+(m−μ). The cross term has mean 0, so
Ep[(x−μ)2]=v+(m−μ)2.
The term (m−μ)2 is smallest at μ=m.
With μ=m, the function 21logσ2+v/(2σ2) has derivative
2σ21−2σ4v in σ2, which is 0 at σ2=v; it is negative
before and positive after, so this is the minimum.
KL(p∥q)=Ep[−logq]−H(p), and H(p) does not depend on q.■
For the two bumps at ±3:m=0 and v=1+32=10, so the forward fit is N(0,10),
as in the figure. The reverse KL has no such formula; it has a local minimum on each bump.
With z∈Rd and a neural network decoder, pθ(x)=∫p(z)pθ(x∣z)dz has no
closed form. A grid with n points per axis needs nd decoder calls. The posterior
pθ(z∣x)=pθ(x,z)/pθ(x) is unknown too, because it needs pθ(x). Sampling is still
easy (ancestral sampling). So evaluating and training are hard, while sampling is easy. The ELBO
gives a quantity we can compute and train.
Theorem 1. Let pθ(x)>0. Let q(z∣x) be any distribution that puts mass only where
pθ(z∣x)>0 (it may put zero mass where the posterior is positive, as a hard assignment does).
Then
So ELBO(q)≤logpθ(x), with equality exactly when q(z∣x)=pθ(z∣x).
Proof.
logp(x) does not depend on z, so logp(x)=Eq[logp(x)].
Bayes' rule: p(x)=p(x,z)/p(z∣x) for every z that q can draw.
Multiply and divide by q inside the log:
logp(z∣x)p(x,z)=logq(z∣x)p(x,z)+logp(z∣x)q(z∣x).
Take Eq of both sides: the first term gives the ELBO, the second gives the KL.
KL≥0, with equality only when q is the posterior. ■
The Jensen route. Define the importance weightw(z)=p(x,z)/q(z∣x). Then
Eq[w]=∑zqqp(x,z)=p(x). The logarithm is concave, so Jensen's inequality gives
logp(x)=logEq[w]≥Eq[logw]=ELBO(q). This route is one line, but it does
not give the size of the gap. Theorem 1 does: the gap is exactly a KL divergence.
What each term rewards. Split logp(x,z)=logpθ(x∣z)+logp(z):
The reconstruction term rewards latents that explain x well. The rate term charges nats for
moving q away from the prior, that is, for storing information about x in z. A second form is
ELBO(q)=Eq[logp(x,z)]+H(q): fit plus spread. Without the entropy H(q), the best
q would be a point mass on the most probable latent.
What maximizing does. Over q, the term logp(x) is a constant. So maximizing the ELBO over q
is the same as minimizing KL(q∥p(z∣x)): a reverse KL, so the best q in a
restricted family is mode seeking. Over θ, a step can raise logpθ(x) or shrink the gap.
So the optimum is biased toward models whose posteriors are easy to fit with q.
Quick check
The decoder θ is fixed. You change only q(z∣x) for one data point x. Which statement is true?
Why.
logpθ(x)=log∑zp(z)pθ(x∣z) contains no q, so it is fixed. The ELBO equals logpθ(x)−KL(q∥pθ(z∣x)), so it rises when q moves toward the posterior. A higher floor does not lift the ceiling.
Theorem 2: EM never decreases the log-likelihood #
When the exact posterior is computable (mixtures, hidden Markov models, factor analysis), we can
make the gap zero. Take a dataset x1,…,xn with one distribution qi per data point, and
L(θ,q)=∑iELBO(xi;θ,qi). The EM algorithm (expectation–maximization)
alternates two steps:
E-step:qi←pθt(z∣xi) for every i. By Theorem 1, every gap is 0.
M-step:θt+1=argmaxθL(θ,q). For a Gaussian mixture this has a closed
form: weighted means, weighted variances and average responsibilities.
Theorem 2.logpθt+1(x1:n)≥logpθt(x1:n).
Proof of Theorem 2
After the E-step, L(θt,q)=logpθt(x1:n) (all gaps are 0).
The M-step gives L(θt+1,q)≥L(θt,q).
Theorem 1 at θt+1 gives logpθt+1(x1:n)≥L(θt+1,q).
Chain the three: logpθt+1(x1:n)≥L(θt+1,q)≥L(θt,q)=logpθt(x1:n).■
EM is coordinate ascent on L: the E-step maximizes over q, the M-step over θ. It
needs the exact posterior in the E-step. For a neural decoder this posterior is unknown, so a VAE
cannot use EM.
A VAE chooses a family Q (for example, Gaussians with diagonal covariance) and one encoder
network qϕ(z∣x) with parameters ϕ. For any x, the encoder outputs a member of
Q. This is amortized inference: one forward pass replaces a separate optimization for
each data point. The model and the encoder are trained together by stochastic gradient ascent on
∑iL(θ,ϕ;xi), where L(θ,ϕ;x) is the ELBO with q=qϕ(⋅∣x).
The gap is not 0, so a step that raises the ELBO can lower logpθ(x). There is no monotone
guarantee as in EM.
EM
per-point VI
amortized VI (VAE)
q for each x
exact posterior
best member of Q, optimized for this x
encoder output qϕ(⋅∣x)
gap
0
approximation gap
approximation gap + amortization gap
cost for a new x
one E-step (if tractable)
an inner optimization loop
one forward pass
The decomposition (Cremer, Li and Duvenaud, 2018). Let qx∗ be the best member of Q for
this x, the one with the largest ELBO. Then
The approximation gap equals KL(qx∗∥p(z∣x)): the family Q is too simple.
Fixes: a richer family (full covariance, normalizing flows, hierarchical latents) or a tighter
bound (IWAE, below).
The amortization gap means that the encoder does not output the best member for this x.
Fixes: a larger encoder, a few gradient steps on each q after the encoder (semi-amortized
inference), or more encoder updates per decoder update.
Quick check
Your VAE uses a diagonal Gaussian encoder. You replace the encoder by a network twice as large, with the same output family. Which gap can this reduce?
Why.
The approximation gap is set by the family Q: even a perfect encoder can only output the best diagonal Gaussian qx∗. A larger encoder can get closer to qx∗ for every x, which reduces the amortization gap. To reduce the approximation gap, enlarge Q (for example with a flow) or use a tighter bound.
Draw K samples from the encoder instead of one, and average the weights inside the logarithm.
Theorem 3 (Burda, Grosse and Salakhutdinov 2016). Let q(z∣x)>0 wherever p(x,z)>0. Draw
z1,…,zK independently from q(z∣x), let wk=p(x,zk)/q(zk∣x) and
p^K=K1∑k=1Kwk. Define the IWAE bound (importance-weighted autoencoder bound)
LK=E[logp^K]=E[logK1k=1∑Kq(zk∣x)p(x,zk)].
Then (i) L1=ELBO; (ii) LK≤logp(x); (iii) LK≤LK+1;
(iv) if the weights are bounded, LK→logp(x) as K→∞.
Why this is true, in words.p^K is an unbiased estimate of p(x). The log of an unbiased
estimate is biased downward (Jensen). Averaging more weights makes the estimate less noisy, so the
downward bias shrinks.
Proof of Theorem 3
(i) For K=1,E[logw1]=Eq[logp(x,z)−logq(z∣x)], which is the ELBO.
(ii) E[wk]=∫q(z∣x)q(z∣x)p(x,z)dz=p(x), so E[p^K]=p(x). Jensen for the
concave log: E[logp^K]≤logE[p^K]=logp(x).
(iii) Take K+1 samples and a subset I of size K, chosen uniformly at random. Each index is in
I with probability K/(K+1), so EI[K1∑k∈Iwk]=K1⋅K+1K∑k=1K+1wk=p^K+1.
Jensen over I, with the samples fixed: logp^K+1=logEI[K1∑k∈Iwk]≥EI[logK1∑k∈Iwk].
Average over the samples. For each fixed I, the K weights in I are independent draws, so the
right side has expectation LK. Hence LK+1≥LK.
(iv) By the law of large numbers, p^K→p(x) with probability 1. If the weights are bounded,
logp^K is bounded too (between the log of the smallest and the largest weight), so the
limit can be taken inside the expectation. ■
Interactive figure: loading…
Move the proposal q (orange) and watch the bound. The model is z∼N(0,1),x∣z∼N(z,0.25), with the observation x=1.5; so logp(x)=−1.931 is known exactly
(blue line), and the posterior is N(1.2,0.2). Each orange point is a Monte Carlo estimate of
LK with ±2 standard errors. With q = the prior, the ELBO (K=1) is 4.8 nats too low,
but L1000 is within 0.002 nats. With q = the posterior every weight equals p(x), and
all LK are exact. Try "too narrow": the weights have infinite variance and the bound
improves very slowly.
How fast the IWAE gap closes, and its cost for training
For large K, expand logp^K around p(x) to second order. With E[p^K]=p(x) and
Var(p^K)=Varq(w)/K:
logp(x)−LK≈2Kp(x)2Varq(w).
The gap shrinks like 1/K, and it is small when q makes the weights nearly equal. The dashed grey
curve in the figure is this prediction. When q is much narrower than the posterior,
Varq(w)=∞ and the formula fails: the bound still rises, but slowly. (For the Gaussian
example above, this happens when q's variance is at most half of the posterior's variance.)
The price is K times the compute. For training, a second price appears: the encoder's gradient gets
noisier relative to its size as K grows; its signal-to-noise ratio falls like 1/K
(Rainforth et al., 2018). For evaluation, large K is standard: Burda et al. report test
log-likelihoods with K=5000.
Quick check
What is the IWAE bound LK for K=1?
Why.
With one sample, logp^1=logw1=logp(x,z1)−logq(z1∣x), and its expectation is the ELBO. The option logEq[w] equals logp(x): the log is outside the expectation there. The reconstruction term misses the rate −KL(q∥p(z)).
Answers to section 1. (1) It covers both bumps. MLE minimizes the forward KL, which is mode
covering: for bumps at ±3 the fit is N(0,10). (2) About 0.3 nats. A point mass has
H(q)=0, so its ELBO is logp(x,z=2), and the gap is −logp(z=2∣x)=−logσ(1)=0.313
nats. (3) The ELBO can go up: the refinement removes part of the amortization gap. logpθ(x)
cannot change, because it does not depend on q.
Same mixture, same x=0.5. The log-joints are logp(x,z=1)=−2.7371 and
logp(x,z=2)=−1.7371. They differ by exactly the log-odds 2x=1.
(a) The ELBO for three choices of q=(q1,q2). Use ELBO=Eq[logp(x,z)]+H(q).
q
Eq[logp(x,z)]
H(q)
ELBO
gap logp(x)−ELBO
uniform (0.5,0.5)
−2.2371
0.6931
−1.5439
0.1201
hard (0,1)
−1.7371
0
−1.7371
0.3133
posterior (0.2689,0.7311)
−2.0060
0.5822
−1.4238
0
Step by step for the first row: Eq[logp(x,z)]=0.5⋅(−2.7371)+0.5⋅(−1.7371)=−2.2371;H(q)=−2⋅0.5log0.5=log2=0.6931; the sum is −1.5439. Check it with Theorem 1:
KL(q∥p(z∣x))=21log0.26890.5+21log0.73110.5=0.3101−0.1899=0.1201. ✓
(b) The two gaps. If Q = hard assignments (as in hard EM or k-means), the best member is
z=2, so the approximation gap at x=0.5 is 0.3133 nats, whatever the encoder. If Q = all
distributions on {1,2}, the approximation gap is 0, and only the encoder can be wrong. Take the
encoder qϕ(z=2∣x)=σ(a+bx). The exact posterior is σ(2x), so (a,b)=(0,2) gives
zero gap for every x. A half-trained encoder (a,b)=(0,1) outputs q2=σ(0.5)=0.6225 at
x=0.5, and its gap is KL((0.3775,0.6225)∥(0.2689,0.7311))=0.0280 nats: a pure
amortization gap.
A case where even the best encoder of this form has a gap: change the model to
21N(0,1)+21N(0,4) (same mean, different variances). Its posterior logit is
logp(z=1∣x)p(z=2∣x)=−log2+83x2, which is symmetric in x. A linear
encoder σ(a+bx) cannot follow it; its best version is almost constant (a=0.244,b=0) and
has an average gap of 0.181 nats over the data. One more input feature, x2, removes the gap: the
family "all distributions on {1,2}" was never the problem, the encoder was.
(c) IWAE with K=2, by exact enumeration. Take q uniform. The weights are
w(1)=0.0648/0.5=0.1295 and w(2)=0.1760/0.5=0.3521. The pair (z1,z2) takes four equally likely
values:
(1,1):logw(1)=−2.0439;
(2,2):logw(2)=−1.0439;
(1,2) or (2,1), probability 21:log20.1295+0.3521=log0.2408=−1.4238=logp(x).
So L2=41(−2.0439)+41(−1.0439)+21(−1.4238)=−1.4839, and
ELBO=−1.5439<L2=−1.4839<logp(x)=−1.4238.
The gap falls from 0.1201 to 0.0601, about half. The large-K formula, with
Varq(w)/p(x)2=0.2136, predicts 0.2136/(2⋅2)=0.0534 for K=2. It becomes
accurate by K=10: predicted 0.0107, exact 0.0110.
NumPy and SciPy check the numbers of section 4. The IWAE bound for a uniform q is computed
exactly, without Monte Carlo noise: the number of draws with z=2 is binomial.
importnumpyasnpfromscipy.statsimportnorm,binomfromscipy.specialimportlogsumexp,expit# The two-Gaussians model: p(z = k) = 1/2, x | z = k ~ N(mu_k, 1), mu = (-1, +1)mu,x=np.array([-1.0,1.0]),0.5log_joint=np.log(0.5)+norm.logpdf(x,mu,1.0)# log p(x, z = k)log_px=logsumexp(log_joint)# log p(x)post=np.exp(log_joint-log_px)# p(z | x)print(log_joint.round(4),round(log_px,4),post.round(4),round(expit(2*x),4))# [-2.7371 -1.7371] -1.4238 [0.2689 0.7311] 0.7311defelbo(q):# E_q[log p(x, z)] + H(q)q=np.asarray(q,float);m=q>0returnnp.sum(q[m]*log_joint[m])-np.sum(q[m]*np.log(q[m]))defkl(q,p):# KL(q || p) on {1, 2}q=np.asarray(q,float);m=q>0returnnp.sum(q[m]*np.log(q[m]/p[m]))forqin([0.5,0.5],[0.0,1.0],post):print(round(elbo(q),4),round(log_px-elbo(q),4),round(kl(q,post),4))# -1.5439 0.1201 0.1201 uniform q: gap = KL to the posterior# -1.7371 0.3133 0.3133 hard assignment# -1.4238 0.0 0.0 q = posterior: no gap# Exact IWAE bound for q uniform: the number m of draws with z = 2 is Binomial(K, 1/2)w=np.exp(log_joint)/0.5# importance weights p(x, z)/q(z)forKin[1,2,10,100,1000]:m=np.arange(K+1)L_K=np.sum(binom.pmf(m,K,0.5)*np.log(((K-m)*w[0]+m*w[1])/K))print(K,round(log_px-L_K,5))# 1 0.12011 | 2 0.06006 | 10 0.01102 | 100 0.00107 | 1000 0.00011
The gap halves from K=1 to K=2 and then falls like 1/K.Numerics: in high dimensions the
raw weights wk underflow or overflow. Always compute logp^K=logsumexp(logw)−logK.
VAEs (lesson 02). The loss is −ELBO with a
Gaussian encoder and a neural decoder. Test log-likelihoods are reported as LK with large K.
Diffusion models (lesson 07). DDPM is a latent variable model with
latents x1,…,xT and a fixed "encoder", the noising process. Its loss is a reweighted ELBO.
Latent diffusion (lesson 13). The first stage is
an autoencoder trained with a reconstruction term and, in the KL-regularized version, a very small
rate term. Latent diffusion models of weather fields and of seismic ground motion use the same two
stages.
Mixtures in climate science. Gaussian mixtures fitted by EM to Northern Hemisphere
geopotential height anomalies found three robust circulation regimes (Smyth, Ide and Ghil,
J. Atmos. Sci., 1999). The posterior gives each day's regime probabilities.
Amortized inference in science. One encoder that outputs a posterior for any new observation
replaces one optimization per seismogram or per field (simulation-based inference uses the same
idea). The amortization gap is the accuracy you pay for that speed.
MLE vs other objectives. Likelihood-trained models (autoregressive, flows, VAEs, diffusion)
cover all modes but may put mass where there is no data; GANs (lesson 05) do not minimize the forward
KL and can drop modes ("mode collapse").
Mixing up forward and reverse KL. MLE minimizes KL(pdata∥pθ), the forward
KL (mode covering). Variational inference over q minimizes KL(q∥p(z∣x)), a reverse KL
(mode seeking).
Comparing models by the ELBO as if it were the likelihood. A model can win by having a smaller
gap, not a higher logp(x). Compare with LK for large K.
Dropping −logq in a Monte Carlo ELBO. Without the entropy, q collapses to a point mass at the
most probable latent.
Mixing up the two gaps. A bigger or better-trained encoder cannot fix the approximation gap: it
is set by the family Q. A richer family lowers the approximation gap, but does not remove
the amortization gap by itself.
Expecting EM's guarantee from a VAE. EM's monotone guarantee comes from the exact posterior in
every E-step (gap 0) and from coordinate ascent. A VAE has a restricted encoder and takes joint
gradient steps in (θ,ϕ), so logpθ(x) can go down.
Ignoring the support condition of IWAE. If q(z∣x)=0 where p(x,z)>0, then p^K is no
longer unbiased and LK does not reach logp(x) (check yourself 5).
Mixture with weights π=(0.2,0.8), means (−1,1), unit variances. What is p(z=2∣x=0)?AnswerThe log-odds are log0.20.8+2⋅0=log4, so p(z=2∣x=0)=4/5=0.8. At x=0 both likelihoods are equal, so the posterior equals the prior.
In the lesson's mixture, what is the gap of a hard assignment at x=0?AnswerThe posterior is (21,21), so the gap is −log21=log2=0.693 nats. With two components, this is the largest gap that a hard assignment to the more likely component can have.
Why does the EM E-step make the bound tight, and why can a VAE not do this step?AnswerSetting qi=pθt(z∣xi) makes KL(qi∥p(z∣xi))=0, so the ELBO equals logpθt(xi) (Theorem 1). For a neural decoder, pθ(z∣x) needs pθ(x)=∫p(z)pθ(x∣z)dz, which has no closed form. So the VAE uses a parametric encoder and keeps a gap.
A VAE gives test values L1=−95.2,L10=−93.8,L1000=−93.1 nats. What
do you know about logp(x) and about the ELBO gap?Answerlogp(x)≥L1000≈−93.1 (up to Monte Carlo noise). The ELBO gap logp(x)−L1 is at least 95.2−93.1=2.1 nats. There is no upper limit from these numbers: logp(x) can be above −93.1.
In the lesson's mixture at x=0.5, use the hard q=(0,1) as the IWAE proposal. What is
LK for every K, and why does it not approach logp(x)?AnswerEvery sample is z=2 with weight p(x,z=2)/1=0.1760, so LK=log0.1760=−1.7371 for all K. The support condition fails: q(z=1)=0 while p(x,z=1)>0, so E[p^K]=p(x,z=2)<p(x).
You fit q=N(μ,σ2) to p=21N(−2,1)+21N(2,1) by forward KL. What are
μ and σ2?AnswerMoment matching: μ=0 and σ2=Varp(x)=1+22=5. (The variance of a mixture is the average within-component variance, 1, plus the variance of the means, 4.)
Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
Which KL divergence does maximum likelihood minimize?
The forward KL KL(pdata∥pθ), because Epdata[−logpθ]=H(pdata)+KL(pdata∥pθ).
Why is the forward KL mode covering?
It averages logp−logq over the data, so any data region where q≈0 costs a huge amount. q must put mass on every mode.
"Why do we train with maximum likelihood, and what does it optimize?" — It minimizes the forward KL
to the data distribution. It is consistent and needs only samples of the data, but it is mode
covering: a model that is too simple puts mass between the modes.
"Derive the ELBO. Which derivation tells you the gap?" — Jensen on logEq[w] gives the bound;
inserting q/q into logp(z∣x)p(x,z) gives ELBO + KL, so the gap is KL(q∥p(z∣x)).
"Why not maximize logp(x) of a VAE directly?" — The integral over z has no closed form, and the
log of a Monte Carlo average is biased downward. The ELBO is a bound we can estimate without bias and
differentiate.
"How would you measure the amortization gap?" — Compare the ELBO of the encoder output, the ELBO
after optimizing q for each test point (same family), and LK with large K as an estimate of
logp(x) (Cremer et al., 2018).
"Is a tighter bound always better for training?" — No. IWAE costs K times more, and the encoder's
gradient signal-to-noise ratio falls like 1/K.
What a generative model is and the map of model families used in this track.
Open on YouTube · the player loads from youtube-nocookie.com when you press play
A short refresher on maximum likelihood before the KL view of section 3.
Open on YouTube · the player loads from youtube-nocookie.com when you press play
The ELBO as a floor under the log-evidence, with plots of the gap.
Open on YouTube · the player loads from youtube-nocookie.com when you press play
The E-step and M-step for a Gaussian mixture, step by step.
Open on YouTube · the player loads from youtube-nocookie.com when you press play
Diederik P. Kingma and Max Welling, An Introduction to Variational Autoencoders
(2019): §2.1 (encoder and amortized inference), §2.2 (the ELBO and its gap), §2.6 (importance
sampling of logp(x) and IWAE).
Yuri Burda, Roger Grosse and Ruslan Salakhutdinov, Importance Weighted Autoencoders
(2016): §3 (the bound and Theorem 1) and Appendix A (the proof).