Md. Asif Uddin

    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.

    Sampling on the path, and sampling beside itTwo small graphs. On the left, mu and sigma feed a sampling node that draws z from a normal distribution, and z feeds the function f. The backward arrow from f to mu and sigma is crossed out, because a draw has no derivative with respect to its parameters. On the right, epsilon is drawn from a standard normal outside the path, and z is computed as mu plus sigma times epsilon, so the derivatives are one with respect to mu and epsilon with respect to sigma.draw on the pathdraw beside itμσdraw z~ N(μ, σ²)f(z)no derivative through a drawμσε~ N(0, 1)×+f(z)∂z/∂μ = 1, ∂z/∂σ = εBoth draw z from N(μ, σ²). Only the right onelets the chain rule reach μ and σ.
    Fig. 3 — Type C · Flow — The same draw written two ways. On the path, a sample has no derivative with respect to its parameters. Beside it, the noise enters from outside and the code is a sum and a product the chain rule can pass through.

    A draw has no derivative

    The reconstruction term of the ELBO is an expectation, Eqϕ[f(z)]\E_{q_\phi}[f(\vec z)] with f(z)=log⁡pθ(x∣z)f(\vec z)=\log p_\theta(\vec x\mid\vec z). Training needs its gradient with respect to the encoder’s parameters ϕ\phi, and ϕ\phi enters through the distribution being averaged over, not through ff. Drawing z\vec z and backpropagating stops at the draw: a sample from N(μ,σ2)\mathcal N(\mu,\sigma^2) is not a differentiable function of μ\mu and σ\sigma 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 ε\vec\varepsilon from N(0,I)\mathcal N(\vec 0,\mat I), which involves no parameter, and compute z=μ+σ⊙ε\vec z=\vec\mu+\vec\sigma\odot\vec\varepsilon. The expectation over qϕq_\phi becomes an expectation over ε\vec\varepsilon, whose distribution does not move when ϕ\phi does, so the gradient can go inside:

    z=μϕ+σϕ⊙ε,∇ϕ Eqϕf(z)    =Eε[∇ϕf(μϕ+σϕ⊙ε)].\begin{aligned} &\vec z=\vec\mu_\phi+\vec\sigma_\phi\odot\vec\varepsilon,\\[6pt] &\nabla_\phi\,\E_{q_\phi}f(\vec z)\\ &\;\;=\E_{\vec\varepsilon}\big[\nabla_\phi f(\vec\mu_\phi+\vec\sigma_\phi\odot\vec\varepsilon)\big]. \end{aligned}
    (I.13.6)

    Moving the noise off the path lets the gradient through the draw.

    In one dimension the derivative with respect to μ\mu is E[f′(z)]\E[f'(z)] and with respect to σ\sigma is E[f′(z) ε]\E[f'(z)\,\varepsilon], since ∂z/∂μ=1\partial z/\partial\mu=1 and ∂z/∂σ=ε\partial z/\partial\sigma=\varepsilon. One draw of ε\varepsilon per example gives an unbiased estimate, and the backward pass of I.6 computes it: ε\varepsilon 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 ℓ=log⁡σ2\ell=\log\sigma^2 adds one link to the chain, σ=eℓ/2\sigma=e^{\ell/2}, so ∂z/∂ℓ=12σε\partial z/\partial\ell=\tfrac12\sigma\varepsilon. 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 ∇q=q ∇log⁡q\nabla q=q\,\nabla\log q:

    ∇ϕ Eqϕf(z)    =∫f(z) ∇ϕqϕ(z) dz    =Eqϕ[f(z) ∇ϕlog⁡qϕ(z)].\begin{aligned} &\nabla_\phi\,\E_{q_\phi}f(\vec z)\\ &\;\;=\int f(\vec z)\,\nabla_\phi q_\phi(\vec z)\,d\vec z\\ &\;\;=\E_{q_\phi}\big[f(\vec z)\,\nabla_\phi\log q_\phi(\vec z)\big]. \end{aligned}
    (I.13.7)

    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 ff, so ff may be a black box or discontinuous, and z\vec z may be discrete. For a Gaussian, ∂log⁡q/∂μ=(z−μ)/σ2=ε/σ\partial\log q/\partial\mu=(z-\mu)/\sigma^2=\varepsilon/\sigma. That is I.13.T2: two unbiased estimates of one gradient.

    Which one is quieter?

    Take f(z)=z2f(z)=z^2 and q=N(1,0.25)q=\mathcal N(1,0.25), so the true derivative with respect to μ\mu is 2μ=22\mu=2. The pathwise estimate is 2(μ+sε)2(\mu+s\varepsilon), with variance 4s2=14s^2=1. The score-function estimate is (μ+sε)2ε/s(\mu+s\varepsilon)^2\varepsilon/s, with variance (μ4+18μ2s2+15s4)/s2−4μ2=21.75(\mu^4+18\mu^2s^2+15s^4)/s^2-4\mu^2=21.75. I.13.B04 derives both.

    As the encoder grows confident the difference becomes extreme. At s=0.01s=0.01 the pathwise variance is 0.00040.0004 and the score function’s is 10,01410{,}014. The score function divides by ss; 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 f(z)=sin⁡(10z)f(z)=\sin(10z) under N(0,1)\mathcal N(0,1) the pathwise estimate 10cos⁡(10z)10\cos(10z) has variance 5050, the score function 0.50.5, and the true gradient is 10e−5010e^{-50}, zero to any printed precision (I.13.X10). The pathwise variance grows with how fast ff changes, the score function’s with how sharp qq is (Mohamed, Rosca, Figurnov and Mnih 2020).

    Discrete codes

    A Bernoulli code cannot be reparameterised this way. Writing z=1[u<π]z=\mathbf 1[u<\pi] for uniform uu produces the right distribution, but its derivative with respect to π\pi 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 21.7521.75. Set mu, s = 0.0, 1.0 and replace f and df by sin⁡(10z)\sin(10z) and 10cos⁡(10z)10\cos(10z): the variances become 5050 and 0.50.5, and the order reverses.

    Sources