Derive a gradient estimator for an expected loss whose sampled latent variable depends on trainable distribution parameters.
Problem
Context. A Gaussian latent variable has trainable mean and positive standard deviation, and a differentiable loss depends on its sample. Training requires the derivative of the expected loss, not merely a derivative of the density.
Task. Express sampling through parameter-independent noise, derive the pathwise gradient, and explain how Monte Carlo estimation uses it. Compare what can change for discrete samples or nondifferentiable transformations.
Constraints. State conditions permitting differentiation under the expectation. Hold base noise fixed during a sample’s derivative; distinguish that sample estimate from the exact expectation gradient. Do not assume every distribution or objective supports the same transformation.
The problem
Let for fixed x. A sampler returning a detached draw supplies no ordinary local autodiff path to its distribution parameters. This is a property of the representation and estimator, not proof that every stochastic expectation is nondifferentiable.
The trick
Reparameterize the sample:
z = mu_phi(x) + sigma_phi(x) * epsilon, where epsilon ~ N(0, I)
For positive standard deviation sigma, the Gaussian sample has the desired distribution while its transform is differentiable. When differentiation and expectation can be interchanged,
If f also depends explicitly on phi, include that additional derivative. For , , , and , the sample is two and loss one. The local derivative is , so and for this fixed draw.
The expected loss instead is , with derivatives . Noise values minus one and plus one give sample gradients and , averaging to . That two-point check matches this quadratic’s moments; it is not Gaussian sampling or a general integration rule.
Learning objective
Hold the random draw fixed while gradients pass through its parameterized transformation
Derivation
Is another noise sample needed to differentiate this one transformation?
Lower-variance gradients
Pathwise estimates can reduce variance relative to score-function estimates, but no distribution-free variance ordering or sample-count guarantee holds. Score-function methods, baselines, analytic terms, and other estimators remain useful under their respective conditions.
What an L5 answer sounds like
A complete explanation states the noise law, transform, held-fixed sample, distributional identity, and interchange assumptions before applying the chain rule.
What an L6 answer adds
Change the Encoder’s Scale Output
New task: The encoder emits log variance r instead of standard deviation sigma.
Picture and change: Use and retain the same epsilon, chaining through .
Check: At , sigma is two; the reference sample’s . Using variance four as the standard deviation changes the distribution and derivative.
Use when: An unconstrained encoder output needs conversion to positive scale.
Change the Latent Support
New task: Replace the Gaussian latent with Bernoulli probability p.
Picture and change: Thresholding uniform noise gives , whose sample derivative is zero almost everywhere.
Check: Nevertheless, has derivative one; averaging those zero derivatives fails to recover it.
Use when: Discrete support breaks the smooth sample-path argument. Use a suitable score-function estimator or a justified relaxation, not a Gaussian substitution.
For framework use, keep mu/log-variance/noise axes aligned, commonly [B,D], and preserve the draw’s differentiable transform.
PyTorch Gaussian rsample() supplies that path; sample() ordinarily does not.
K samples require noise/transform work plus downstream computation.
Detaching z blocks the path; fresh noise invalidates the finite-difference comparison.
Tells that get you a strong-hire vote
Explain the sample derivative, expected derivative, and their different numerical values rather than presenting a level speech.
Tells that get you down-leveled
Avoid claiming that all stochastic nodes are intrinsically nondifferentiable or that pathwise gradients always have lower variance. Gumbel-softmax differentiates a relaxed objective; annealing temperature does not guarantee unbiased discrete-objective gradients.
Common follow-up
SAC can differentiate a reparameterized continuous-action sample through a learned critic under its objective. DDPG instead uses a deterministic policy gradient; it is not the same Gaussian sampling construction. Discrete-action policies can use score-function estimators and baselines; algorithm choice also depends on data, constraints, and learning assumptions. DPO uses a preference-data objective rather than backpropagating through generated discrete tokens; this does not make all data collection unnecessary.
Related: RLHF and DPO, cross-entropy and softmax, and Bayesian versus frequentist.