Quiz: the variational autoencoder
Before this unit: From PCA to autoencoders (previous unit); Day 36: Neural networks.
See also: Day 30: Snakes and active shape models.
Short questions on clouds, the prior and the VAE loss. Try each on paper first, then open the answer. The encoder gives each image a cloud \mathcal N(\mu, \sigma^2); the decoder is trained on draws z = \mu + \sigma\varepsilon; the loss is reconstruction +\ \beta\cdot\mathrm{KL}, with \mathrm{KL} = \tfrac12(\mu^2 + s^2 - 1 - \ln s^2) per dimension.
Q1. In a plain autoencoder each image becomes one point; in a VAE a small cloud. Give two things the cloud buys you.
What must the decoder do with random points from the cloud? What does the prior do to all the clouds?
- The decoder is trained on random points around each image, and all clouds are pulled towards one standard cloud, so they overlap: the latent space has few gaps, and points near and between images decode to sensible images. That is what makes generating possible. (2) The spread says how sure the encoder is about where the image belongs.
Q2. Compute the KL to the prior of the clouds \mathcal N(0, 1), \mathcal N(1, 1) and \mathcal N(0, 0.5^2).
\tfrac12(\mu^2 + s^2 - 1 - \ln s^2), and \ln 0.25 = -1.386.
0; \tfrac12(1 + 1 - 1 - 0) = 0.5; \tfrac12(0 + 0.25 - 1 + 1.386) = 0.318.
Q3. What would a VAE learn if the loss had only the KL term? And with only the reconstruction term?
Which cloud makes the KL zero? What is the cheapest way to rebuild perfectly?
Only KL: every image gets the prior as its cloud, so z carries no information and the decoder outputs the same (average) image for everything. Only reconstruction: the clouds shrink to points and can sit anywhere, far apart: a plain autoencoder, with gaps.
Q4. The encoder gives \mu = 0.4, \sigma = 0.2. With \varepsilon = 1.5, what is z? Why is z written this way instead of being drawn directly from \mathcal N(0.4, 0.2^2)?
z = \mu + \sigma\varepsilon.
z = 0.4 + 0.2 \cdot 1.5 = 0.7. Written as a formula in \mu and \sigma, z changes smoothly when the encoder changes \mu or \sigma, so the training can compute how the loss changes with the encoder’s weights (the gradient). A direct draw would hide that dependence.
Q5. You increase \beta from 1 to 5. What happens to the clouds, and to the reconstructed images?
Which of the two forces gets stronger?
The pull towards the prior gets stronger: clouds move towards 0 and get wider, so neighbouring images overlap more and the latent space is tidier. The decoder must serve wider clouds with one image each, so reconstructions get blurrier and less specific.
Q6. (math) True or false: a VAE trained with \beta = 1 maximises a lower bound on the log-probability of the training images.
ELBO stands for evidence lower bound.
True. The loss with \beta = 1 is minus the ELBO, and the ELBO is at most \ln p(x); the gap is the KL between the encoder’s cloud and the true posterior of z given the image.