Variational autoencoders
Explain the different training objectives of autoencoders, VAEs, GANs and diffusion models.
THE IDEA
Make the codes sampleable
A variational autoencoder (VAE) changes two things. The encoder does not output a single code but a distribution: a mean μ and a spread σ for each latent dimension. And the loss gets a second term that pulls every such distribution toward a fixed prior, usually N(0, 1).
For one dimension the KL term has a closed form: ½(σ² + μ² − 1 − ln σ²). It is exactly 0 when μ = 0 and σ = 1 (the prior itself) and grows as the encoder drifts away. Because all codes are kept near the prior, sampling a code from N(0, 1) and decoding it gives something sensible.
Grey: the prior N(0, 1). Blue: what the encoder says about this input.
Predict first. Does a VAE force every individual reconstruction to be exact?
Chapter context
Generative modeling tries to represent or sample from a data distribution. Different model families construct that distribution differently; a plausible-looking sample alone does not tell us how the model was trained or how well it covers the data.
TRY IT YOURSELF
Part 1. Do it hands-on
Compute KL values by hand and make the penalty vanish. The Python part implements the KL term, the full loss and a collapse check.
Grey: the prior N(0, 1). Blue: what the encoder says about this input.
- To do: Set the encoder's mean and spread so the KL penalty is exactly zero
- To do: KL for mean 1 and spread 1: ½(σ² + μ² − 1 − ln σ²). Type it
- To do: KL for mean 0 and spread 0.5 (3 decimals)
- To do: Total loss = reconstruction + β · KL with reconstruction 2.0, KL 0.5 and β = 0.5. Type it
Part 2. Do it in code
- kl_gaussian(mu, sigma): KL( N(mu, sigma²) || N(0, 1) ) summed over all entries: 0.5 * sum(sigma² + mu² − 1 − log(sigma²)). mu and sigma are arrays. Raise ValueError if any sigma is not positive.
- vae_loss(x, x_hat, mu, sigma, beta=1.0): reconstruction (sum of squared differences between x and x_hat) plus beta times kl_gaussian(mu, sigma). Return a float.
- posterior_collapsed(mu, sigma, tol=1e-3): True if the encoder output is (within tol) the prior in every dimension, meaning it carries no information about the input: all |mu| <= tol and all |sigma − 1| <= tol.
Stuck, or something not working? You can come back to it any time.
CHECK YOUR UNDERSTANDING
Does a VAE force every individual reconstruction to be exact?
Chapter reference: math, worked example & code
This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.
Math and notation
A VAE uses , with . A standard diffusion forward marginal is
Here alpha-bar summarizes retained signal. This is a noising equation, not a complete sampling algorithm.
A VAE optimizes an evidence lower bound:
For diagonal Gaussian q and standard normal p, its KL is . The original GAN minimax objective is
Practical generator losses often use a non-saturating variant. A common diffusion noise-prediction loss is under a specified noising schedule.
Worked example
For a scalar diffusion example, take x0=2, alpha-bar=0.64 and epsilon=−1. The signal term is 0.8×2=1.6 and the noise term is 0.6×(−1)=−0.6, giving xt=1.0. The clean value cannot be inferred from xt alone without additional assumptions or a learned model.
For q=N(1,1) against a standard normal prior, the one-dimensional VAE KL is 0.5×(1²+1−1−ln 1)=0.5 nats. This is only the regularization term, not the complete VAE objective.
Reference code
The following example uses the Python standard library and does not call a remote service.
from math import sqrt, isclose
x0, alpha, noise = 2,.64,-1
xt=sqrt(alpha)*x0+sqrt(1-alpha)*noise
assert isclose(xt,1)
assert 2+.5*(-1) == 1.5
from math import log
assert isclose(.5*(1**2+1**2-1-log(1**2)),.5)
Python examples are provided for study and local execution; this page does not execute Python.
Common mistakes
A VAE's approximate posterior is not identical to its prior. GAN loss is not a universal sample-quality score. A diffusion noising equation is not sufficient to implement the reverse model.