Latent variables and the ELBO
How to make the code distribution part of the model, why the likelihood of a latent-variable model is intractable, and how the evidence lower bound gets around it.
From autoencoders to latent variables
The topic Autoencoders ended with a model that compresses well but cannot generate. It has no distribution over codes, so there is nothing to draw a new code from. And its decoder was trained only at the codes of training images, so a code from anywhere else may decode to a smear.
The way out, as that topic suggested, is to make the distribution of codes part of the model. Choose a distribution over codes before training, let the decoder define the probability of an image given its code, and train both so that the codes stay where that distribution expects them. A model built this way is a latent-variable model. Its language — joint, marginal and conditional distributions — comes first.
Joint, marginal, conditional
Take two random variables: the data and a latent variable . Their joint density is a cloud over the plane.
Projecting the cloud onto an axis gives a marginal. Marginalization integrates the other variable out:
A conditional is a slice of the joint at a fixed value of the condition, renormalized so that it is again a density:
For example, is the slice of along the line , divided by so that it integrates to one over . Read backwards, the same definition is the product rule, , which we will use in both directions. For discrete variables, replace integrals with sums.
The latent-variable model
A latent-variable model generates data in two steps: draw a code from the prior , then draw an image from the decoder (the likelihood) . This defines the joint
The prior is chosen, not learned; the standard choice is . All the learnable structure lives in the decoder with parameters .
The model's distribution over images is the marginal, called the evidence:
Maximum-likelihood training means maximizing over the training images.
The reverse question, "which codes explain this image?", is answered by the posterior. By Bayes' theorem,
Why the likelihood is intractable
Both quantities we care about involve the evidence: the likelihood is the evidence itself, and the posterior has it in the denominator. And the evidence is an integral over the whole latent space .
With a neural-network decoder, is an arbitrary nonlinear function of , and the integral has no closed form. Numerical options fail too:
- Quadrature. A grid with points per axis needs decoder evaluations. For and a coarse that is — per image, per training step.
- Naive Monte Carlo. Write and average over prior samples. The estimate is unbiased, but for a given almost every prior sample explains it badly: the average is dominated by rare lucky draws.
How bad that is can be measured. The terms are independent, each with mean and variance , and an average divides the variance by . Relative to ,
Take a toy with independent coordinates, and , at . Per coordinate, , and the Gaussian integral of the squared likelihood gives . Their ratio is , and with coordinates it is . A 10% relative error needs samples: 140 for , about 635,000 for , about for . The cost grows exponentially with the latent dimension, per image and per training step.
So the likelihood we want to maximize and the posterior we would need for encoding are both intractable. Samples drawn from the posterior instead of the prior would all land where is explained. We cannot draw them, but we can learn something close.
The approximate posterior
The way out is to stop asking for the exact posterior and approximate it by a member of a simple family. The VAE uses a diagonal Gaussian whose mean and variances are produced by an encoder network with parameters :
Replacing an intractable posterior with the member of a simple family closest to it in is variational inference.
Classic variational inference fits a separate pair for every data point by its own optimization, and has to repeat it for every new point. The VAE instead trains one network that maps any to the parameters of its . The cost of inference is paid once, during training, and shared across all inputs: this is amortized inference.
One problem remains: the KL above contains the very posterior we cannot compute, so we cannot minimize it directly. The next block shows how to get around that.
Where the ELBO comes from
Start from the evidence and multiply by inside the integral (we drop and for now; a Gaussian is positive everywhere, so the division is safe):
The inequality is Jensen's: the logarithm is concave, so . is the evidence lower bound (ELBO).
How loose is it? Write the gap as a single expectation under (the constant can go inside), substitute , and cancels. With the parameters restored, the result is an exact identity:
Since KL is non-negative, this is the bound again, and it is tight exactly when equals the true posterior. Note what depends on what: involves only . A step in usually raises the ELBO by raising the likelihood, though it can also make the posterior easier for to fit, so maximizing the ELBO only approximately maximizes the likelihood.
Reconstruction and regularization
Writing inside the ELBO splits it into two terms:
Reconstruction measures how well an image survives the trip image → code → image. Regularization measures how far each code distribution is from the prior. They pull in opposite directions. Reconstruction averages over , and an average never exceeds its maximum, so on its own it would put all of on the single best code: a point, the plain autoencoder again. The KL charges for that collapse to a point and for every departure from .
To make reconstruction concrete, take the Gaussian decoder from Autoencoders: the network outputs a mean image , and every pixel gets the same fixed variance , so . As we saw there, and in the log-density of Multivariate Gaussians, is then a scaled squared error plus a constant that does not depend on the network. With the scale is exactly 1, and the training loss, the negative ELBO with one sampled , becomes up to a constant. Another reweights the KL by — the idea behind β-VAE. The KL between two Gaussians has a closed form, which the next topic computes.
The prior is fixed, not the posterior
What exactly does the prior constrain?
What is fixed is the prior : it is chosen before training and never learned. The posterior is not fixed: for each input the encoder outputs its own two vectors, and .
The KL term pulls each toward the prior, but only as far as reconstruction allows. An input can afford a fairly narrow, off-centre Gaussian when that helps the decoder tell it apart from other inputs. What it cannot afford is to run off to some arbitrary corner of , or to shrink to a single point: a point code would pay an infinite KL. So all codes end up in one known region around the origin, at unit scale.
That is what makes generation possible. Strictly, it is the mixture of all posteriors, , that should fill the prior without holes; training pushes it there only approximately. Where it succeeds, a fresh lands where training codes live, and the decoder turns it into a sensible image.
So training a VAE means maximizing the ELBO over and together. Its reconstruction term is an expectation over , which in practice we estimate with a sampled . How to train the encoder through that sample is the next topic, The reparameterization trick.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Go deeper — optional
Code and labs do not count toward the main percentage or the certificate.
- ELBO on a binary-latent toyoptional
Compare log p(x), ELBO at a mismatched q, and equality at the true posterior. Self-check in the notebook.
~25 min · easier on a computer · The ELBO topic read.
Self-check in the notebook — not part of mastery or the certificate.
Answers are saved on the server and tied to this browser until you sign in.