Reparameterization
Explain the different training objectives of autoencoders, VAEs, GANs and diffusion models.
THE IDEA
Sampling that gradients can pass through
The VAE must sample a code z from the encoder's distribution, and sampling is random: you cannot differentiate through a random draw. The fix is to move the randomness outside:
The noise ε is drawn separately and treated as a constant input. Now z is an ordinary function of μ and σ, with ∂z/∂μ = 1 and ∂z/∂σ = ε. The sample is still random (a new ε each time), but the loss can send gradients to the encoder.
Blue dot: μ. Orange dot: the sampled z. Moving ε moves z; the encoder's μ and σ stay differentiable.
Predict first. Mean 2, scale 0.5, noise −1. What latent is produced?
eps = rng.standard_normal(n) # randomness, no parameters in it
z = mu + sigma * eps # differentiable in mu and sigmaChapter 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
Reparameterise a sample, differentiate through it by hand, then code the sampler and its gradients.
Blue dot: μ. Orange dot: the sampled z. Moving ε moves z; the encoder's μ and σ stay differentiable.
- To do: Mean 2, scale 0.5, noise ε = −1. What latent z = μ + σ·ε is produced? Type it
- To do: Loss L = (z − 3)² with the same numbers. Type dL/dμ (the noise ε stays fixed)
- To do: Type dL/dσ for the same loss
- To do: Use the four noise draws ε = [−1, 0, 1, 2] with μ = 2, σ = 0.5. What is the mean of the four latents? Type it
Part 2. Do it in code
- reparameterize(mu, sigma, eps): z = mu + sigma * eps (works for numbers and arrays).
- reparam_grads(mu, sigma, eps, target): for the loss L = (z − target)² return (z, dL/dmu, dL/dsigma) treating eps as a fixed constant.
- sample_latents(mu, sigma, n, seed): draw n latents with np.random.default_rng(seed).standard_normal(n) as the noise and reparameterize. Return an array of length n.
Stuck, or something not working? You can come back to it any time.
CHECK YOUR UNDERSTANDING
For mean 2, scale .5 and noise -1, what latent is produced?
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.