Exercises — Reparameterization trick
These exercises rebuild the Reparameterization trick from the ground up and push you to master level. Each one has a full solution hidden inside a collapsible callout — try first, then reveal. Numeric claims are all machine-checked.
Before we start, one shared picture. Every exercise below lives on the same board: a fixed noise cloud that never depends on our learnable knobs, and a deterministic pipe that shifts it by and stretches it by to produce the latent .

Level 1 — Recognition
L1.1 Spot the non-differentiable node
In the two lines below, which one blocks gradient flow to , and why?
- (A)
- (B)
Recall Solution
(A) blocks it. The arrow "" (sampling) is a black box: if you nudge , the drawn number jumps around randomly, so has no defined value. In (B) the sampling happens first into (which does not touch ), and then is an ordinary formula of . So and are perfectly ordinary derivatives. (B) is differentiable.
L1.2 Read the transform
Given , , , compute .
Recall Solution
Picture: start at the centre , step (left, because ), land at .
L1.3 Which distribution?
If and , name the distribution of and state its mean and variance.
Recall Solution
by the location–scale property.
- Mean: .
- Variance: . "Location" = the shift ; "scale" = the stretch .
Level 2 — Application
L2.1 Single-sample gradient
Encoder gives , . You draw . Loss . Compute , , and both gradients , .
Recall Solution
Step 1 — build . . Step 2 — loss. . Step 3 — . . Step 4 — local derivatives of the pipe. . Step 5 — chain rule. Read it: both gradients are negative, so gradient descent will increase and — moving up toward the target . Makes sense: is below target.
L2.2 Vector form (diagonal covariance)
Latent is 2-D. , , . Compute using .
Recall Solution
Element-wise: So . The just says "multiply entry-by-entry" — dimension 1 uses its own , dimension 2 its own. No cross-talk.
L2.3 Sensitivity check
Same setup as L2.1 but now . Without redoing all arithmetic from scratch, predict the sign of before computing, then compute it.
Recall Solution
Predict: . Here . First compute , so . Negative times negative positive. Compute: . Why the flip: with , growing pushes down, away from target — so loss rises, hence a positive gradient telling us to shrink .
Level 3 — Analysis
L3.1 Monte-Carlo gradient estimate
Loss with fixed, . Estimate using the three samples , then compare to the exact value.
Recall Solution
Per-sample gradient: , and .
- :
- :
- :
Average: . Exact: . Read it: the 3-sample estimate hovers near the true ; with more samples it converges (law of large numbers). Both point the same direction: increase .
L3.2 Why swap gradient and expectation
Explain in one clean chain why but the same swap fails for .
Recall Solution
Write the reparameterized loss as an integral: . The measure does not contain , so by the Leibniz rule you may pull inside — only depends on . For the density itself carries . Differentiating produces an extra term — the score-function term you meet in 6.1.02-Policy-Gradients. Reparameterization avoids that by moving all -dependence into the integrand and none into the measure.
L3.3 Jacobian of the multivariate transform
For in , write the Jacobian and and say why backprop is cheap.
Recall Solution
\frac{\partial z_i}{\partial\sigma_j}=\epsilon_i\,\delta_{ij}\;\Rightarrow\;\frac{\partial z}{\partial\sigma}=\begin{bmatrix}\epsilon_1&0\\0&\epsilon_2\end{bmatrix}.$$ Both are **diagonal** ($\delta_{ij}$ = Kronecker delta, $1$ if $i=j$ else $0$). Diagonal Jacobians mean each dimension's gradient is one multiply — $O(d)$ work, no full matrix. This is why VAEs scale.Level 4 — Synthesis
L4.1 Reparameterize a uniform
You need (uniform between and ) to be differentiable in . Give the reparameterization using a fixed base , and its derivatives.
Recall Solution
Transform: , where is parameter-free. Check endpoints: ; . Uniform stays uniform under a linear map, so . ✓ Derivatives: . Same recipe as the Gaussian: fixed base noise, linear deterministic pipe.
L4.2 The KL term is not sampled
In the VAE loss , explain why only the first term needs the trick.
Recall Solution
The reconstruction term is an expectation over , so it needs a differentiable sample — that's where the trick lives. The 4.3.01-KL-Divergence term between two Gaussians has a closed form: which is an ordinary function of — no sampling, already differentiable. See the full breakdown in 4.5.02-ELBO-Derivation. So the trick is only needed on the sampled expectation.
L4.3 Verify the closed-form KL numerically
For , the KL should be (the posterior equals the prior). For , compute it.
Recall Solution
Formula: .
- : . ✓ (identical distributions ⇒ zero divergence.)
- : . Sanity: a shift with equal spread costs a small, positive KL.
Level 5 — Mastery
L5.1 When the trick fails: discrete latents
A latent with . Show why has no analogue, and name the escape route.
Recall Solution
The Gaussian trick works because a continuous invertible map moves probability mass smoothly. For a discrete , any deterministic function of that outputs is a step function: its derivative w.r.t. is almost everywhere and undefined at the jump. No smooth pipe exists. Escape route: relax the discreteness with the 5.2.03-Gumbel-Softmax trick — reparameterize with Gumbel noise plus a temperature that softens the step into a differentiable curve, then anneal . Alternatively use the score-function / 6.1.02-Policy-Gradients estimator (higher variance, but works for any distribution).
L5.2 Full backward pass by hand
encoder gives , (so automatically). With , loss , current , . Compute and .
Recall Solution
Forward. . . . Backward. .
- : .
- : chain through . , and . So . Note the parametrization: it guarantees and inserts an extra factor in the chain. This is standard 2.4.01-Backpropagation bookkeeping.
L5.3 Variance intuition of the estimator
Two schemes estimate the same gradient: (R) reparameterization, (S) score-function . Both are unbiased. Argue which usually has lower variance and why.
Recall Solution
(R) usually wins. Reparameterization routes the gradient through 's own slope — it uses information about how changes, so smooth gives smooth, low-variance gradients. The score-function estimator only sees 's value multiplied by a log-density gradient; it ignores 's slope and its variance grows with 's magnitude and the tails of . Hence practitioners prefer (R) whenever the latent is continuous and is differentiable, falling back to (S) only for discrete/black-box .
Recall Quick self-test (cloze)
The reparameterization trick moves randomness into a ==parameter-free noise variable so the sample becomes a deterministic function of the parameters==. The pipe for a Gaussian is ::: with . The gradient we can now compute is ::: and . For discrete latents we switch to ::: Gumbel-Softmax or a score-function estimator.
Parent: Reparameterization trick · Hinglish: 4.5.04 Reparameterization trick (Hinglish) · Foundations: 4.5.01-Variational-Autoencoders, 4.5.02-ELBO-Derivation