Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.An autoencoder you can sample from
A plain autoencoder puts each input at some point in code space, but nobody knows which points between them mean anything. A variational autoencoder (VAE) instead maps each input to a small cloud of codes and gently pulls all the clouds toward one standard bell-shaped distribution. Afterwards you can pick a random code from that bell curve, decode it, and get a new, realistic sample. The demo shows the penalty that pulls a cloud N(μ, σ²) toward N(0, 1).
VAEs generate and interpolate data, give smooth latent spaces for planning and control, flag anomalies by likelihood, and are the compression stage of latent diffusion models.
- 1. A VAE's encoder outputs…
- 2. The codes are pulled toward…
- 3. New data is made by…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Encoder: outputs μ(x) and log σ²(x), two numbers per latent dimension (32 outputs for a 16-dimensional code).
- Reparameterisation: z = μ + σ·ε with ε ~ N(0, 1), so gradients flow through μ and σ. For μ = 1, log σ² = ln 4 (σ = 2) and ε = 0.5: z = 2.
- Loss (negative ELBO, up to constants) = reconstruction + KL:
- Gaussian decoder: reconstruction ‖x − x̂‖²/(2σ_x²), weighted 1/(2·0.05²) = 200 for σ_x = 0.05;
- KL(N(μ, σ²) ‖ N(0, 1)) = ½(μ² + σ² − 1 − ln σ²), summed over latent dimensions;
- for μ = 1, σ = 0.5: ½(1 + 0.25 − 1 − ln 0.25) = 0.818;
- the KL is zero only at μ = 0, σ = 1.
- Generation: z ~ N(0, I), then x = decoder(z).
- 1. z for μ = 1, σ = 2, ε = 0.5:
- 2. KL for μ = 0, σ = 1:
- 3. Encoder outputs for a 16-dimensional code:
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.VAEs in code
Exercise 1.
- Reparameterisation gave samples with the requested mean 1.5 and standard deviation 0.5.
- The KL term was zero at the prior and 0.5 for μ = 1, σ = 1.
- The negative ELBO combined both terms.
Practice.
- Have the encoder output log σ² for numerical stability.
- Use a Bernoulli (binary cross-entropy) decoder for binary images, or a Gaussian one with a chosen or learned σ_x.
- Watch for “posterior collapse,” where the KL drives codes to the prior and the decoder ignores them. KL annealing (warming up β) or free bits help.
mu, logvar = enc(x).chunk(2, dim=-1)
z = mu + torch.exp(0.5 * logvar) * torch.randn_like(mu) # reparameterisation trick
rec = F.mse_loss(dec(z), x, reduction="none").sum(-1) / (2 * sx**2)
kl = 0.5 * (mu**2 + logvar.exp() - 1 - logvar).sum(-1)
loss = (rec + beta * kl).mean()- 1. Posterior collapse means…
- 2. A remedy for collapse:
- 3. The encoder outputs log σ² because…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
From autoencoder to generative model.
- A plain autoencoder’s codes can sit anywhere, with gaps and arbitrary scale, so random codes may decode to nonsense.
- The VAE’s KL term makes the codes of all inputs together (the aggregate posterior) roughly fill the prior.
- Random prior samples then fall where the decoder has been trained.
The ELBO.
- The loss is the negative evidence lower bound: log p(x) ≥ E_q[log p(x|z)] − KL(q(z|x) ‖ p(z)).
- Maximising it trains the decoder as a likelihood model and the encoder as an approximate posterior. The H3 pages derive this in full.
Trade-offs.
- Weighting the KL by β (β-VAE) trades reconstruction sharpness for a more regular, sometimes disentangled latent space.
- Gaussian decoders tend to give blurry samples; hierarchical VAEs, VQ-VAEs and diffusion decoders sharpen them.
- 1. The KL term makes the aggregate posterior…
- 2. The VAE loss is the negative of…
- 3. A larger β gives…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.Does the KL term make prior samples realistic?
History.
- D. P. Kingma and M. Welling, “Auto-encoding variational Bayes” (ICLR 2014).
- D. J. Rezende, S. Mohamed and D. Wierstra, “Stochastic backpropagation and approximate inference in deep generative models” (ICML 2014).
- Variants include β-VAE (2017), VQ-VAE (2017) and NVAE (2020); see section H3.
Exercise 2.
- Data: 400 points on a 270° arc of the unit circle (noise 0.03).
- Models, both with a 1-D code, 32-unit tanh encoder and decoder, and 3000 Adam steps:
- a plain autoencoder;
- a VAE with σ_x = 0.05 and β = 1.
- Test: decode 2000 samples z ~ N(0, 1). Count the fraction landing on the arc (within 0.1 of the circle and inside its 270° range), and the total-variation (TV) distance of their angles from uniform coverage, which counts off-arc samples as mismatch.
| model | training codes range | samples on the arc | TV distance from uniform |
|---|---|---|---|
| autoencoder | −1.25 to 1.77 | 0.850 | 0.166 |
| VAE | −1.87 to 1.97 | 0.956 | 0.099 |
- The VAE’s codes match the prior: they spread symmetrically over about ±2, like N(0, 1), so prior samples fall where the decoder was trained, and 96% decode onto the arc.
- The plain autoencoder’s codes sit wherever training put them (here off-centre). Prior samples outside that range decode off the arc (15% of them), and coverage is uneven.
- This is robust: seeds 1–3 gave 0.70, 0.80 and 0.81 on-arc for the autoencoder against 0.97, 0.97 and 0.975 for the VAE, with TV 0.20–0.30 against 0.09–0.095.
- Caveat: the plain autoencoder’s result depends on the luck of where its codes land; the VAE’s is designed in.
VAEs in AI, robotics and aerospace
- World models: VAEs compress camera frames into latent states for planning (World Models, Dreamer-style agents).
- Anomaly detection: low ELBO flags unusual telemetry or images.
- Design and generation: sampling and interpolating shapes, trajectories and molecules; compressing images for latent diffusion.
- Design lesson: a generative latent space needs both a decoder and a matched distribution of codes; the KL term supplies the second.
References
- D. P. Kingma and M. Welling (2014). “Auto-encoding variational Bayes.” ICLR.
- D. J. Rezende, S. Mohamed and D. Wierstra (2014). “Stochastic backpropagation and approximate inference in deep generative models.” Proc. ICML.
- D. P. Kingma and M. Welling (2019). “An introduction to variational autoencoders.” Foundations and Trends in Machine Learning 12.