14.3 Variational Autoencoders
A probabilistic latent space and the reparameterization trick.
These notes walk through the lecture slides. The original deck is unchanged; use (slides) on the course hub if you want that PDF.
Figures and notes follow Géron, Hands-On Machine Learning; Deisenroth, Faisal, and Ong, Mathematics for Machine Learning; and Theodoridis, Machine Learning: A Bayesian and Optimization Perspective.
1. Variational autoencoders
A variational autoencoder (VAE) is a probabilistic autoencoder: after training, the output is still partly random. A denoising autoencoder uses noise only while it trains.
It is also generative: you can draw new points that look as if they came from the training set.
The figure builds the coding as plus Gaussian noise times . The random draw is an input, not a node you cannot differentiate.
2. Applications
| Use | Sketch |
|---|---|
| Image generation | Art, avatars, synthetic frames. |
| Anomaly detection | Odd transactions, factory defects, unusual medical images. |
| Data compression | A short latent vector for storage or transmission. |
| Drug discovery | New molecules with target properties. |
| NLP | Coherent text: chat, translation, copy. |
3. The procedure
The encoder does not emit one coding. It emits a mean and a standard deviation . The coding is then drawn from a Gaussian with that mean and that width. The decoder reconstructs from the draw, as usual.
Training pushes the cloud of codings toward a Gaussian blob in latent space. After that, generation is cheap: sample a coding from the Gaussian, decode it.
4. The cost function
Two pieces.
- Reconstruction loss. Make look like (cross-entropy, or the negative log-likelihood of under a Gaussian).
- Latent loss. Make the coding distribution look like a simple Gaussian. That term is the KL divergence between the actual coding law and the target Gaussian.
5. ELBO
The training objective is the variational lower bound, or evidence lower bound (ELBO). It is a tractable stand-in for the true log-likelihood of the data.
Write for the observation, for the latent, for decoder parameters, for encoder parameters. The ELBO is
where is the likelihood, is the approximate posterior, and is the prior.
Maximize it in both parameter blocks:
SGD or Adam is the usual optimizer. Reconstruction plus a Gaussian prior on is what you are buying: a latent space you can sample.
6. Gaussian assumption
If the decoder likelihood is Gaussian, the reconstruction term is a weighted squared error plus constants:
Here is the dimension of , and is the variance on coordinate .
The KL between a diagonal Gaussian posterior and a standard Gaussian prior is closed form:
with the mean of latent coordinate .
Put the two together:
That is the ELBO when both the approximate posterior and the prior are Gaussian.
Practice
-
What problem does the reparameterization trick solve?
-
Name the two terms in the VAE cost (the ELBO).
-
After training, how do you generate a new sample without feeding in a real ?