The Variational Autoencoder (VAE) fixes this by making the latent space explicitly probabilistic and regularized toward a known prior (typically standard Gaussian), so that sampling z ~ N(0, I) and decoding produces plausible outputs.
The encoder outputs the parameters of an approximate posterior q_φ(z|x) = N(μ(x), σ²(x)) rather than a single point, and the model is trained to maximize the Evidence Lower Bound (ELBO):
ELBO(x) = E_{z~q_φ(z|x)}[log p_θ(x|z)] - D_KL(q_φ(z|x) ‖ p(z))
Derivation of the ELBO. We want to maximize log p_θ(x) = log ∫ p_θ(x|z)p(z) dz, an intractable integral. Introduce any distribution q_φ(z|x) and apply Jensen's inequality (log of an expectation ≥ expectation of the log, since log is concave):
log p_θ(x) = log E_{z~q_φ}[p_θ(x|z)p(z)/q_φ(z|x)] ≥ E_{z~q_φ}[log p_θ(x|z)p(z)/q_φ(z|x)] = ELBO(x)
Expanding the right side and grouping terms recovers that two-term expression: a reconstruction term E_{q_φ}[log p_θ(x|z)] (how well the decoder reconstructs x from a sampled z — for Gaussian p_θ(x|z) with fixed variance, this reduces to negative squared reconstruction error, recovering the vanilla autoencoder's objective as a special case) and a KL regularization term pulling the approximate posterior toward the prior p(z) — this is the term that makes the latent space well-organized and sample-able, absent entirely from the vanilla autoencoder.
Continue with the book
Explore the full treatment, worked examples, and exercises in The AI Algorithms Compendium.
Companion article for The AI Algorithms Compendium, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.
