7 routes42 sections324 units

Unit 6.2 · about 4 h

208 / 324

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.

  • derive
  • implement

You can now

  • 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

  • ELBO reconstruction and KL terms
  • Reparameterized sampling and gradient flow

Study, in this order

2 steps

  1. ELBO derivation and the reconstruction-minus-KL form
    Understanding Deep Learning - generative models

    Sections 17.3 to 17.6, with Equations 17.14 to 17.24 (pp. 331 to 338)

    Depth · 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.

  2. The reparameterization trick
    Understanding Deep Learning - generative models

    Section 17.7 with Equation 17.25 and Notebook 17.2 (pp. 339 to 340)

Focus on

  • Jensen turns \(\log \mathbb{E}\) into \(\mathbb{E} \log\) and creates the bound (Eqs 17.14 to 17.16). Equation 17.17 shows the gap to the true log likelihood is exactly \(D_{\mathrm{KL}}[q \,\|\, \text{posterior}]\), so the bound is tight when \(q\) equals the posterior.
  • Equation 17.18 rewrites the same bound as expected log likelihood minus \(D_{\mathrm{KL}}[q \,\|\, \text{prior}]\). Section 17.6 conditions \(q\) on \(x\) 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 \(\epsilon\) from \(\mathcal{N}(0, I)\). Backprop passes through \(\mu\) and \(\Sigma\) and never down the stochastic branch.

Reproduce closed-book

Closed-book, derive Equation 17.18 from Equation 17.16 in four lines, then compute the Deep-ML 393 check (0.5 reconstruction, 2.5 KL, 3.0 total) with NumPy.

Traps

  • Coding std as exp(logvar) instead of exp(0.5 logvar). The samples then have variance \(\sigma^4\) while the KL in Eq 17.24 assumes \(\sigma^2\).
  • 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 \(\beta\) above 1 it is still a lower bound on \(\log \Pr(x)\), but a looser one, so its value is not comparable with the \(\beta = 1\) bound.

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.

Self-test

  1. State the ELBO in reconstruction-minus-KL form and say when it is tight.

  2. Write the reparameterized sample and explain why gradients flow.

  3. For x=[[1,0]], x_hat=[[0.5,0.5]], mu=[[1,2]] and log_var=[[0,0]], what does Deep-ML 393 return?

  4. Why does the VAE use one sample per data point in Equation 17.23?

Completion criterion

Stop when

You derive Eq 17.18 without notes and your vae_loss passes both public tests with the two terms reported separately.

Mark passed, then open 6.3 Only mark this when you can do it without notes.

Leads to

Comes after

Resources for this unit

  • Understanding Deep Learning - generative models
    Simon J. D. Princeprimarytheory
    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 DCGAN
    D2L authorssupportimplementation
    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 problems
    Deep-MLoptionalpractice
    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 slice
    Stanford Universityoptionalcourse
    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 Models
    arXivreferencereference
    Read Section 4 only for the velocity parameterization \(v = \alpha \epsilon - \sigma x\) and why it stays stable at zero signal-to-noise ratio. Leave Algorithms 1 and 2 and the distillation results for later.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels