Dive into Deep Learning · §34.7
Expectations and Their Gradients
differentiation under the integral sign · Leibniz rule · score-function (REINFORCE) · pathwise reparameterization · variance trade-offs
Motivation
Foundations
Exchanging Limits
Continuous Softmax
Moving Support
\frac{d}{d\theta}\int_{a(\theta)}^{b(\theta)} f(x, \theta)\,dx = \int_{a(\theta)}^{b(\theta)} \frac{\partial f}{\partial \theta}(x, \theta)\,dx + f\bigl(b(\theta), \theta\bigr)b'(\theta) - f\bigl(a(\theta), \theta\bigr)a'(\theta)
Moving-Support Pitfall
The Monte Carlo Obstacle
Score-Function Identity
Baseline Subtraction
Differentiating Through Samples
Differentiability Boundary
Exact Moment Calculation
Empirical Benchmark
Drawing n = 200{,}000 samples at \theta = 1.5 confirms that all three estimators are unbiased for \ell'(1.5) = 3.0000 and reproduces the exact theoretical standard deviations 2.000, 7.181, and 4.899:
true derivative 2*theta = 3.0
pathwise mean=3.0041 sd=2.000 (exact 2.000) se=0.0045
score mean=3.0068 sd=7.146 (exact 7.181) se=0.0160
score+baseline mean=2.9961 sd=4.858 (exact 4.899) se=0.0109
High-Frequency Regime
Does the pathwise estimator always have lower variance than the score-function estimator? No. Consider the oscillating loss g(x) = \sin(\omega x), where \frac{d}{d\theta}\mathbb{E}[\sin(\omega X)] = \omega\cos(\omega\theta)e^{-\omega^2/2}:
omega=1 true=+0.0429 pathwise +0.0414 (sd 0.66) score +0.0432 (sd 0.54)
omega=3 true=-0.0070 pathwise -0.0067 (sd 2.12) score -0.0080 (sd 0.71)
Design Trade-Offs
| Property / Requirement | Score-Function (REINFORCE) | Pathwise (Reparameterization) | Relaxed / Straight-Through (e.g. Gumbel-Softmax) |
|---|---|---|---|
| Core Formula | g(X)\,\nabla_{\boldsymbol{\theta}}\log p_{\boldsymbol{\theta}}(X) | \nabla_{\boldsymbol{\theta}}g(T_{\boldsymbol{\theta}}(\mathbf{z})) = \mathbf{J}_{T}^\top \nabla_x g | Differentiate smooth surrogate z_\tau = \sigma((\theta+\zeta)/\tau) |
| Requires Differentiable g? | No (works on steps & black-box rewards) | Yes (g must be differentiable/Lipschitz) | Yes (surrogate loss must be differentiable) |
| Handles Discrete X? | Yes (Bernoulli, categorical actions) | No (no continuous path T_{\boldsymbol{\theta}}(\mathbf{z})) | Yes (approximates discrete choice at temperature \tau) |
| Bias | Unbiased (on fixed support) | Unbiased (for smooth g) | Biased (bias shrinks as \tau \to 0, variance blows up) |
| Single-Sample Variance | Scales with \|g(X)\|^2 (needs baseline b) | Scales with \|\nabla_x g(X)\|^2 (low for smooth g) | Controlled by temperature \tau (bias–variance trade-off) |
Review & Practice
Work out these three questions on the Leibniz rule, score function, and pathwise estimators:
Review & Practice
Answers:
Key Takeaways
| Rule / Estimator | Mathematical Identity | Conditions & Variance Behavior |
|---|---|---|
| Interior Swap | \frac{d}{d\theta}\int f(x,\theta)\,dx = \int \frac{\partial f}{\partial \theta}(x,\theta)\,dx | Fixed limits; continuous \partial_\theta f + integrable dominating bound |
| Leibniz Boundary Rule | \frac{d}{d\theta}\int_{a(\theta)}^{b(\theta)} f\,dx = \int_a^b \partial_\theta f\,dx + f(b,\theta)b' - f(a,\theta)a' | Required whenever integration limits / support move with \theta |
| Log-Integral-Exp | \frac{d}{d\theta}\log\int e^{\theta x}p(x)\,dx = \mathbb{E}_{\tilde{p}_\theta}[X] (\tilde{p}_\theta \propto e^{\theta x}p) | Continuous counterpart of \nabla\operatorname{LSE}(\mathbf{z}) = \operatorname{softmax}(\mathbf{z}) |
| Score Function (REINFORCE) | \nabla_{\boldsymbol{\theta}}\mathbb{E}_{p_{\boldsymbol{\theta}}}[g(X)] = \mathbb{E}_{p_{\boldsymbol{\theta}}}\bigl[(g(X)-b)\nabla_{\boldsymbol{\theta}}\log p_{\boldsymbol{\theta}}(X)\bigr] | Works for discontinuous g & discrete X; variance scales with \|g-b\|^2 |
| Optimal Baseline b^\star | b^\star = \mathbb{E}[g(X)s_\theta(X)^2] / \mathbb{E}[s_\theta(X)^2] | Unbiased because \mathbb{E}[\nabla_{\boldsymbol{\theta}}\log p_{\boldsymbol{\theta}}(X)] = \mathbf{0} on fixed support |
| Pathwise (Reparameterized) | \nabla_{\boldsymbol{\theta}}\mathbb{E}_{p_{\boldsymbol{\theta}}}[g(X)] = \mathbb{E}_{\mathbf{z}\sim q}\bigl[\nabla_{\boldsymbol{\theta}}g(T_{\boldsymbol{\theta}}(\mathbf{z}))\bigr] | Requires smooth g & continuous X = T_{\boldsymbol{\theta}}(\mathbf{z}); variance scales with \|\nabla g\|^2 |