The Reparameterization Trick
You can't backpropagate through a coin flip, because randomness has no slope — the reparameterization trick rewrites a random draw as a fixed formula applied to noise, so gradient descent can flow straight through it.
Prerequisites: Latent Variable Models and the ELBO, Backpropagation
Training a network by gradient descent needs one thing above all: every operation in the forward pass must have a well-defined slope, so backpropagation can multiply slopes together on the way back and tell every weight which direction to move. A random draw — literally sampling a number from a distribution — has no slope. Asking "if I nudge the mean of this distribution up a hair, how does the sample I just drew change?" doesn't have a sensible answer, because the sample already happened; it's a fixed number now, disconnected from the parameters that produced it. Any network with a sampling step sitting between its parameters and its loss is, as written, un-trainable by gradient descent. The reparameterization trick rewrites the sampling step so the randomness and the parameters are no longer tangled together, restoring a clean slope.
The analogy: a dartboard with a movable target vs a fixed grid plus wind
Imagine training someone to hit a dartboard by moving the board itself toward wherever their darts happen to land — a strange setup, but suppose that's the training signal. If the dart's landing spot is generated by "throw randomly around the board's current position," there's no way to ask "how would the landing spot have changed if I'd moved the board two inches left," because the randomness of the throw is entangled with the board's position in a way that can't be untangled after the fact — the throw already includes both effects mixed together.
Now change the setup: keep a fixed grid on the wall, always throw at the same random offset from a fixed reference dart, generated by a fixed gust of wind that has nothing to do with the board — and only afterward, add the board's current position to that random offset to get the true landing spot. Now moving the board two inches left has an obvious, exact effect: the landing spot moves two inches left too, because the random part (the wind) and the position part (the board) are cleanly separated, added together only at the very end. The reparameterization trick is exactly this separation, applied to sampling from a distribution.
The mechanism: pull the randomness outside
Say a network needs to sample a hidden value from a normal distribution whose mean and standard deviation are themselves outputs of the network — computed from the network's weights, so they need gradients flowing through them. The naive way writes
which just says "draw from a normal distribution centered at with spread " — but as an operation in a computational graph, this treats as coming directly from a black-box sampler, with no differentiable path from or to . The reparameterization trick rewrites this identical distribution as
Here (epsilon) is a random number drawn from a plain, fixed standard normal distribution — the "wind," which has nothing to do with the network's parameters and needs no gradient. And is just ordinary arithmetic: multiplication and addition, both perfectly differentiable. The statistical distribution of is provably identical either way — this is the same normal distribution with the same mean and spread — but the computational graph is completely different. Now and , both ordinary numbers, so backpropagation can flow straight through to whatever network layers produced and .
The gradient-descent explorer below shows a parameter being nudged step by step to reduce a loss. Reparameterization is what makes that same machinery applicable when one of the "layers" along the way happens to involve randomness — without it, the optimizer would have no slope to follow through that layer at all.
Worked example 1: a gradient with and without the trick
Suppose a tiny network outputs and , and a downstream loss is , wanting close to 3. Sample (fixed noise, generated once). Then , and .
Backward pass: . Using the reparameterized form, and , so
Both are ordinary numbers a network can use to update its weights. Without reparameterizing — if had been sampled directly as with no rewritten form — there would be no formula connecting to and at all, and this step would simply have no gradient to report.
Worked example 2: checking the trick preserves the distribution
Draw five values of from a standard normal: , with , . The reparameterized samples are : . Their mean is , close to as expected from just five samples, and their spread is visibly close to the spread of the raw values. This confirms the rewritten sampling procedure produces the same kind of numbers a direct draw from would — the trick changes how the sample is computed, never what distribution it comes from.
Reparameterization doesn't change what gets sampled — it changes where the randomness enters the computation, moving it to a fixed, parameter-free source so that everything downstream of it is ordinary differentiable arithmetic that gradients can pass through.
What this means in practice
This trick is what makes Variational Autoencoders trainable end-to-end by ordinary backpropagation, rather than needing much noisier gradient-estimation techniques built for non-differentiable randomness. It generalizes wherever a model must sample from a continuous distribution mid-network — for instance, sampling a latent "market regime" in a generative model of price paths and still wanting gradients to flow back into whatever produced that regime's mean and spread.
The trick does not work for every distribution equally easily — it requires the distribution to be expressible as a fixed, differentiable function of parameter-free noise, which is straightforward for a normal distribution () but not at all straightforward for something like a categorical (discrete choice) distribution, where there is no natural "add noise, then shift" decomposition. This is exactly why generative models with discrete latent variables need separate tricks (like the Gumbel-softmax relaxation) rather than plain reparameterization — a common error is assuming this trick is a universal fix for "sampling blocks gradients," when it only applies to distributions with the right reparameterizable structure.
Practice
- Given , , and , compute by hand, then find and .
- Explain why itself never needs a gradient computed for it, even though it directly determines the value of .
- A colleague proposes reparameterizing a coin-flip (Bernoulli) latent variable the same way as a normal one, writing . What goes wrong with this, and what property of the normal distribution's formula fails to carry over?
Practice in interviews
Further reading
- Kingma & Welling, Auto-Encoding Variational Bayes (2013)