Unit 2 of 7 · about 4 hours · derive, implement
The ELBO and reparameterized sampling
The ELBO is the only training signal in the required VAE build. A sign error in the KL term or a blocked sampler fails silently into blurry reconstructions.
You'll be able to
- Derive the ELBO from Jensen and split it into a reconstruction term and a KL term against the prior.
- Implement the one-sample VAE loss with separate reconstruction and KL terms that match the public tests.
- Implement reparameterized sampling so gradients flow through the encoder mean and variance.
Covers
Read
In this order, only the parts listed
0 of 2 done
- ELBO derivation and the reconstruction-minus-KL formUnderstanding Deep Learning - generative models
Sections 17.3 to 17.6, with Equations 17.14 to 17.24 (pp. 331 to 338)
Read Section 17.6.1 for the one-sample estimate (Eq 17.23). Leave Sections 17.8 to 17.9 and the Notes for later units.
- The reparameterization trickUnderstanding Deep Learning - generative models
Section 17.7 with Equation 17.25 and Notebook 17.2 (pp. 339 to 340)
Focus on
- Jensen turns into and creates the bound (Eqs 17.14 to 17.16). Equation 17.17 shows the gap to the true log likelihood is exactly , so the bound is tight when equals the posterior.
- Equation 17.18 rewrites the same bound as expected log likelihood minus . Section 17.6 conditions on to get the VAE form (Eq 17.21) with encoder networks for mean and covariance.
- The one-sample estimate (Eq 17.23) pairs one decoded log likelihood with the closed-form Gaussian KL (Eq 17.24). Keep the two terms separate in code so collapse is visible.
- Reparameterization (Eq 17.25) moves randomness into from . Backprop passes through and and never down the stochastic branch.
Common mistakes · 3
- Coding
stdasexp(logvar)instead ofexp(0.5 logvar). The samples then have variance while the KL in Eq 17.24 assumes . - Averaging the KL over the wrong axis. The closed form sums over latent dimensions first and then averages over the batch; swapping the two changes the loss scale with latent size.
- Comparing the Equation 17.30 objective with an ordinary ELBO. With above 1 it is still a lower bound on , but a looser one, so its value is not comparable with the bound.
Try it yourself
Closed-book, before checking anything
Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.State the ELBO in reconstruction-minus-KL form and say when it is tight.Answer
(Eqs 17.18 and 17.21). It equals minus (Eq 17.17), so it is tight exactly when equals the true posterior.
2.Write the reparameterized sample and explain why gradients flow.Answer
with from (Eq 17.25). Randomness enters through , which has no parameters, so gradients flow through and and skip the stochastic branch.
3.For
x=[[1,0]],x_hat=[[0.5,0.5]],mu=[[1,2]]andlog_var=[[0,0]], what does Deep-ML 393 return?Answer(3.0, 0.5, 2.5). Reconstruction 0.5 is the summed squared error 0.25 + 0.25. KL 2.5 is 0.5 times (1 + 4). Total is their sum.
4.Why does the VAE use one sample per data point in Equation 17.23?Answer
The expectation over is estimated by Monte Carlo with while the KL stays closed-form. One sample per point is unbiased and keeps the per-step cost low; more samples only reduce estimator variance.
Practice
- Deep-ML 393: Implement VAE Loss (ELBO)
Implement
vae_loss(x, x_reconstructed, mu, log_var)returning (total,reconstruction,KL); the two public tests check(0.02, 0.02, 0.0)and(3.0, 0.5, 2.5)Solve it changed-prompt: rename the arguments, swap the batch order, and re-derive the KL from Eq 17.24 before coding. The tests check the split, not just the total.
You're done when
You derive Eq 17.18 without notes and your vae_loss passes both public tests with the two terms reported separately.
Only when you can do this without notes.
Leads to
Notes
Resources · 1 for this unit, 4 more in the section
- Understanding Deep Learning - generative modelsSimon J. D. Prince · primary
Read Chapter 14 for evaluation limits, 15 for GANs, 17 for VAE and ELBO, and 18 for diffusion. Read Chapter 16 only for flow recognition. Use notebooks 17.1-17.3 and 18.1-18.4 as mathematical and visual practice.
Also in this section
- D2L - GAN and DCGAND2L authors · support
Use gan.md and dcgan.md for one bounded adversarial exposure. The required implementation remains a small VAE with separate reconstruction and KL terms on a two-dimensional mixture or binarized MNIST subset.
- Deep-ML candidate - VAE, GAN and diffusion problemsDeep-ML · optional
After the platform admission check, use at most one of 393 VAE Loss, 1384 Reparameterization, 174 1D GAN or 303 Forward Diffusion as a changed-prompt drill.
- Stanford CS231n - diffusion assignment sliceStanford University · optional
Use the Assignment 3 diffusion component only after the VAE gate if diffusion mechanics remain weak. It is not a second required generative implementation.
- Progressive Distillation for Fast Sampling of Diffusion ModelsarXiv · reference
Read Section 4 only for the velocity parameterization and why it stays stable at zero signal-to-noise ratio. Leave Algorithms 1 and 2 and the distillation results for later.