Reparameterization trick explained

The reparameterization trick (aka "reparameterization gradient estimator") is a technique used in statistical machine learning, particularly in variational inference, variational autoencoders, and stochastic optimization. It allows for the efficient computation of gradients through random variables, enabling the optimization of parametric probability models using stochastic gradient descent, and the variance reduction of estimators.

It was developed in the 1980s in operations research, under the name of "pathwise gradients", or "stochastic gradients".[1] [2] Its use in variational inference was proposed in 2013.[3]

Mathematics

Let

z

be a random variable with distribution

q\phi(z)

, where

\phi

is a vector containing the parameters of the distribution.

REINFORCE estimator

Consider an objective function of the form:L(\phi) = \mathbb_[f(z)]Without the reparameterization trick, estimating the gradient

\nabla\phiL(\phi)

can be challenging, because the parameter appears in the random variable itself. In more detail, we have to statistically estimate:\nabla_\phi L(\phi) = \nabla_\phi \int dz \; q_\phi(z) f(z) The REINFORCE estimator, widely used in reinforcement learning and especially policy gradient,[4] uses the following equality:\nabla_\phi L(\phi) = \int dz \; q_\phi(z) \nabla_\phi(\ln q_\phi(z)) f(z) = \mathbb_[\nabla_\phi(\ln q_\phi(z)) f(z)]This allows the gradient to be estimated:\nabla_\phi L(\phi) \approx \frac 1N \sum_^N \nabla_\phi(\ln q_\phi(z_i)) f(z_i) The REINFORCE estimator has high variance, and many methods were developed to reduce its variance.[5]

Reparameterization estimator

The reparameterization trick expresses

z

as:z = g_\phi(\epsilon), \quad \epsilon \sim p(\epsilon)Here,

g\phi

is a deterministic function parameterized by

\phi

, and

\epsilon

is a noise variable drawn from a fixed distribution

p(\epsilon)

. This gives:L(\phi) = \mathbb_[f(g_\phi(\epsilon))] Now, the gradient can be estimated as:\nabla_\phi L(\phi) = \mathbb_[\nabla_\phi f(g_\phi(\epsilon))]\approx \frac 1N \sum_^N \nabla_\phi f(g_\phi(\epsilon_i))

Examples

For some common distributions, the reparameterization trick takes specific forms:

Normal distribution

For

z\siml{N}(\mu,\sigma2)

, we can use:z = \mu + \sigma \epsilon, \quad \epsilon \sim \mathcal(0, 1)

Exponential distribution

For

z\simExp(λ)

, we can use:z = -\frac \log(\epsilon), \quad \epsilon \sim \text(0, 1)Discrete distribution can be reparameterized by the Gumbel distribution (Gumbel-softmax trick or "concrete distribution").[6]

In general, any distribution that is differentiable with respect to its parameters can be reparameterized by inverting the multivariable CDF function, then apply the implicit method. See for an exposition and application to the Gamma Beta, Dirichlet, and von Mises distributions.

Applications

Variational autoencoder

In Variational Autoencoders (VAEs), the VAE objective function, known as the Evidence Lower Bound (ELBO), is given by:

\text(\phi, \theta) = \mathbb_[\log p_\theta(x|z)] - D_(q_\phi(z|x) || p(z))

where

q\phi(z|x)

is the encoder (recognition model),

p\theta(x|z)

is the decoder (generative model), and

p(z)

is the prior distribution over latent variables. The gradient of ELBO with respect to

\theta

is simply\mathbb_[\nabla_\theta \log p_\theta(x|z)] \approx \frac \sum_^L \nabla_\theta \log p_\theta(x|z_l)but the gradient with respect to

\phi

requires the trick. Express the sampling operation

z\simq\phi(z|x)

as:z = \mu_\phi(x) + \sigma_\phi(x) \odot \epsilon, \quad \epsilon \sim \mathcal(0, I)where

\mu\phi(x)

and

\sigma\phi(x)

are the outputs of the encoder network, and

\odot

denotes element-wise multiplication. Then we have\nabla_\phi \text(\phi, \theta) = \mathbb_[\nabla_\phi \log p_\theta(x|z) + \nabla_\phi \log q_\phi(z|x) - \nabla_\phi \log p(z)]where

z=\mu\phi(x)+\sigma\phi(x)\odot\epsilon

. This allows us to estimate the gradient using Monte Carlo sampling:\nabla_\phi \text(\phi, \theta) \approx \frac \sum_^L [\nabla_\phi \log p_\theta(x|z_l) + \nabla_\phi \log q_\phi(z_l|x) - \nabla_\phi \log p(z_l)]where

zl=\mu\phi(x)+\sigma\phi(x)\odot\epsilonl

and

\epsilonl\siml{N}(0,I)

for

l=1,\ldots,L

.

This formulation enables backpropagation through the sampling process, allowing for end-to-end training of the VAE model using stochastic gradient descent or its variants.

Variational inference

More generally, the trick allows using stochastic gradient descent for variational inference. Let the variational objective (ELBO) be of the form:\text(\phi) = \mathbb_[\log p(x, z) - \log q_\phi(z)] Using the reparameterization trick, we can estimate the gradient of this objective with respect to

\phi

:\nabla_\phi \text(\phi) \approx \frac \sum_^L \nabla_\phi [\log p(x, g_\phi(\epsilon_l)) - \log q_\phi(g_\phi(\epsilon_l))], \quad \epsilon_l \sim p(\epsilon)

Dropout

The reparameterization trick has been applied to reduce the variance in dropout, a regularization technique in neural networks. The original dropout can be reparameterized with Bernoulli distributions:y = (W \odot \epsilon) x, \quad \epsilon_ \sim \text(\alpha_)where

W

is the weight matrix,

x

is the input, and

\alphaij

are the (fixed) dropout rates.

More generally, other distributions can be used than the Bernoulli distribution, such as the gaussian noise:y_i = \mu_i + \sigma_i \odot \epsilon_i, \quad \epsilon_i \sim \mathcal(0, I)where

\mui=

\top
m
i

x

and
2
\sigma
i

=

\top
v
i

x2

, with

mi

and

vi

being the mean and variance of the

i

-th output neuron. The reparameterization trick can be applied to all such cases, resulting in the variational dropout method.[7]

See also

Further reading

Notes and References

  1. Figurnov . Mikhail . Mohamed . Shakir . Mnih . Andriy . 2018 . Implicit Reparameterization Gradients . Advances in Neural Information Processing Systems . Curran Associates, Inc. . 31.
  2. Fu, Michael C. "Gradient estimation." Handbooks in operations research and management science 13 (2006): 575-616.
  3. Kingma . Diederik P. . Auto-Encoding Variational Bayes . 2022-12-10 . 1312.6114 . Welling . Max. stat.ML .
  4. Williams . Ronald J. . 1992-05-01 . Simple statistical gradient-following algorithms for connectionist reinforcement learning . Machine Learning . en . 8 . 3 . 229–256 . 10.1007/BF00992696 . 1573-0565.
  5. Greensmith . Evan . Bartlett . Peter L. . Baxter . Jonathan . 2004 . Variance Reduction Techniques for Gradient Estimates in Reinforcement Learning . Journal of Machine Learning Research . 5 . Nov . 1471–1530 . 1533-7928.
  6. Maddison . Chris J. . The Concrete Distribution: A Continuous Relaxation of Discrete Random Variables . 2017-03-05 . 1611.00712 . Mnih . Andriy . Teh . Yee Whye. cs.LG .
  7. Kingma . Durk P . Salimans . Tim . Welling . Max . 2015 . Variational Dropout and the Local Reparameterization Trick . Advances in Neural Information Processing Systems . 28. 1506.02557 .