Variational Autoencoders & the ELBO
Latent variables and variational inference.
The ELBO is one inequality, . Behind it hide two different gaps, two competing gradient estimators, a family of ever tighter bounds, and a theorem saying that the bound is maximized by a model that ignores its latent code. This lesson makes each of them precise.
What you’ll learn
- Split the gap between and an amortized ELBO into approximation and amortization gaps.
- Prove that the reparameterization and score-function estimators are unbiased, compare their variances, and prove that the importance-weighted bound tightens monotonically.
- Derive the rate–distortion decomposition with , and prove that posterior collapse is optimal for expressive decoders.
- Place the -VAE, free bits, VQ-VAE and hierarchical VAEs in this picture.
Amortized inference and its two gaps
Cremer, Li & Duvenaud (2018) measured both gaps and found that suboptimal inference was often due more to the encoder than to the Gaussian family. They also observed a subtler effect: because the objective subtracts , training pushes the decoder toward models whose true posteriors are easy to approximate. The ELBO does not just evaluate a model; it shapes it.
Each gap has its own remedies. Richer families such as normalizing-flow posteriors (Rezende & Mohamed, 2015) attack the approximation gap. Iterative refinement, a few steps of per-example gradient ascent started from the encoder's output (semi-amortized VAEs), attacks the amortization gap.
At test time you keep a trained diagonal-Gaussian encoder, but for each you run 50 steps of gradient ascent on the ELBO over that 's Gaussian parameters, starting from the encoder's output. Which gap can this reduce?
- Only the approximation gap
- Only the amortization gap
- Both gaps
- Neither: the ELBO cannot be improved after training
Show the answer
B. Only the amortization gap
The family stays diagonal Gaussian, so the best achievable ELBO, , is unchanged, and so is the approximation gap. Per-example optimization only moves toward that supremum, shrinking the amortization gap.
Gradient estimators and tighter bounds
(a) The law of the unconscious statistician rewrites the expectation over a fixed distribution:
By the mean value theorem, each difference quotient of in a coordinate direction is bounded by , so dominated convergence moves the limit inside:
(b) Differentiate the density instead of the integrand, and use :
Estimator (b) is fully general (discrete , black-box ) but uses only values of , so its variance grows with ; it needs baselines and control variates in practice, as in policy gradients. Estimator (a) uses the slope of . For discrete latents, continuous relaxations such as the Gumbel-softmax (Concrete) distribution and straight-through estimators trade a little bias for estimator (a)'s low variance.
Take and , so . The reparameterization estimator is exactly (variance ). What is the variance of the single-sample score-function estimator at ?
Show the answer
11
With the estimator is , with mean and second moment . The variance is , and it grows without bound as the mean moves away from . A baseline , giving , removes most of it.
Each weight has mean . Jensen's inequality for the concave log gives the upper bound:
For , let be a uniformly random -subset of . Each index lands in with probability , so the full average is an average over subsets:
Apply Jensen again, this time to , then take expectations over the samples. Every subset average has the law of an -sample average:
For the limit, the strong law gives almost surely, and bounded convergence passes the log and the expectation through. The rate comes from a second-order Taylor expansion of around , using .
A tighter bound is not automatically a better training signal. Rainforth et al. (2018) showed that the signal-to-noise ratio of the IWAE gradient decays like for the encoder's parameters while it grows like for the decoder's, so large improves the model but starves the encoder. Doubly reparameterized estimators (Tucker et al., 2019) repair the encoder's gradient.
Rate, distortion and posterior collapse
Insert into the rate ("ELBO surgery", Hoffman & Johnson, 2016):
The decoder gives the matching lower bound, since a conditional entropy is at most the cross-entropy of any model of the same conditional (Gibbs' inequality):
And the ELBO never exceeds . Together (Alemi et al., 2018):
Every model on the line , from to , has the same, optimal, ELBO, and there the two bounds pinch: . At one end the code carries no information about ; at the other it carries all of it and reconstruction is perfect. The objective cannot tell them apart. Where a trained model lands depends on the model class and on optimization.
To move a model off the end, change the objective or the model class. The -VAE (Higgins et al., 2017) maximizes : buys rate, penalizes it, and sweeping traces the rate–distortion curve of the model family. With a Gaussian decoder of fixed variance , is a noise level in disguise:
Think first In a text VAE with an LSTM decoder, the KL term falls to almost zero early in training and never recovers. Annealing from 0 to 1 often prevents this, although the final objective is the same ELBO. Why can annealing help at all?
It changes which optimum gradient descent finds, not which optima exist. At initialization the decoder cannot yet use a code, so encoding information costs rate at once and pays off only later; with from the start, the cheapest move is to shut the code off, and the model may never leave that region. With early, encoder and decoder first build a useful code, which the rate penalty then compresses. When the decoder is expressive enough, the theorem says the collapsed solution is still a global optimum.
Discrete codes, deep hierarchies and the frontier
The VQ-VAE (van den Oord, Vinyals & Kavukcuoglu, 2017) makes the code discrete. Each encoder output vector is snapped to its nearest codebook entry, with . The posterior is one-hot, so with a uniform prior over codes the KL is the constant per position and drops out. The argmin has no gradient, so the decoder's gradient with respect to is copied onto (the straight-through estimator), and two extra terms pull codebook and encoder together:
This two-stage recipe, compress to discrete tokens and then model the tokens, became a backbone of generative modeling. VQ-VAE-2 (Razavi, van den Oord & Vinyals, 2019) used a hierarchy of codes. VQGAN (Esser, Rombach & Ommer, 2021) added perceptual and adversarial losses so that a transformer could model the tokens. The autoencoders of latent diffusion (Diffusion Models) and the residual-vector-quantized neural audio codecs of Speech Synthesis descend from the same line. A recurring failure is codebook collapse, in which only a few codes are ever used; -means initialization and re-seeding dead codes help.
Continuous latents scale by depth instead. Hierarchical VAEs with top-down inference, from the Ladder VAE (Sønderby et al., 2016) to NVAE (Vahdat & Kautz, 2020) and Very Deep VAEs (Child, 2021), stack dozens of latent layers. Child argued that a deep enough hierarchical VAE can represent an autoregressive model, and showed such VAEs matching or beating autoregressive likelihoods on images while sampling far faster. A diffusion model is formally a hierarchical VAE with latent layers of the data's dimension and a fixed, non-learned encoder (Kingma et al., 2021).
Key takeaways
- The amortized ELBO's gap is an approximation gap (the family) plus an amortization gap (the shared encoder), and training also bends the decoder toward posteriors that are easy to approximate.
- The reparameterization and score-function estimators are both unbiased. The first differentiates through the sample and needs a differentiable ; the second uses only values of and has high variance without baselines.
- Importance-weighted bounds increase monotonically to , with a gap of about , but the encoder's gradient signal-to-noise ratio decays like .
- with . Every model on is ELBO-optimal, so collapse is optimal whenever the decoder alone can model the data; (a disguised decoder variance), free bits and annealing move models along or off that line.
- VQ-VAEs use discrete codes, a constant KL and straight-through gradients. The "tokenize, then model the tokens" pipelines of VQGAN, latent diffusion and audio codecs grew from them, while hierarchical VAEs and diffusion models deepen the latent hierarchy.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Two VAEs trained on the same discrete dataset, of entropy , both reach the best possible average ELBO, . Model A has rate ; model B has rate nats. How much information about do their codes carry?
- The same amount, since their ELBOs are equal
- B carries at most 40 nats, possibly none at all
- A carries none; B carries exactly 40 nats
- A carries nats, because its decoder reproduces the data distribution
Show the answer
C. A carries none; B carries exactly 40 nats
On the optimal line the bounds pinch, so : for A and nats for B. Equal ELBOs, very different use of the latent; that is the rate–distortion critique of the ELBO.
A -VAE uses and a Gaussian decoder with fixed variance . Which standard VAE () has the same optimal parameters?
- Decoder variance
- Decoder variance
- Decoder variance , KL weight
- Decoder variance
Show the answer
D. Decoder variance
, and the positive factor does not move the maximizer. So at behaves like at : penalizing rate is the same as assuming noisier data.
As the number of importance samples grows, which statement about the IWAE objective is true?
- The bound tightens monotonically toward , but the signal-to-noise ratio of the encoder's gradient decays like
- The bound can get looser for some , so must be tuned on validation data
- For large the bound exceeds
- Encoder and decoder gradients both become less noisy, so larger is always better for training
Show the answer
A. The bound tightens monotonically toward , but the signal-to-noise ratio of the encoder's gradient decays like
The random-subset argument gives for , with a gap of about . Rainforth et al. (2018) showed that the decoder's gradient SNR grows like while the encoder's decays like .
Why does the VQ-VAE training loss contain no KL term?
- The commitment term is exactly the KL divergence, written differently
- The posterior is one-hot and the prior uniform over codes, so the KL is the constant
- KL divergence is undefined for discrete distributions
- The straight-through estimator cancels the KL's gradient
Show the answer
B. The posterior is one-hot and the prior uniform over codes, so the KL is the constant
whatever is chosen, so it has no gradient and can be dropped. The rate is fixed by the codebook size; the prior is learned in a second stage.
Further reading
- D. P. Kingma & M. Welling, "Auto-encoding variational Bayes," ICLR (2014).
- D. J. Rezende, S. Mohamed & D. Wierstra, "Stochastic backpropagation and approximate inference in deep generative models," ICML (2014).
- Y. Burda, R. Grosse & R. Salakhutdinov, "Importance weighted autoencoders," ICLR (2016).
- A. A. Alemi, B. Poole, I. Fischer, J. V. Dillon, R. A. Saurous & K. Murphy, "Fixing a broken ELBO," ICML (2018).
- D. P. Kingma & M. Welling, "An introduction to variational autoencoders," Foundations and Trends in Machine Learning (2019).