A variational autoencoder's encoder outputs, for a 1-D latent code, a mean and standard deviation . (a) Compute . (b) Draw by the reparameterisation trick if the noise sample is . (c) Why do we need this trick, and what role does the KL term play compared with a plain autoencoder?
Unsupervised learning, Autoencoders
Level L3 Intermediate. Length Medium. Nature Th+Mat Both.
What's being asked
Whether you can evaluate the Gaussian KL term and explain the reparameterisation trick and the regularisation effect of the KL term.
Key idea
The VAE maximises the ELBO ; for Gaussians, per dimension, and with makes sampling differentiable.
Steps
- The closed-form KL for one dimension is . For a code with several dimensions, sum over the dimensions.
- Substitute: and , so nats.
- Reparameterise: .
- Why: sampling directly is a random operation, and gradients cannot flow through it to the encoder. Writing as a deterministic function of and external noise gives and , so we can back-propagate.
- A plain autoencoder maps each input to a single point, and the latent space can have gaps and arbitrary scale. The KL term pulls every code distribution towards the prior , so the space is smooth and filled, and we can generate new data by sampling and decoding.
- The KL is only when and . The trade-off between it and the reconstruction term is controlled by a weight (as in the -VAE).
Common mistakes
- Taking when the encoder outputs , or forgetting the factor in the KL.
- Sampling outside the computation graph, so that no gradient reaches the encoder.
- Using the KL as the entire loss. Without the reconstruction term, the posterior collapses to the prior and the code carries no information.
How long your answer should be
Half a page; 5 minutes.
Last checked on 4 December 2026.
Read this
Previous in Unsupervised learning: Probabilistic PCA models with and . The maximum-likelihood solution is and . (a) For and sample covariance eigenvalues , find and the scale of each column of for . (b) Why does the covariance of the model match the data in the top directions? (c) How does PPCA relate to ordinary PCA as ?
Next in Unsupervised learning: For , find the singular values, the best rank-1 approximation in the Frobenius norm, and the approximation error. What fraction of the squared Frobenius norm does keep, and why is this the best possible rank-1 choice?