Proposition 3I.13.P0346 of 100 in the corpus
The reparameterisation trick moves the randomness off the path of the gradient.
A draw has no derivative with respect to its parameters. Writing it as a sum and a product of parameter-free noise lets backpropagation through it, and usually gives far quieter gradients than the score-function estimator, though not always.
A draw has no derivative
The reconstruction term of the ELBO is an expectation, with . Training needs its gradient with respect to the encoder’s parameters , and enters through the distribution being averaged over, not through . Drawing and backpropagating stops at the draw: a sample from is not a differentiable function of and as the program is written. The figure shows the break on its left.
Write the draw as a function
The same distribution can be produced differently. Draw from , which involves no parameter, and compute . The expectation over becomes an expectation over , whose distribution does not move when does, so the gradient can go inside:
Moving the noise off the path lets the gradient through the draw.
In one dimension the derivative with respect to is and with respect to is , since and . One draw of per example gives an unbiased estimate, and the backward pass of I.6 computes it: is one more input to the graph. Kingma and Welling (2014) and Rezende, Mohamed and Wierstra (2014) introduced this for variational autoencoders.
An encoder that predicts adds one link to the chain, , so . I.13.X02 takes one step by hand.
The estimator that needs no derivative
There is a second unbiased estimator. Differentiate the integral and use :
The score function needs no derivative of f and pays in variance.
This is the score-function estimator, which Williams (1992) introduced as REINFORCE. It never differentiates , so may be a black box or discontinuous, and may be discrete. For a Gaussian, . That is I.13.T2: two unbiased estimates of one gradient.
Which one is quieter?
Take and , so the true derivative with respect to is . The pathwise estimate is , with variance . The score-function estimate is , with variance . I.13.B04 derives both.
As the encoder grows confident the difference becomes extreme. At the pathwise variance is and the score function’s is . The score function divides by ; the pathwise estimate multiplies by it. A trained VAE has narrow encoder distributions and a smooth decoder log-likelihood, which is the regime where reparameterisation wins.
It does not always win. For under the pathwise estimate has variance , the score function , and the true gradient is , zero to any printed precision (I.13.X10). The pathwise variance grows with how fast changes, the score function’s with how sharp is (Mohamed, Rosca, Figurnov and Mnih 2020).
Discrete codes
A Bernoulli code cannot be reparameterised this way. Writing for uniform produces the right distribution, but its derivative with respect to is zero almost everywhere. A backward pass through it returns a gradient of zero where the true one is not (I.13.X08). Discrete codes need the score function, or a continuous relaxation such as the Gumbel-softmax of Jang, Gu and Poole (2017), which is reparameterisable and biased.
Measure the two variances
from math import exp, pi, sqrt
mu, s = 1.0, 0.5
f, df = (lambda z: z*z), (lambda z: 2*z)
grid = [i/1000 for i in range(-8000, 8001)]
weight = [exp(-e*e/2)/sqrt(2*pi)/1000 for e in grid] # N(0, 1) on a fine grid
def moments(estimate):
m1 = sum(w*estimate(e) for w, e in zip(weight, grid))
m2 = sum(w*estimate(e)**2 for w, e in zip(weight, grid))
return round(m1, 6), round(m2 - m1*m1, 6)
print("pathwise", moments(lambda e: df(mu + s*e)))
print("score ", moments(lambda e: f(mu + s*e)*e/s))
It prints pathwise (2.0, 1.0) and
score (2.0, 21.75): the same mean, the correct gradient, and
variances that differ by a factor of . Set mu, s = 0.0, 1.0
and replace f and df by and
: the variances become and , and the order
reverses.
Sources
Depends on
Used by
Problems using this
- I.13.B04 — Two estimators of one gradientprobability▲▲△
- I.13.X02 — One reparameterised gradientgradient▲△△
- I.13.X08 — A discrete code has no pathwise derivativecounterexample▲▲△
- I.13.X10 — The pathwise estimator is not always the quieter onecounterexample▲▲△