A variational autoencoder's encoder outputs, for a 1-D latent code, a mean μ=1 and standard deviation σ=0.5. (a) Compute KL(N(μ,σ2) ∥ N(0,1)). (b) Draw z by the reparameterisation trick if the noise sample is ε=0.8. (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 =Eq(z∣x)[ln⁡p(x∣z)]−KL(q(z∣x)∥p(z)); for Gaussians, KL=12(σ2+μ2−1−ln⁡σ2) per dimension, and z=μ+σε with ε∼N(0,1) makes sampling differentiable.

Steps

  1. The closed-form KL for one dimension is 12(σ2+μ2−1−ln⁡σ2). For a code with several dimensions, sum over the dimensions.
  2. Substitute: σ2=0.25 and μ2=1, so KL=12(0.25+1−1−ln⁡0.25)=12(0.25+1.3863)≈0.818 nats.
  3. Reparameterise: z=μ+σε=1+0.5×0.8=1.4.
  4. Why: sampling z∼N(μ,σ2) directly is a random operation, and gradients cannot flow through it to the encoder. Writing z as a deterministic function of (μ,σ) and external noise ε gives ∂z/∂μ=1 and ∂z/∂σ=ε, so we can back-propagate.
  5. 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 N(0,1), so the space is smooth and filled, and we can generate new data by sampling z∼N(0,I) and decoding.
  6. The KL is 0 only when μ=0 and σ=1. The trade-off between it and the reconstruction term is controlled by a weight (as in the β-VAE).

Common mistakes

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 x=Wz+μ+ϵ with z∼N(0,Iq) and ϵ∼N(0,σ2Id). The maximum-likelihood solution is WML=Uq(Λq−σ2I)1/2R and σML2=1d−q∑j>qλj. (a) For d=4 and sample covariance eigenvalues 6,3,0.6,0.4, find σML2 and the scale of each column of WML for q=2. (b) Why does the covariance of the model match the data in the top q directions? (c) How does PPCA relate to ordinary PCA as σ2→0?
Next in Unsupervised learning: For A=(3045), find the singular values, the best rank-1 approximation A1 in the Frobenius norm, and the approximation error. What fraction of the squared Frobenius norm does A1 keep, and why is this the best possible rank-1 choice?