M5.3 Adjusting a Sample-Producing Machine: Differentiating a Generator
Builds on3 notes
A machine turning noise into products
A differentiable program transforms noise into products . We know the noise and program, but may not know the output density .
We want the whole output distribution to match a target , rather than making every product identical. Which parameter update improves that match?
Fixing the same noise and changing parameters reveals how samples move. Can those movements give the gradient of a distributional discrepancy?
DiscussionQ1
How does the output law depend on parameters, and what makes direct differentiation difficult?
Show the reasoning
The output law is the pushforward of fixed through . Compare it with the target using
This direction, , penalizes generated mass placed where the target density is too small. The choice also affects treatment of missing modes.
Both the sampling law and depend on parameters. A black-box sampler does not reveal how individual samples move. Reparameterization provides that path, though it is not the only gradient-estimation method.
Write the expectation over fixed noise , then place all sampling dependence in a differentiable . This is reparameterization.
Fix the random draws and adjust the machine
Imagine pre-drawing . For fixed parameters each output is now deterministic. The derivative describes how a parameter change moves sample .
At each sample, the spatial slope of describes the local mismatch. Pulling this slope back through the generator derivative converts a sample-space direction into a parameter gradient. The density itself also depends explicitly on parameters, so we must account for that term too.
The resulting expression averages spatial score differences multiplied by parameter sensitivities. The following derivation explains why an additional term vanishes in expectation.
Fixed noise exposes sample motion; generator derivatives convert spatial scores into parameter updates.
Derive it in one dimension
First use scalar samples and parameters. Assume differentiable , suitable smooth densities, finite KL, and justified exchanges of derivatives and integrals. The support must be parameter-independent or moving-boundary contributions correctly handled. Then
Differentiate inside the fixed-noise expectation. Parameters occur both through and explicitly in the model-density index:
Term differentiates the density parameter at fixed , not along the generated sample path. Under the regularity assumptions its expectation is zero:
Changing the variable name from to gives the first equality; differentiating normalization gives the final zero. Therefore
For -dimensional samples and parameters, gives , where . The result has parameter dimension .
A spatial score points toward increasing log density. An -independent normalizer disappears under this derivative, but is not thereby calculated. The generator supplies the Jacobian; model and target distributions supply the scores.
DetailsZero-mean terms and estimator variance
Another identity is the parameter-score estimator for parameter-independent f under appropriate assumptions. Its parameter derivative differs from the spatial score above and can handle discrete outcomes. There is no universal variance ordering against pathwise estimators [3].
Removing zero-mean preserves unbiasedness. It does not universally reduce variance, since
At exact model-target equality, vanishes samplewise, so removing eliminates this noise source [4].
What information does the formula need?
At each generated sample, obtain three quantities:
- Generator Jacobian: available through differentiation of the program.
- Target spatial score: analytic if the target density is known; target samples alone do not directly supply it. Score estimation is another learning problem [5].
- Model spatial score: knowing a generator does not generally expose its density or score. An invertible generator may permit change of variables; a general generator may require estimation or may not even induce an ambient-space density.
Two scores do not necessarily require two additional neural networks: analytic expressions and other estimators may exist. Bypassing normalizers in spatial derivatives does not evaluate likelihoods or KL values.
Check differentiable sample paths, score accuracy at generated locations, and the KL direction. Discrete outputs need other estimators or relaxations. Reverse KL can tolerate missing target modes, so a mode-coverage goal may call for a different discrepancy.
A Gaussian calculation with and without the extra term
Let , , and . KL is . Spatial scores are and , whose difference is exactly for every sample. Generator sensitivity is one, so has no sampling variation here.
import numpy as np
rng = np.random.default_rng(0)
theta, target, lr = 0., 5., .1
for step in range(60):
z = rng.standard_normal(256)
x = theta+z
score_model = -(x-theta)
score_target = -(x-target)
grad_A = np.mean(score_model-score_target)
grad_B = np.mean(x-theta) # Parameter derivative at fixed x; mean zero.
theta -= lr*grad_A
if step%15==0:
print(step,theta,grad_A,grad_B)
print(theta)
The update approaches five. Each batch’s equals the exact gradient, while fluctuates with mean zero. Including would add noise in this example; that observation does not establish a universal variance comparison.
Estimated scores can be inaccurate in sparse tails. Nonlinearity alone does not imply no analytic density: has a shifted half-normal law, but its moving support invalidates the simple zero-mean proof without boundary treatment. A low-dimensional latent mapped into higher dimensions can induce a singular law, making the displayed density-based KL undefined or infinite.
Switch generators and target information to inspect how score differences move parameters.
Check your understanding
References
- Kingma, D. P., Welling, M. Auto-Encoding Variational Bayes. ICLR 2014.
- Rezende, D. J., Mohamed, S., Wierstra, D. Stochastic Backpropagation and Approximate Inference in Deep Generative Models. ICML 2014.
- Mohamed, S., Rosca, M., Figurnov, M., Mnih, A. Monte Carlo Gradient Estimation in Machine Learning. JMLR 21, 2020.
- Roeder, G., Wu, Y., Duvenaud, D. Sticking the Landing: Simple, Lower-Variance Gradient Estimators for Variational Inference. NeurIPS 2017.
- Hyvärinen, A. Estimation of Non-Normalized Statistical Models by Score Matching. JMLR 6, 2005.