Quant Memo
Advanced

Posterior Collapse and KL Annealing

When a VAE's decoder gets powerful enough to reconstruct data without reading the latent code, the encoder gives up and the KL term collapses to zero — and the standard fix is to turn the KL penalty up slowly instead of all at once.

Prerequisites: Variational Autoencoders, KL Divergence

Train a variational autoencoder with a strong decoder — say an autoregressive text or audio decoder — and something strange can happen: the latent code zz stops mattering. The encoder's posterior q(zx)q(z|x) collapses onto the prior p(z)p(z) for every single input, meaning zz carries zero information about xx. Reconstructions still look fine, because the decoder learned to reproduce plausible outputs on its own. But the latent space is useless: sample two different zz's and you get the same output, because the decoder never learned to listen to zz in the first place.

The analogy: a student who stops reading the prompt

Imagine a student graded only on essay quality, never on whether the essay actually answered the prompt. If the student is skilled enough to write a plausible essay from any one-word prompt, they will stop reading prompts carefully — there's no penalty for ignoring them, and ignoring them is easier. That is posterior collapse: the decoder is the skilled student, the latent code is the prompt, and the training objective never explicitly rewards the decoder for using it.

Why the ELBO invites this

The VAE objective, the evidence lower bound, is a sum of two competing terms:

ELBO=Eq(zx)[logp(xz)]reconstructionKL(q(zx)p(z))closeness to prior\text{ELBO} = \underbrace{\mathbb{E}_{q(z|x)}[\log p(x|z)]}_{\text{reconstruction}} - \underbrace{\text{KL}\big(q(z|x)\,\|\,p(z)\big)}_{\text{closeness to prior}}

In words: reward accurate reconstruction, but penalize the encoder for straying from the prior. Early in training, before zz has learned anything useful, the cheapest way to raise the ELBO is often to make q(zx)q(z|x) identical to the prior for every xx — the KL term drops to exactly zero, and if the decoder can still reconstruct reasonably well without zz, reconstruction loss barely suffers. That local optimum is easy to fall into and hard to climb back out of, because once the decoder ignores zz, the reconstruction gradient carries no signal telling the encoder to make zz informative again.

Worked example 1: the collapse is a real optimum, in numbers

Suppose, per sequence, reconstruction using an informative zz reaches log-likelihood logp(xz)=50\log p(x|z) = -50 nats, and the KL cost of that informative posterior is 88 nats. A decoder good enough to ignore zz instead reaches logp(x)=55\log p(x) = -55 nats (worse reconstruction, but no KL cost at all). Compare full ELBOs:

informative: 508=58,collapsed: 550=55\text{informative: } -50 - 8 = -58, \qquad \text{collapsed: } -55 - 0 = -55

Collapse wins, 55>58-55 > -58, even though its reconstruction is 5 nats worse — the KL penalty outweighs the reconstruction gain. This is why plain ELBO training can prefer a useless latent space.

Worked example 2: annealing tips the balance

KL annealing multiplies the KL term by a weight β(t)\beta(t) that ramps from 00 up to 11 over training, using reconβKL\text{recon} - \beta \cdot \text{KL}. With a linear schedule β(t)=min(1,t/10,000)\beta(t) = \min(1, t/10{,}000):

  • Step 2,000 (β=0.2\beta = 0.2): informative =500.2(8)=51.6= -50 - 0.2(8) = -51.6; collapsed =55= -55. Informative now wins.
  • Step 5,000 (β=0.5\beta = 0.5): informative =500.5(8)=54= -50 - 0.5(8) = -54; collapsed =55= -55. Informative still wins, by a narrower margin.
  • Step 10,000 (β=1\beta = 1): back to the original numbers, informative loses on paper — but by then the decoder has already learned to depend on zz, so abandoning it would hurt reconstruction, not just cost KL. The early low-β\beta window is what gets zz established before the full penalty arrives.
training step β(t) margin large 0
As β ramps up, the informative solution's ELBO advantage over the collapsed one shrinks — the low-β window early in training is what gives the encoder a chance to make $z$ useful before the full KL penalty bites.
x encoder z (used) encoder z≈prior
Healthy path (top): decoder actually reads an informative $z$. Collapsed path (bottom): $q(z|x)$ has drifted to the prior, so $z$ carries no signal and the decoder effectively reconstructs from nothing.

Posterior collapse happens because the ELBO lets the model pay zero KL cost by making zz uninformative, and a strong-enough decoder can still reconstruct well without it. KL annealing — starting the KL weight near 0 and raising it slowly — gives the encoder a window to make zz genuinely useful before the full penalty arrives.

What this means in practice

Posterior collapse matters whenever a VAE's decoder is autoregressive or otherwise expressive enough to model xx on its own — language VAEs and audio VAEs are the classic victims, but any financial sequence model with a powerful decoder (a VAE meant to generate synthetic price paths, say) can suffer the same failure, silently producing a latent space that carries no interpretable structure for stress-testing or interpolation. Besides KL annealing, practitioners use "free bits" (only penalizing KL above a small floor per latent dimension) and weakening the decoder (dropout on its own inputs) to force it to lean on zz.

A model with collapsed posteriors can still show a good reconstruction loss and a good-looking ELBO, so collapse is easy to miss by only watching the loss curve. The tell is the KL term itself sitting at or near zero throughout training — always plot it separately from the reconstruction term, not just their sum.

Related concepts

Practice in interviews

Further reading

  • Bowman et al., Generating Sentences from a Continuous Space (2016)
ShareTwitterLinkedIn