Tutorials › Statistical Methods for Single-Cell RNA Sequencing › The Reparameterization Trick for Gaussian Latents

Latent Variable Models for Single-Cell Data · Tutorial 659 of 700

The Reparameterization Trick for Gaussian Latents

Learn to express Gaussian latent sampling as a differentiable transformation and calculate the resulting pathwise gradient for an encoder.

Advanced 11 min read

What You'll Learn

  • Write a Gaussian latent draw using a parameter-free standard-normal random variable.
  • Derive the pathwise gradient of an expectation over a Gaussian latent variable.
  • Calculate sampled gradients through a one-dimensional encoder output.
  • Extend the calculation to a diagonal multivariate Gaussian latent.
  • Identify assumptions, implementation failures, and the score-function alternative.

Why Reparameterize a Gaussian Latent?

In a count VAE, an encoder assigns a distribution to the latent representation of a cell, rather than a single fixed latent value. An objective such as the evidence lower bound (ELBO) includes an expectation over that distribution. To optimize the objective, we need gradients with respect to the parameters that determine the distribution. A direct random draw can obscure how those parameters affect the sampled value.

The Gaussian reparameterization trick separates the randomness from the distribution parameters. It expresses the latent draw as a deterministic, differentiable function of those parameters and a random variable whose distribution does not depend on them. The resulting gradient is called a pathwise gradient: it follows the change in the sampled latent value as the parameters change.

Definition: If \(Z\sim N(\mu,\sigma^2)\), with \(\sigma>0\), then \(Z\) can be generated as \(Z=\mu+\sigma\epsilon\), where \(\epsilon\sim N(0,1)\). The random variable \(\epsilon\) supplies the randomness, while \(\mu\) and \(\sigma\) determine the location and scale of the resulting Gaussian.

Deriving the Pathwise Gradient

Let \(\theta\) denote parameters that determine \(\mu_\theta\) and \(\sigma_\theta>0\), and let \(f\) be a differentiable function of the latent value. We want to differentiate the expectation \(J(\theta)=\mathbb{E}_{Z\sim N(\mu_\theta,\sigma_\theta^2)}[f(Z)]\). Using the Gaussian representation, the same objective is

$$ J(\theta)=\mathbb{E}_{\epsilon\sim N(0,1)} \left[f\left(\mu_\theta+\sigma_\theta\epsilon\right)\right]. $$

The distribution of \(\epsilon\) is fixed: it does not change with \(\theta\). Under conditions that allow differentiation inside the expectation, such as differentiability of the integrand and an integrable bound on its derivative in a neighborhood of the parameter value, the chain rule gives

$$ \nabla_\theta J(\theta) = \mathbb{E}_{\epsilon} \left[ f'\left(\mu_\theta+\sigma_\theta\epsilon\right) \left(\nabla_\theta\mu_\theta +\epsilon\nabla_\theta\sigma_\theta\right) \right]. $$

The derivative inside the expectation has two contributions. Changing \(\theta\) can move the latent distribution's mean, through \(\nabla_\theta\mu_\theta\), and change its scale, through \(\nabla_\theta\sigma_\theta\). The same draw of \(\epsilon\) is held fixed when taking this derivative. Randomness is still present across draws, so a gradient calculated from a finite number of draws is generally noisy.

In practice, an encoder often outputs a mean and a log standard deviation. Write \(\rho_\theta=\log\sigma_\theta\), so \(\sigma_\theta=\exp(\rho_\theta)\). This parameterization ensures a positive scale, and the sampled latent is

$$ Z=\mu_\theta+\exp(\rho_\theta)\epsilon. $$

For a scalar parameter \(\theta\), the derivative of one sampled value is

$$ \frac{\partial Z}{\partial\theta} = \frac{\partial\mu_\theta}{\partial\theta} + \exp(\rho_\theta)\epsilon \frac{\partial\rho_\theta}{\partial\theta}. $$

This expression makes the gradient route explicit: the objective depends on the sample \(Z\), and the sample depends differentiably on the encoder parameters.

Worked Example: Generating a Gaussian Latent Draw

Suppose a hypothetical cell has an encoder distribution \(Z\sim N(2,0.25)\). Thus \(\mu=2\) and \(\sigma=0.5\), because the second argument of \(N\) is the variance. For one illustrative draw, take \(\epsilon=-1.2\).

The reparameterized draw is

$$ z=\mu+\sigma\epsilon=2+(0.5)(-1.2)=1.4. $$

The standardized value is \((z-\mu)/\sigma=(1.4-2)/0.5=-1.2\), which matches the chosen standard-normal draw. Repeating the procedure with independent draws from \(N(0,1)\) produces draws from the stated Gaussian. This example illustrates the transformation; it does not assert that this particular latent value would be observed in real single-cell data.

A One-Dimensional Encoder Gradient

Consider a hypothetical cell with count vector \(x=2\), used here as a single numerical input to a simple encoder. Let its mean be \(\mu_w(x)=wx+b\), and let its log standard deviation be \(\rho\). For fixed \(x\), a latent draw is \(z=wx+b+\exp(\rho)\epsilon\). To show the gradient calculation transparently, use the scalar function \(f(z)=-\frac{1}{2}(z-y)^2\), with \(y=1\). This function is a simple differentiable objective, not a count likelihood.

Worked Example: A Sampled Gradient Through the Encoder

Set \(w=0.3\), \(b=-0.1\), and \(\rho=-0.2\). Then \(\mu_w(2)=(0.3)(2)-0.1=0.5\), and \(\sigma=\exp(-0.2)\approx0.8187\). For the illustrative draw \(\epsilon=0.4\),

$$ z=0.5+(0.8187)(0.4)\approx0.8275. $$

The derivative of the objective with respect to the latent value is \(f'(z)=-(z-y)\), so at this draw \(f'(z)=-(0.8275-1)=0.1725\). Since \(\partial z/\partial w=x=2\), the sampled pathwise gradient with respect to \(w\) is

$$ \frac{\partial f(z)}{\partial w} = f'(z)\frac{\partial z}{\partial w} \approx(0.1725)(2)=0.3450. $$

For the log standard deviation, \(\partial z/\partial\rho=\exp(\rho)\epsilon\approx(0.8187)(0.4)=0.3275\). Therefore,

$$ \frac{\partial f(z)}{\partial\rho} = f'(z)\frac{\partial z}{\partial\rho} \approx(0.1725)(0.3275)=0.0565. $$

These are gradients for this particular noise draw, not exact derivatives of the expectation. A Monte Carlo estimate averages such gradients over independent draws of \(\epsilon\). Under the regularity conditions for exchanging derivative and expectation, that average estimates the gradient of the expected objective.

For this example, the expected objective can also be calculated exactly, which checks the meaning of the sampled gradients. Since \(Z\) has mean \(\mu\) and variance \(\sigma^2\),

$$ \mathbb{E}[f(Z)] = -\frac{1}{2}\mathbb{E}[(Z-y)^2] = -\frac{1}{2}\left((\mu-y)^2+\sigma^2\right). $$

Here \(\mu=2w+b=0.5\), \(\sigma^2=\exp(2\rho)=\exp(-0.4)\approx0.6703\), and \(y=1\), so the expected objective is approximately \(-0.46\). Its exact derivatives are

$$ \frac{\partial \mathbb{E}[f(Z)]}{\partial w} =-(\mu-y)x=-(0.5-1)(2)=1.000, \qquad \frac{\partial \mathbb{E}[f(Z)]}{\partial\rho} =-\sigma^2\approx-0.6703. $$

The single-draw gradients \(0.3450\) and \(0.0565\) need not equal these exact derivatives. Their role is to provide random estimates; averaging gradients from independent draws targets the exact derivatives.

Extension to Several Gaussian Latent Dimensions

For a diagonal Gaussian with \(d\) latent dimensions, let \(\boldsymbol{\mu}\) be a \(d\)-vector of means and \(\boldsymbol{\sigma}\) a \(d\)-vector of positive standard deviations. Generate independent standard-normal components in \(\boldsymbol{\epsilon}\), then set each latent component to \(z_j=\mu_j+\sigma_j\epsilon_j\). In vector notation, this is \(\mathbf{z}=\boldsymbol{\mu}+\boldsymbol{\sigma}\odot\boldsymbol{\epsilon}\), where \(\odot\) denotes componentwise multiplication. The chain rule applies to each component and to any differentiable scalar objective \(f(\mathbf{z})\).

Worked Example: A Two-Dimensional Pathwise Gradient

Take \(\boldsymbol{\mu}=(0.2,-0.4)\), \(\boldsymbol{\sigma}=(0.5,1)\), and a hypothetical noise draw \(\boldsymbol{\epsilon}=(1,-0.5)\). The latent draw is

$$ \mathbf{z} = (0.2,-0.4)+(0.5,1)\odot(1,-0.5) = (0.7,-0.9). $$

Let \(f(\mathbf{z})=z_1z_2\). Its derivatives with respect to the two latent components are \(\partial f/\partial z_1=z_2=-0.9\) and \(\partial f/\partial z_2=z_1=0.7\). Consequently, the sampled gradients with respect to the two means are \((-0.9,0.7)\). If \(\rho_j=\log\sigma_j\), then \(\partial z_j/\partial\rho_j=\sigma_j\epsilon_j\). Thus the sampled gradients with respect to \((\rho_1,\rho_2)\) are \(((-0.9)(0.5)(1),(0.7)(1)(-0.5))=(-0.45,-0.35)\).

This calculation separates the local derivative of the objective from the effect of each distribution parameter on its latent component. With an encoder, the mean and log standard deviation may themselves depend on many parameters; the chain rule continues from these outputs back through the encoder.

Using the Trick in an ELBO and Checking Its Assumptions

In the ELBO derived for a count VAE, one term is an expectation over the encoder's latent distribution of a function involving the count likelihood and the prior. When that function is differentiable in the latent variable, reparameterization lets a sampled latent contribute a pathwise gradient to the expectation. A KL term that has a closed-form expression can instead be differentiated directly; reparameterization is needed for an expectation estimated using latent samples, not automatically for every term in the objective.

The basic calculation relies on several conditions and implementation choices:

  • The Gaussian scale must be positive. Using \(\sigma=\exp(\rho)\) enforces this mathematically; numerical implementations should also avoid scales that underflow or become excessively large.
  • The objective must be differentiable with respect to the sampled latent, or at least differentiable almost everywhere in a way that supports the gradient calculation. Discrete sampling steps or nondifferentiable operations can interrupt the pathwise gradient.
  • Differentiating under the expectation must be justified by suitable integrability and smoothness conditions. The formula should not be assumed valid for an arbitrary unbounded or irregular objective.
  • In a Monte Carlo implementation, draw \(\epsilon\) from a parameter-independent standard normal distribution and preserve its computational connection through \(z=\mu+\sigma\epsilon\). If the sample is detached from the calculation graph, gradients cannot pass through the draw.

A useful diagnostic is to fix a noise value and compare an automatic-differentiation gradient with a finite-difference calculation that uses that same noise value. For a parameter \(\theta\), the finite difference is \([f(z(\theta+h))-f(z(\theta-h))]/(2h)\), with \(\epsilon\) held constant in both evaluations. Resampling noise for the two evaluations adds random variation to the comparison and can make a correct gradient appear wrong. This diagnostic checks the derivative of a sampled path; it does not by itself establish that a finite-sample average has low Monte Carlo error.

Alternatives, Limitations, and Common Mistakes

A principal alternative is the score-function estimator. For a differentiable density \(q_\theta(z)\), under conditions permitting differentiation under the integral, it uses \(\nabla_\theta\mathbb{E}_{q_\theta}[f(Z)]=\mathbb{E}_{q_\theta}[f(Z)\nabla_\theta\log q_\theta(Z)]\). Unlike the pathwise calculation, this expression does not require differentiating \(f\) with respect to \(z\), so it can apply when the sampled variable or objective is not differentiable in a useful way. Its estimates can have high variance, particularly if \(f(Z)\) varies substantially. The pathwise estimator exploits a differentiable transformation and is often more direct for Gaussian latents, but it also has Monte Carlo variability and depends on a valid differentiable path.

ApproachGradient routeMain consideration
Pathwise reparameterizationDifferentiate \(f(z)\) through a differentiable draw \(z=g_\theta(\epsilon)\).Requires a suitable differentiable transformation and regularity for the gradient formula.
Score-function estimatorWeight \(f(z)\) by \(\nabla_\theta\log q_\theta(z)\).Does not require differentiating through \(f\), but can have high variance.

A common mistake is to think that a sampled latent is “random, so it cannot be differentiated.” The correction is that the random input \(\epsilon\) is held fixed for the derivative, while the transformed value \(z=\mu_\theta+\sigma_\theta\epsilon\) changes with \(\theta\). Another error is to confuse variance with standard deviation: if a Gaussian is written \(N(\mu,\sigma^2)\), its sampling scale is \(\sigma\), not \(\sigma^2\). Finally, a pathwise gradient from one noise draw is not the exact gradient of the expectation; it is a stochastic estimate.

The trick does not remove the need to choose a suitable latent distribution, objective, or number of Monte Carlo draws. Nor does a successful gradient calculation show that a latent representation captures biologically meaningful structure. It addresses how gradients pass through continuous random Gaussian latents, not what conclusions should be drawn from the learned latents.

Key takeaway: Express a Gaussian latent as a differentiable function of its parameters and parameter-independent standard-normal noise. Apply the chain rule to each sampled path, and average over noise draws when estimating the gradient of an expectation.

Check Your Understanding

Try each question, then check the solutions below.

  1. If \(Z\sim N(1,4)\), what scale multiplies a standard-normal draw in the reparameterization?
  2. For \(z=\mu+\exp(\rho)\epsilon\), give \(\partial z/\partial\rho\).
  3. Why should the same \(\epsilon\) be used in both sides of a finite-difference check of a sampled path?
  4. Name one situation where the score-function estimator may be useful even if the pathwise estimator is not readily available.

Solutions. (1) The variance is \(4\), so the standard deviation is \(2\), and \(Z=1+2\epsilon\). (2) \(\partial z/\partial\rho=\exp(\rho)\epsilon\). (3) Holding \(\epsilon\) fixed isolates the change in the transformed sample caused by changing the parameter; resampling would add noise to the finite difference. (4) It may be useful when the sampled variable or objective does not provide a suitable differentiable path, because its formula does not require differentiating \(f\) with respect to the sample.

Clinical Biostats