The Reparameterisation Trick

easy
Unit 8 · Attention0/3 to milestone

Sampling z∼N(μ,σ2)z \sim \mathcal N(\mu, \sigma^2) isn't differentiable in μ\mu and σ\sigma. The reparameterisation trick writes z=μ+σ ϵz = \mu + \sigma\,\epsilon with ϵ∼N(0,1)\epsilon \sim \mathcal N(0, 1), so gradients flow through μ\mu and σ\sigma.

VAEs predict log⁡σ2\log\sigma^2 ("logvar") for stability. Implement:

  • sample_z(mu, logvar, eps): μ+e12logvar ϵ\mu + e^{\frac12 \text{logvar}}\,\epsilon (the noise eps is passed in)
  • sample_grads(mu, logvar, eps, dz): given upstream gradient dz, return (dmu, dlogvar)
#vae#generative-models#sampling2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
The Reparameterisation Trick · TechGradify