6.2 Variational autoencoders
1. Learn
Section titled “1. Learn”- Understanding Deep Learning (Prince)chapter 17 Variational autoencoders, sections 17.1–17.7: latent variable models, the ELBO, the VAE and the reparameterisation trick
- Kingma and Welling, Auto-Encoding Variational Bayessections 2.1–2.4 and 3, and appendix B (the Gaussian KL)
2. Bridge
Section titled “2. Bridge”A model you can sample. A VAE says how the data could have been made: draw a code z from a fixed prior p(z) = N(0, I), then draw x from a decoder p(x | z). To generate, do exactly that. Training should maximise log p(x) = log ∫ p(x | z) p(z) dz, but the integral over every code has no closed form (Prince, sections 17.1–17.3).
The bound. An encoder q(z | x) = N(μ(x), diag σ(x)²) guesses which codes could have made x. Jensen’s inequality then gives the evidence lower bound, log p(x) ≥ Eq[log p(x | z)] − KL(q(z | x) ‖ p(z)), which is tight when q equals the true posterior p(z | x). The first term is the 6.1 reconstruction loss; the second keeps every code distribution near the prior, so prior samples land where the decoder was trained (Kingma and Welling 2013, sections 2.2–2.3).
Two Gaussians, one formula. Both q and p are Gaussian, so the KL has a closed form, derived below and in appendix B of Kingma and Welling. Only the reconstruction term needs a sample of z.
Reparameterisation. Sampling z ~ q is not differentiable in μ and σ, but z = μ + σ ε with ε ~ N(0, I) is: the randomness moves into an input, and gradients flow through μ and σ (Kingma and Welling, section 2.4). The alternative, the score-function estimator of 5.10, is unbiased too but far noisier; the task measures both.
β and the bound’s gap. Weighting the KL by β trades reconstruction against how tightly the codes fill the prior (Higgins et al. 2017); only β = 1 is the ELBO. To check how loose the bound is, average K importance-weighted samples inside the log: the estimate rises towards log p(x) as K grows (Burda et al. 2015).
3. Explore
Section titled “3. Explore”Variational autoencoders: edit exercises/stubs/m6_2_vae.py on your own machine. This task needs PyTorch, which the in-page runner does not have.
pip install torch # CPU build is enough for the tests
mlc check 6.2Then train on MNIST. Open notebooks/6.2-vae.ipynb on Kaggle (a CPU is enough) and paste in your 6.2 file. It compares the two gradient estimators, trains VAEs with a 2D and a 32D code at several β, plots the latent space and samples next to your 6.1 autoencoder’s, and estimates log p(x) with your importance sampler for K from 1 to 1,000.
5. Check
Section titled “5. Check”- not yet: All tests pass (0/?)
- not yet: Checkpoint quiz ≥ 80%
Learner log: time spent and points of confusion
Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.