4.5.4 · D1Generative Models

Foundations — Reparameterization trick

2,517 words11 min readBack to topic

This page assumes you have seen none of the notation the Reparameterization trick parent note uses. We build every symbol from the ground up, in an order where each one leans only on the ones before it. Prefer Hindi-English? See the Hinglish version →.


1. What is a random variable? (the symbol , and "")

The squiggle is read "is drawn from" or "is distributed as". So

reads: " is a random number produced by the bell curve ." We haven't defined , , or yet — that's next. For now, just picture a machine that spits out a fresh number each time you press a button.

Figure — Reparameterization trick

Look at the figure: each press of the button drops a dot at a different spot along the number line, but the dots cluster. That clustering shape is exactly the distribution.

Recall Why does the topic need

? In a VAE, is the hidden "latent code" — a compressed random summary of the input. Sampling it is the exact step where gradients get stuck.


2. The bell curve: , , ,

The two numbers:

  • (Greek letter "mu") = the mean = where the peak sits on the number line. Slide right, the whole bell slides right.
  • (Greek "sigma") = the standard deviation = how wide the bell is. Big = fat, spread-out bell; small = tall, narrow spike. We always require (a width can't be negative or zero).
  • = "sigma squared" = the variance. It is just multiplied by itself. We square it because widths add up naturally in squared units (this is a fact of statistics; take it as given here).

So the notation means "the bell curve centered at with width-squared ."

Figure — Reparameterization trick

3. Expected value and variance

Two facts we will lean on (both follow from the definitions above — "averaging is fair to shifting and scaling"):

  • — you can pull constants out of an average.
  • — adding the constant shifts everything equally so spread is unchanged; scaling by multiplies every distance-from-mean by , hence squared distances by .

A subscript on tells you which random source you're averaging over:

means "average as ranges over the standard normal." The subscript matters enormously later: the whole trick is about changing which variable the expectation is over.

Recall Why does the topic need

? The VAE loss is an average over random samples: . We can't optimize a single lucky sample — we optimize the average behaviour.


4. A function of a random number:

Picture as a valley: the bottom of the valley is the best , the walls punish bad values.


5. Slope of a function: the derivative

Why we want it: the slope tells us which way is downhill. If the slope is negative, increasing lowers the loss; if positive, decreasing lowers it. That is the entire engine of learning.

Figure — Reparameterization trick

6. The chain rule: gradients that pass through a middle step

The reparameterization trick exists precisely so that the middle piece actually exists. If is drawn by a random button, that middle slope is undefined and the chain snaps. If is a formula, the middle slope is a clean number and the chain holds.


7. The parameters , the encoder , and the input

So the encoder's output distribution is written

Here reads "the distribution , controlled by , over latent given the input ." The bar means "given / conditioned on". This is the encoder's guess; the details of why it takes this shape come from the ELBO derivation and live inside the full VAE.


8. The gradient symbol and the transform

Everything in section 2's promise pays off here: is literally scale by , then shift by — the two knobs of a bell curve, applied by hand to a fixed noise sample.


9. Element-wise multiply and the identity matrix

You need this only for the multivariate case; the one-dimensional intuition is identical, repeated times.


How these foundations feed the trick

The map below is not a restatement of headings — it shows the dependency of ideas and where the gradient breaks vs. flows: the left branch is the naive path that snaps at the random draw; the right branch is how the transform reroutes the same average so backprop survives.

gradient snaps here

same expected loss

draw z from moving curve q-phi

cannot differentiate through the draw

STUCK no gradient to phi

fixed noise epsilon from N(0,1)

transform g shift and scale

encoder knobs phi give mu and sigma

same average by the core identity

chain rule multiplies local slopes

gradient reaches phi backprop works

Related tricks that solve the same gradient-through-randomness problem in other settings: 5.2.03-Gumbel-Softmax (for discrete samples), and 6.1.02-Policy-Gradients (an alternative estimator when reparameterization isn't available). The 4.3.01-KL-Divergence appears elsewhere in the VAE loss but is not needed to understand the trick itself.


Equipment checklist

Can you answer each without peeking? Reveal to check.

What does the squiggle mean?
"is drawn from" — the variable on the left is a random sample from the distribution on the right.
What is a probability distribution, in one line?
the rule saying how likely each value is — the shape; a random variable is one dot drawn from that shape.
What two numbers fully describe a Gaussian ?
the mean (center) and the standard deviation (width); written inside as variance .
Define variance in terms of expectation.
— the average squared distance from the mean.
What is special about ?
it is centered at 0, width 1, and fixed — it never depends on the learnable knobs .
In plain words, what is ?
the long-run average value of over many samples.
What does the derivative tell you?
the slope of the loss valley — which way and how steeply changes as moves.
State the chain rule linking , , and .
.
What do and represent?
the center and width of the latent bell curve that the encoder computes for input , controlled by knobs .
How does the network guarantee ?
it outputs an unconstrained number and passes it through softplus, or outputs and takes — always positive.
Write the core reparameterization identity.
.
Write the reparameterization transform.
with .
Why does this transform make differentiable in ?
because is now a plain formula in (with the randomness parked in fixed ), so exists.