Sampling by Integration
Dive into Deep Learning · §17.3
Reverse-Time Integration: ODEs and Ancestral Chains
continuity equation · probability-flow ODE · Euler steps & data mean · path-agnostic coordinates · ancestral chain & reverse SDE
Why Following a Velocity Field Recovers Mode Weights
Motivation
- Fixed-level Langevin failure: Local scores \nabla\log p(\mathbf{x}) keep p stationary once reached, but offer zero global flux across the 10\text{--}12 nat valleys separating modes.
- Continuous transport from a known prior: Reverse-time integration transports a known prior p_1 = \mathcal{N}(\mathbf{0}, \mathbf{I}) continuously along p_t down to p_0.
- Conservation of probability mass: Because no trajectories are created or destroyed, the fraction of Gaussian starts landing in mode k equals \pi_k.
Our Two Trained Predictors and the Exact Oracle
Experimental Setup
To separate discretization error from neural network approximation error, we benchmark two trained TimeMLP(256, 3) models against the exact mixture oracle:
loss over the last 1,000 steps: velocity 4.919, noise 0.537
- Linear-path velocity network (
ema_v): Trained on \boldsymbol\epsilon - \mathbf{x}_0 along \mathbf{x}_t = (1 - t)\mathbf{x}_0 + t\boldsymbol\epsilon; reaches the theoretical velocity floor (\approx 4.91).
- VP-path noise network (
ema_eps): Trained on \boldsymbol\epsilon along the variance-preserving schedule; reaches the theoretical noise floor (\approx 0.54).
- Exact posterior oracle (
oracle): Evaluates the closed-form mixture posterior means (\hat{\mathbf{x}}_0, \hat{\boldsymbol\epsilon}) at any (\mathbf{x}, \alpha_t, \sigma_t).
Conservation of Mass and the Continuity Equation
Probability Transport
\frac{\partial p_t(\mathbf{x})}{\partial t} = -\nabla \cdot \big(p_t(\mathbf{x})\,\mathbf{u}_t(\mathbf{x})\big)
- Mass flux vector \mathbf{J}_t = p_t(\mathbf{x})\mathbf{u}_t(\mathbf{x}): Measures probability mass crossing a unit surface per unit time; its negative divergence is the rate of density increase.
- Exact marginal transport theorem: If (p_t, \mathbf{u}_t) satisfy the continuity equation, particles starting from \mathbf{x}_1 \sim p_1 and evolving via d\mathbf{x}/dt = \mathbf{u}_t(\mathbf{x}) have exact marginal density \mathbf{x}_t \sim p_t for all t \in [0, 1].
Proof: The Marginal Velocity Satisfies Continuity
From Conditional Clouds to Marginal Density
\mathbb{E}_{\mathbf{x}_0}\big[p_t(\mathbf{x} \mid \mathbf{x}_0)\,\mathbf{u}_t(\mathbf{x} \mid \mathbf{x}_0)\big] = p_t(\mathbf{x})\,\mathbf{u}_t(\mathbf{x})
- Step 1: Conditional conservation: For each fixed \mathbf{x}_0, the Gaussian cloud p_t(\mathbf{x} \mid \mathbf{x}_0) satisfies \partial_t p_t(\mathbf{x} \mid \mathbf{x}_0) = -\nabla \cdot \big(p_t(\mathbf{x} \mid \mathbf{x}_0)\mathbf{u}_t(\mathbf{x} \mid \mathbf{x}_0)\big).
- Step 2: Bayes’ rule averaging: Using p(\mathbf{x}_0)p_t(\mathbf{x} \mid \mathbf{x}_0) = p_t(\mathbf{x})p(\mathbf{x}_0 \mid \mathbf{x}_t = \mathbf{x}) factors p_t(\mathbf{x}) out of the expectation, yielding \partial_t p_t = -\nabla \cdot (p_t\mathbf{u}_t) by linearity.
Why ODE Trajectories Never Cross Conditional Lines
Geometry of the Probability-Flow ODE
\frac{d\mathbf{x}}{dt} = \mathbf{u}_t(\mathbf{x}), \qquad t: 1 \to 0
- Conditional lines cross everywhere: Straight chords \mathbf{x}_t = (1-t)\mathbf{x}_0 + t\boldsymbol\epsilon from different modes intersect throughout \mathbb{R}^d \times (0, 1).
- Marginal ODE trajectories never intersect: Because \mathbf{u}_t(\mathbf{x}) assigns a single velocity vector to each (\mathbf{x}, t), Picard–Lindelöf uniqueness bends trajectories around intersections into a smooth bijection \mathcal{N}(\mathbf{0}, \mathbf{I}) \leftrightarrow p_0.
Euler’s Method and the Single-Step Mean Collapse
Discretizing the Probability-Flow ODE
\mathbf{u}_1(\mathbf{x}_1) = \mathbf{x}_1 - \mathbb{E}[\mathbf{x}_0] \quad \Longrightarrow \quad \mathbf{x}_1 - \mathbf{u}_1(\mathbf{x}_1) = \mathbb{E}[\mathbf{x}_0]
- Euler discretization: \mathbf{x}_{t_{k-1}} = \mathbf{x}_{t_k} - \frac{1}{K}\mathbf{u}_{t_k}(\mathbf{x}_{t_k}) across K steps.
- Why K = 1 collapses on general data: At t = 1, \mathbf{x}_1 = \boldsymbol\epsilon is independent of \mathbf{x}_0, so the posterior mean is the global data mean \mathbb{E}[\mathbf{x}_0] and a single Euler jump sends every starting point to \mathbb{E}[\mathbf{x}_0] = (-0.50, -0.70).
Tracing Five Starts Under 1, 10, and 100 Euler Steps
Exact Velocity Field Trajectories
Integrating the exact mixture field mix.velocity from five fixed initial points, the origin (0, 0) and (\pm 1.5, 0), (0, \pm 1.5), illustrates how multi-step curvature partitions the prior:
start 1 step 10 steps 100 steps
( 0.00, 0.00) (-0.50, -0.70) (-1.91, -1.27) (-1.98, -1.30)
( 1.50, 0.00) (-0.50, -0.70) ( 2.73, -1.34) ( 2.90, -1.36)
(-1.50, 0.00) (-0.50, -0.70) (-2.92, -1.38) (-3.06, -1.40)
( 0.00, 1.50) (-0.50, -0.70) (-0.11, 2.59) (-0.09, 2.77)
( 0.00, -1.50) (-0.50, -0.70) (-1.98, -2.06) (-2.05, -2.17)
- 1 step: All five starts collapse identically onto \mathbb{E}[\mathbf{x}_0] = (-0.50, -0.70) in the empty valley between modes.
- 10 vs. 100 steps: By 10 steps, every trajectory has committed to a mode (within \approx 0.2 of its 100-step endpoint, biased slightly toward the global mean).
- Mass-conserving basins of attraction: Three of the five starts (including the origin) flow into the heaviest mode (\pi_1 = 0.5 at (-2.5, -1.5)), while (1.5, 0) flows to mode 2 and (0, 1.5) to mode 3.
Path-Agnostic Coordinates: Rescaling by α_t
Unifying ODE Integration Across Schedules
\bar{\mathbf{x}}_t = \frac{\mathbf{x}_t}{\alpha_t} = \mathbf{x}_0 + \bar\sigma_t\,\boldsymbol\epsilon, \qquad \frac{d\bar{\mathbf{x}}}{d\bar\sigma} = \hat{\boldsymbol\epsilon}(\bar{\mathbf{x}}, \bar\sigma)
- Universal VE coordinates (Karras et al., 2022): Dividing \mathbf{x}_t by \alpha_t with noise ratio \bar\sigma_t = \sigma_t/\alpha_t = e^{-\lambda_t/2} absorbs the linear drift so every path obeys d\bar{\mathbf{x}}/d\bar\sigma = \hat{\boldsymbol\epsilon}.
- Path-agnostic Euler update (= DDIM at \eta = 0): Stepping from \bar\sigma_t to \bar\sigma_s and multiplying by \alpha_s gives \mathbf{x}_s = \alpha_s\hat{\mathbf{x}}_0 + \sigma_s\hat{\boldsymbol\epsilon}.
Implementing Path-Agnostic ODE Sampling
Universal Predictor & Euler Loop
Pairing make_pred (which maps any network output to (\hat{\mathbf{x}}_0, \hat{\boldsymbol\epsilon}) at matched log-SNR) with ode_sample lets any solver integrate any trained model on any schedule:
- Handling pure noise (\alpha_1 = 0, \bar\sigma_1 = \infty): At t = 1 on the linear path, \bar{\mathbf{x}} = \mathbf{x}/\alpha_t is infinite, but the update \mathbf{x}_s = \alpha_s\hat{\mathbf{x}}_0 + \sigma_s\hat{\boldsymbol\epsilon} only needs (\hat{\mathbf{x}}_0, \hat{\boldsymbol\epsilon}) evaluated at (\mathbf{x}_1, \alpha_1 = 0, \sigma_1 = 1).
- Why noise models need \alpha_1 > 0: Because
'eps' recovers \hat{\mathbf{x}}_0 = (\mathbf{x}_t - \sigma_t\hat{\boldsymbol\epsilon})/\alpha_t, it cannot start at \alpha_1 = 0; on the VP path, \alpha_1 \approx 0.007 > 0.
- Equivalence on the linear path: Substituting \alpha_s = 1 - s and \sigma_s = s gives \mathbf{x}_s = (1 - s)\hat{\mathbf{x}}_0 + s\hat{\boldsymbol\epsilon} = \mathbf{x}_t - (t - s)(\hat{\boldsymbol\epsilon} - \hat{\mathbf{x}}_0), which is identical to explicit Euler in t.
- Exactness on 1-point data: Unlike Euler in t on the VP path, \mathbf{x}_s = \alpha_s\hat{\mathbf{x}}_0 + \sigma_s\hat{\boldsymbol\epsilon} integrates single-point data with zero discretization error for any step size.
Sampling the Learned Velocity Field at 1, 10, 100 Steps
Empirical ODE Convergence
Running ode_sample on our trained linear-velocity network ema_v from 2{,}000 Gaussian starts shows how discretization error vanishes as K increases:
one step: mean [-0.511, -0.736] std [0.036, 0.027]
data: {'weights': [0.517, 0.292, 0.192], 'stray': 0.0145, 'spread': 0.488}
10 steps, network: {'weights': [0.522, 0.273, 0.206], 'stray': 0.0315, 'spread': 0.447}
100 steps, network: {'weights': [0.512, 0.278, 0.211], 'stray': 0.0125, 'spread': 0.475}
10 steps, oracle: {'weights': [0.522, 0.273, 0.206], 'stray': 0.019, 'spread': 0.459}
ode_sample against the loop on the exact field: 9e-05
Disentangling Discretization Error from Network Error
Analyzing the 10-Step vs. 100-Step Runs
- 10 Euler steps (trained MLP vs. exact oracle): Both narrow the within-mode spread (0.45 and 0.46 vs. 0.48\text{--}0.49 for the data), so the narrowing is solver discretization, not network error. Strays rise to \approx 3\% (network) and 2\text{--}3\% (oracle) from the data’s 1.1\text{--}1.5\%.
- 100 Euler steps: Matches the data’s mode weights within sampling error, its spread (0.48), and its strays (\approx 1.3\%).
DDPM Ancestral Sampling: First-Order Decomposition
Stochastic Reverse Markov Chain
\mathbf{x}_s \approx \underbrace{\Big[\mathbf{x}_t + \frac{\beta}{2}\nabla \log p_t + \sqrt{\beta}\,\mathbf{z}\Big]}_{\text{Langevin step } (h = \beta)} + \underbrace{\frac{\beta}{2}\big(\mathbf{x}_t + \nabla \log p_t\big)}_{\text{Flow ODE Euler step}}
- First-order expansion (1/\sqrt{1-\beta} \approx 1 + \beta/2): Splitting the drift of DDPM’s reverse step reveals two simultaneous updates.
- Structural insight: Every ancestral step is 1 Euler step of the probability-flow ODE (transporting p_t \to p_s) plus 1 Langevin MCMC step (damping stray drift errors).
Continuous Time: Reverse SDE vs. Probability-Flow ODE
Anderson’s Time-Reversal Theorem (1982)
d\mathbf{x} = \big[\mathbf{f}(\mathbf{x}, t) - g(t)^2\nabla_{\mathbf{x}} \log p_t(\mathbf{x})\big]\,dt + g(t)\,d\bar{\mathbf{W}}
- Reverse-Time SDE (stochastic): Discretized by DDPM’s ancestral chain; fresh Brownian noise g(t)\,d\bar{\mathbf{W}} continuously mixes trajectories and damps score errors.
- Probability-Flow ODE (deterministic): Drops g\,d\bar{\mathbf{W}} and halves the score term (g^2 \to \frac{1}{2}g^2) to define an invertible bijection \mathbf{x}_1 \leftrightarrow \mathbf{x}_0 sharing the exact same marginals p_t(\mathbf{x}).
Paired Benchmark: 20,000 Starts vs. Exact Oracle
Ancestral Chain vs. Probability-Flow ODE
We run 200 ancestral steps (ema_eps on VP) and 100 Euler ODE steps (ema_v on linear) from 20{,}000 shared Gaussian starts \mathbf{x}_1, paired with exact oracle runs using identical initial points and injected noise:
data: {'weights': [0.504, 0.299, 0.197], 'stray': 0.0103, 'spread': 0.476}
ancestral, network: {'weights': [0.502, 0.296, 0.202], 'stray': 0.0121, 'spread': 0.483}
ancestral, oracle: {'weights': [0.503, 0.296, 0.201], 'stray': 0.0121, 'spread': 0.482}
Euler, network: {'weights': [0.499, 0.305, 0.196], 'stray': 0.0127, 'spread': 0.471}
Euler, oracle: {'weights': [0.499, 0.304, 0.197], 'stray': 0.0107, 'spread': 0.474}
standard error of a weight: [0.0035, 0.0032, 0.0028]
ancestral: network and oracle end in different modes for 0.38% of the starts
Euler: network and oracle end in different modes for 0.21% of the starts
Comparing ODE and Ancestral Samplers on the Mixture
Interpreting the 20,000-Sample Experiment
With N = 20{,}000 samples, the binomial standard error on a mode weight is \sqrt{w(1-w)/N} \approx 0.0035, allowing a sharp test of distributional accuracy:
- Sub-1% mode disagreement with the exact oracle: For 99.5\% to 99.8\% of the 20{,}000 starting points, the trained neural network and the exact analytical posterior mean send the trajectory to the exact same mode.
- Mode weights within sampling noise: Both the 200-step ancestral chain and the 100-step Euler ODE match the oracle’s mode weights within \le 0.004 (\approx 1\,\mathrm{SE}), within-mode spread within 0.01, and stray fraction (\approx 1.1\%).
- Deterministic map vs. stochastic correction: The ODE maps each initial \mathbf{x}_1 deterministically to a unique \mathbf{x}_0 (enabling inversion and latent interpolation in §17.5), while the ancestral chain injects Langevin noise at every step.
Synthesis: The Generative Models of Chapter 17
Unifying Path, Target, and Sampler
Every diffusion and flow-matching model in the literature is specified by three modular choices—a forward path, a regression target, and a reverse integrator:
| Single-Level DSM |
Fixed blur \mathbf{x}_0 + \sigma\boldsymbol\epsilon |
Score -\boldsymbol\epsilon/\sigma |
Fixed-\sigma Langevin MCMC |
§17.1 |
| NCSN (Song & Ermon, 2019) |
Geometric VE ladder \tilde\sigma_i |
Score -\boldsymbol\epsilon/\tilde\sigma_i (weight \tilde\sigma_i^2) |
Annealed Langevin ladder |
§17.2 |
| DDPM (Ho et al., 2020) |
Linear-\beta VP (\alpha_t^2 + \sigma_t^2 = 1) |
Noise \boldsymbol\epsilon |
Ancestral chain (reverse SDE) |
§17.2–17.3 |
| Flow Matching / Rectified Flow |
Linear (1 - t)\mathbf{x}_0 + t\boldsymbol\epsilon |
Velocity \boldsymbol\epsilon - \mathbf{x}_0 |
Euler / Heun on flow ODE |
§17.2–17.5 |
| \mathbf{v}-Prediction (Salimans & Ho) |
Cosine VP (\cos\frac{\pi t}{2}, \sin\frac{\pi t}{2}) |
Angular velocity \mathbf{v} = \alpha_t\boldsymbol\epsilon - \sigma_t\mathbf{x}_0 |
ODE or ancestral sampler |
§17.2, §17.6 |
| EDM (Karras et al., 2022) |
VE coordinates \bar{\mathbf{x}} = \mathbf{x}_0 + \bar\sigma\boldsymbol\epsilon |
Preconditioned clean point \hat{\mathbf{x}}_0 |
2nd-order Heun on \bar\sigma^{1/\rho} grid |
§17.5–17.6 |
Concept Check: Transport, Euler Steps & SDE Split
Check Your Understanding
Test your understanding of reverse-time integration and sampler mechanics:
- Single Euler step from pure noise: On our three-mode mixture with means (-2.5, -1.5), (2.5, -1.5), (0, 2.5) and weights (0.5, 0.3, 0.2), where does one Euler step \mathbf{x}_0 = \mathbf{x}_1 - \mathbf{u}_1(\mathbf{x}_1) send any initial point \mathbf{x}_1?
- Error amplification near pure noise: On the linear path, \hat{\mathbf{x}}_0 = (\mathbf{x}_t - t\hat{\boldsymbol\epsilon})/(1 - t). If a noise predictor \hat{\boldsymbol\epsilon} has error \boldsymbol\delta at t = 0.95, how large is the resulting error in the velocity \mathbf{u}_t = \hat{\boldsymbol\epsilon} - \hat{\mathbf{x}}_0?
- Why reverse SDE and flow ODE share marginals: By what term do the backward steps of the reverse SDE and probability-flow ODE differ, and why does that term leave p_t unchanged?
Answers:
- Since \mathbf{u}_1(\mathbf{x}_1) = \mathbf{x}_1 - \mathbb{E}[\mathbf{x}_0], every starting point \mathbf{x}_1 collapses onto the global data mean \mathbb{E}[\mathbf{x}_0] = 0.5(-2.5, -1.5) + 0.3(2.5, -1.5) + 0.2(0, 2.5) = \mathbf{(-0.50, -0.70)}.
- Since \mathbf{u}_t = (\hat{\boldsymbol\epsilon} - \mathbf{x}_t)/(1 - t), an error \boldsymbol\delta in \hat{\boldsymbol\epsilon} is magnified by 1/(1 - t) = 1/0.05 = \mathbf{20\times} (\mathbf{20\,\boldsymbol\delta}), and diverges at t = 1 (\alpha_1 = 0).
- Their backward steps differ by \frac{1}{2}g(t)^2\nabla \log p_t(\mathbf{x})\,\Delta t + g(t)\sqrt{\Delta t}\,\mathbf{z}, which is an infinitesimal Langevin step of step size h = g(t)^2\Delta t whose stationary distribution is p_t.
Summary: Reverse-Time Integration Mechanics
Key Takeaways
| Continuity Equation |
\partial_t p_t = -\nabla \cdot (p_t \mathbf{u}_t) |
Conserves total probability mass |
Ensures \mathbf{u}_t = \mathbb{E}[\dot{\mathbf{x}}_t \mid \mathbf{x}_t] recovers exact mode weights |
| Probability-Flow ODE |
d\mathbf{x}/dt = \mathbf{u}_t(\mathbf{x}) (t: 1 \to 0) |
Smooth deterministic bijection \mathbf{x}_1 \leftrightarrow \mathbf{x}_0 |
Reproducible samples, latent codes, exact likelihoods |
| 1-Step Euler Collapse |
\mathbf{x}_1 - \mathbf{u}_1(\mathbf{x}_1) = \mathbb{E}[\mathbf{x}_0] |
Tangent at t = 1 points to prior mean |
Collapses all starts to \mathbb{E}[\mathbf{x}_0]; curved paths need \ge 10 steps |
| Rescaled VE Coordinates |
\bar{\mathbf{x}} = \mathbf{x}/\alpha_t, \;\bar\sigma = \sigma_t/\alpha_t |
d\bar{\mathbf{x}}/d\bar\sigma = \hat{\boldsymbol\epsilon}(\bar{\mathbf{x}}, \bar\sigma) on any path |
Euler step \mathbf{x}_s = \alpha_s\hat{\mathbf{x}}_0 + \sigma_s\hat{\boldsymbol\epsilon} (= DDIM at \eta = 0) |
| DDPM Ancestral Step |
\frac{\mathbf{x}_t + \beta\nabla \log p_t}{\sqrt{1-\beta}} + \sqrt{\beta}\mathbf{z} |
= \text{Flow ODE Euler step} + \text{Langevin step} |
Discretizes Anderson’s reverse SDE; damps drift errors |