Paper 88~11 min readICLR 2014worked math + runnable code
Paper Breakdown

VAE,
explained.

A plain autoencoder squeezes an image into a few numbers and rebuilds it — but pick a random point in that squeezed space and decode it, and you get garbage. The latent space is a pile of disconnected islands with holes everywhere. The Variational Autoencoder fixes this with one probabilistic twist: encode each input not to a point but to a little cloud — a distribution — and gently pull all those clouds toward one shared, smooth shape you can sample from. The reward is a latent space you can wander through and generate from. Here's the ELBO that trains it, and the two tricks that make it possible.

Video breakdown
The animated walkthrough is in production.
Read the full breakdown below in the meantime ↓
01

The broken latent space

An autoencoder is two networks: an encoder that compresses an input x into a small latent code z, and a decoder that rebuilds x from z. Train it to reconstruct and it learns a compact code. But as a generative model it fails: the encoder is free to scatter codes anywhere it likes, so the latent space becomes a lumpy archipelago — clusters of valid points separated by vast dead regions. Sample a random z and decode it, and you almost certainly land in a dead region and get nonsense.

The Variational Autoencoder (Kingma & Welling, 2013) fixes this by making the latent space well-behaved by construction. Two changes do it: the encoder outputs a distribution instead of a point, and a regularizer pulls every input's distribution toward a single simple prior — a standard normal. Now the space is smooth and gap-free, so a random draw from the prior decodes to something plausible. You've turned a compressor into a generator. The trick is doing this without breaking training — which is where the paper's two ideas come in.

The one-sentence version

Encode each input to a Gaussian cloud instead of a point, pull all the clouds toward a standard normal so the latent space is smooth and samplable, and train by maximizing the ELBO — reconstruction minus the KL to the prior.

02

Encode to a distribution

Instead of emitting a single code, a VAE's encoder emits the parameters of a Gaussian: a mean μ and a variance σ² per latent dimension. The latent for an input is a cloud centered at μ with spread σ — a small region of the space that all decodes to (roughly) the same input. That alone starts to fill the gaps: nearby points now mean similar things, because each input claims a whole neighborhood, not a pinprick.

But clouds could still drift apart and leave holes. So the VAE adds the KL-divergence regularizer: a penalty for how far each encoder cloud q(z|x) = N(μ, σ²) sits from the shared prior p(z) = N(0, 1). This pressure herds all the clouds toward the origin and toward unit spread, so together they tile a single smooth blob you can sample from. The two forces are in tension — reconstruction wants each cloud tight and distinctive so it can rebuild its input; the KL wants every cloud to look like the same standard normal — and the VAE's whole behavior is that balance. Crank the KL up and the space is smooth but reconstructions blur; turn it down and you drift back toward a plain autoencoder.

03

The reparameterization trick

There's a problem hiding in "sample z from N(μ, σ²)." Training needs gradients to flow back through the encoder to adjust μ and σ — but sampling is random, and you can't backpropagate through a random draw. The gradient path is cut.

Move the randomness out of the path
Blockedz ~ N(μ, σ²)  — random sampling has no gradient to μ, σ.
Rewritez = μ + σ·ε,  with ε ~ N(0, 1) drawn separately.
NowRandomness lives in ε (no parameters); μ and σ are deterministic.
ResultGradients flow through μ and σ → the network trains end to end.

The reparameterization trick rewrites the sample as z = μ + σ·ε, where ε is drawn from a fixed standard normal that carries no learnable parameters. Statistically this produces exactly the same distribution as sampling from N(μ, σ²) directly — but now the only randomness is ε, and μ and σ sit on a clean, differentiable path. Gradients flow through them as if the sampling weren't there. This one reframing is what turned the VAE from an elegant idea into a network you can train with ordinary backpropagation, and the trick reappears throughout modern generative modeling.

04

The ELBO

The true objective — maximize the data's log-likelihood log p(x) — is intractable. The VAE instead maximizes a tractable lower bound, the ELBO: a reconstruction term minus the KL from the encoder distribution to the prior.

ELBO  =  𝔼q(z|x)[ log p(x|z) ]  −  KL( q(z|x) ‖ p(z) )   ⟹   loss = reconstruction + KL

Maximizing the ELBO = minimizing (reconstruction error + KL). The first term makes z carry enough about x to rebuild it; the second keeps q(z|x) near the standard-normal prior, so the latent space stays smooth and samplable.

For a Gaussian encoder and a standard-normal prior, the KL has a closed form — no sampling needed — which is what makes the whole thing cheap to optimize:

KL( N(μ, σ²) ‖ N(0, 1) )  =  ½ · Σ ( μ² + σ² − 1 − log σ² )

When μ = 0 and σ = 1 (the cloud exactly matches the prior), every term is 0 and the KL is 0 — no penalty. Push μ away from 0, or σ away from 1, and the penalty grows. The runnable version below computes this KL, the reparameterized sample, and the full VAE loss.

RUN IT YOURSELF

The ELBO, from scratch

A VAE's loss is two terms: reconstruction (how well the decoder rebuilds x) plus the KL from the encoder's Gaussian to the standard-normal prior. The KL has a closed form, ½·Σ(μ² + σ² − 1 − log σ²), which is exactly zero when the cloud matches the prior (μ=0, σ=1) and grows as it drifts away. The reparameterization trick writes the latent sample as z = μ + σ·ε so gradients can flow. Change the mean, log-variance, or the reconstruction and watch the KL and the total loss move.

CPython · WebAssembly
05

What it showed

The VAE gave deep learning a principled, trainable generative model with an explicit latent space:

ContributionSignificance
A tractable objectiveThe ELBO turned an intractable likelihood into something you can optimize with gradient descent.
The reparameterization trickMade sampling differentiable, enabling end-to-end training — a technique reused far beyond VAEs.
A smooth, samplable latent spaceRandom draws from the prior decode to plausible data; you can interpolate between points and get coherent morphs.
Stable, likelihood-based trainingUnlike adversarial models, VAEs train stably with a clear objective and an encoder you can reuse.

The trade-off, well known even at the time, is that VAE samples tend to be blurry — the Gaussian assumptions and the averaging pressure of the reconstruction term smooth out fine detail. That softness is why sharp image synthesis later leaned on GANs and then diffusion. But the VAE's structured latent space and stable training kept it central, especially as a component rather than a standalone image generator.

06

The latent-space idea

The VAE's deepest legacy is the idea of a learned, well-structured latent space you can generate and interpolate in — and that idea powers systems far bigger than the original model. Most strikingly, latent diffusion (the basis of Stable Diffusion) runs its diffusion process not on raw pixels but inside a VAE-style latent space: an autoencoder compresses images to a compact latent, diffusion generates in that space, and the decoder paints the result back to pixels. The efficiency of modern image generation rests directly on that compression trick.

Beyond images, the reparameterization trick became a standard tool for any model that needs to backpropagate through a sampling step, and the ELBO framing connects deep learning to a whole tradition of variational inference. VAEs also seeded a family of successors — masked and discrete-latent variants like VQ-VAE that swap the Gaussian latent for a learned codebook, powering later autoregressive image and audio models. The plain VAE rarely wins a sample-quality contest today, but its three gifts — an explicit latent, a likelihood-based objective, and differentiable sampling — are woven through the generative models that do.

Worth knowing

The KL term can collapse: if the decoder is powerful enough to reconstruct x while ignoring z, the encoder just sets q(z|x) = prior (KL = 0) and the latent carries no information — "posterior collapse." Fixes like KL warm-up (annealing the KL weight up over training) and the β-VAE's tunable KL weight address this, and they're a reminder that the reconstruction-vs-KL balance is the whole ballgame.

Frequently asked

Quick answers

What is a VAE?

A generative model that encodes data to a latent Gaussian and decodes it, regularized so the latent space is smooth and samplable. Trained by maximizing the ELBO.

What is the ELBO?

The training objective: reconstruction quality minus the KL from the encoder distribution to the prior — a tractable lower bound on the data log-likelihood.

What is the reparameterization trick?

Writing z = μ + σ·ε with ε from a fixed N(0,1), so randomness sits in ε and gradients flow through μ and σ — making sampling differentiable.

VAE vs GAN?

VAEs train stably on a likelihood objective with an explicit latent but blurrier samples; GANs train adversarially for sharper samples but with no encoder or likelihood.

Finished this one? 0 / 101 Paper Breakdowns done

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Paper Breakdowns