Dive into Deep Learning · §38.6
Denoising Score Matching and Score-Based Diffusion Models
Hyvärinen & Vincent theorems · Tweedie’s formula · Langevin mixing · DDPM & DDIM · classifier-free guidance
Motivation
Both generative reverse processes, Anderson’s reverse-time SDE and the probability-flow ODE, require only one unknown quantity: the score \nabla_{\mathbf{x}}\log p_t(\mathbf{x}) of the noised data distribution.
Hyvärinen’s Identity (2005)
Expanding the square in J_{\text{ESM}}(\boldsymbol{\theta}) and integrating the cross term -\int \mathbf{s}_{\boldsymbol{\theta}}(\mathbf{x}) \cdot (\nabla\log p(\mathbf{x}))\,p(\mathbf{x})\,d\mathbf{x} = -\int \mathbf{s}_{\boldsymbol{\theta}} \cdot \nabla p\,d\mathbf{x} by parts moves the derivative off the unknown data density p and onto the network \mathbf{s}_{\boldsymbol{\theta}}:
J_{\text{ESM}}(\boldsymbol{\theta}) = \mathbb{E}_{\mathbf{x} \sim p}\!\left[\frac{1}{2}\|\mathbf{s}_{\boldsymbol{\theta}}(\mathbf{x})\|^2 + \nabla \cdot \mathbf{s}_{\boldsymbol{\theta}}(\mathbf{x})\right] + C, \qquad C = \frac{1}{2}\mathbb{E}_p\|\nabla\log p(\mathbf{x})\|^2
Sanity Check
For data x \sim \mathcal{N}(0, v) and linear score model s_w(x) = -x/w, the implicit objective is J(w) = \mathbb{E}[\frac{1}{2}s_w^2 + s_w'] = \frac{v}{2w^2} - \frac{1}{w}, whose derivative -\frac{v}{w^3} + \frac{1}{w^2} = 0 recovers w = v from samples alone:
implicit score matching picks w = 2.03 (v = 2.0)
Foundational Identity
For any pair (X, Y) with conditional expectation \mathbf{m}(X) = \mathbb{E}[Y \mid X], the tower rule eliminates the cross term:
\mathbb{E}\!\left[\|\mathbf{v}(X) - Y\|^2\right] = \mathbb{E}\!\left[\|\mathbf{v}(X) - \mathbf{m}(X)\|^2\right] + \mathbb{E}\!\left[\|Y - \mathbf{m}(X)\|^2\right]
Trace-Free Score Learning
Perturb each clean sample \mathbf{x} \sim p with Gaussian noise of scale \sigma: \tilde{\mathbf{x}} = \mathbf{x} + \sigma\boldsymbol{\epsilon} (\boldsymbol{\epsilon} \sim \mathcal{N}(\mathbf{0}, \mathbf{I})), so p_\sigma(\tilde{\mathbf{x}} \mid \mathbf{x}) = \mathcal{N}(\tilde{\mathbf{x}}; \mathbf{x}, \sigma^2\mathbf{I}). While the marginal score \nabla\log p_\sigma(\tilde{\mathbf{x}}) is unknown, the conditional score is known in closed form for every training pair:
\nabla_{\tilde{\mathbf{x}}}\log p_\sigma(\tilde{\mathbf{x}} \mid \mathbf{x}) = \frac{\mathbf{x} - \tilde{\mathbf{x}}}{\sigma^2} = -\frac{\boldsymbol{\epsilon}}{\sigma}
Optimal Denoising
Rearranging Vincent’s identity \nabla\log p_\sigma(\tilde{\mathbf{x}}) = \mathbb{E}\!\left[\frac{\mathbf{x} - \tilde{\mathbf{x}}}{\sigma^2} \,\middle|\, \tilde{\mathbf{x}}\right] gives Tweedie’s formula for the MMSE clean-point estimator \hat{\mathbf{x}}_0(\tilde{\mathbf{x}}) = \mathbb{E}[\mathbf{x} \mid \tilde{\mathbf{x}}]:
Gaussian Check & Curvature
For Gaussian data x \sim \mathcal{N}(0, v) with v = 1 and noise \sigma = 0.5, the noised marginal is p_\sigma = \mathcal{N}(0, 1.25), Tweedie’s optimal denoiser shrinks by \frac{v}{v+\sigma^2}\tilde{x} = 0.8\,\tilde{x}, and the irreducible MSE floor is \frac{1}{\sigma^2} - \frac{1}{v+\sigma^2} = 4 - 0.8 = 3.2 > 0:
mean squared error of the conditional mean -xt/1.25: 3.215 (floor 3.2)
least-squares slope of x on xt: 0.801 (0.8)
Curvature Bound of Any Noised Density: Differentiating Tweedie’s formula once more with respect to \tilde{\mathbf{x}} proves: \nabla^2\log p_\sigma(\tilde{\mathbf{x}}) = -\frac{\mathbf{I}}{\sigma^2} + \frac{1}{\sigma^4}\operatorname{Cov}[\mathbf{x} \mid \tilde{\mathbf{x}}] \quad \Longrightarrow \quad -\nabla^2\log p_\sigma(\tilde{\mathbf{x}}) \preceq \frac{1}{\sigma^2}\mathbf{I} No peak of a \sigma-blurred density can ever curve more sharply than the Gaussian kernel \mathcal{N}(\mathbf{0}, \sigma^2\mathbf{I}) that blurred it.
1D Mixture Experiment
On 4{,}096 draws from p = \frac{1}{2}\mathcal{N}(-2, 0.5^2) + \frac{1}{2}\mathcal{N}(2, 0.5^2) noised four times each at \sigma = 0.5 (16{,}384 pairs), fitting 17 fixed basis functions by np.linalg.lstsq against y = -\epsilon/\sigma recovers the exact score 4\tanh(4\tilde{x}) - 2\tilde{x}:
mean squared error: fit 2.08, exact score 2.07, population floor 2.11
excess risk 0.009; fit at x = 8: -4.7, exact score -12 (no pairs there)
1D Mixture Experiment
Although the cloud of targets -\epsilon/\sigma scatters vertically with variance \approx 2.11 (the population floor), its conditional mean is the exact score curve, and the least-squares fit achieves an excess risk of only 0.01.
Out-of-Support Variance
Repeating the least-squares fit across 20 independent draws of data and noise reveals a fundamental limitation of single-level score matching:
exact score over 20 draws: mean squared error 2.11, sd 0.037
fit at x = 1 over 20 draws: from +1.8 to +2.1 (exact score +2.0)
fit at x = 8 over 20 draws: from -15.6 to +17.1 (exact score -12.0)
MCMC via the Score
Given a score field \nabla\log p(\mathbf{x}) at a single noise level, Langevin dynamics climbs the log-density while injecting unit Brownian noise to prevent collapse onto a mode:
d\mathbf{X}_t = \frac{1}{2}\nabla\log p(\mathbf{X}_t)\,dt + d\mathbf{W}_t \;\xrightarrow{\text{Euler–Maruyama, step } h}\; \mathbf{x}_{k+1} = \mathbf{x}_k + \frac{h}{2}\mathbf{s}(\mathbf{x}_k) + \sqrt{h}\,\boldsymbol{\xi}_k
The Mode-Mixing Bottleneck
What happens when we run 10{,}000 Langevin chains for 2{,}000 steps (h = 0.01) using the exact score of the lopsided mixture p = 0.8\,\mathcal{N}(-2, 0.5^2) + 0.2\,\mathcal{N}(2, 0.5^2)?
broad start: P(X > 0) = 0.484, E[X^2] = 4.20 (truth 0.2, 4.25)
all chains at -2 (weight 0.8): 0.006 end on the other side
all chains at +2 (weight 0.2): 0.025 end on the other side
Noise-Conditional Score Matching
Training a single time-conditioned score network \mathbf{s}_{\boldsymbol{\theta}}(\mathbf{x}_t, t) across a continuum of Gaussian noising levels \mathbf{x}_t = \alpha_t\mathbf{x}_0 + \sigma_t\boldsymbol{\epsilon} cures all three defects of single-level score matching:
Training Across All Levels
Averaging the DSM loss over noise levels t \sim \mathcal{U}[\varepsilon, T] with weight schedule w(t) > 0:
\mathcal{L}(\boldsymbol{\theta}) = \frac{1}{2}\mathbb{E}_{t}\!\left[w(t)\,\mathbb{E}_{\mathbf{x}_0, \boldsymbol{\epsilon}}\!\left[\left\|\mathbf{s}_{\boldsymbol{\theta}}(\alpha_t\mathbf{x}_0 + \sigma_t\boldsymbol{\epsilon}, t) + \frac{\boldsymbol{\epsilon}}{\sigma_t}\right\|^2\right]\right]
Connecting Discrete & Continuous
The Denoising Diffusion Probabilistic Model (DDPM) runs an N = 1000-step Markov chain with schedule \beta_1, \ldots, \beta_N \in [10^{-4}, 0.02]:
\mathbf{x}_t = \sqrt{1 - \beta_t}\,\mathbf{x}_{t-1} + \sqrt{\beta_t}\,\boldsymbol{\epsilon}_t \quad \Longrightarrow \quad \mathbf{x}_t = \sqrt{\bar{\alpha}_t}\,\mathbf{x}_0 + \sqrt{1 - \bar{\alpha}_t}\,\boldsymbol{\epsilon}, \qquad \bar{\alpha}_t = \prod_{s=1}^t (1 - \beta_s)
alpha_bar at t = 10, 100, 1000: 0.998, 0.897, 4e-05; sigma_bar from 0.01 to 157
Noise Prediction & Sampling
Parameterizing the score model via a noise-prediction network \mathbf{s}_{\boldsymbol{\theta}}(\mathbf{x}_t, t) = -\boldsymbol{\epsilon}_{\boldsymbol{\theta}}(\mathbf{x}_t, t) / \sigma_t (\sigma_t = \sqrt{1 - \bar{\alpha}_t}) unifies DDPM training and sampling with score-based SDEs:
Skipping Levels via the PF-ODE
DDIM predicts \hat{\mathbf{x}}_0 = (\mathbf{x}_t - \sqrt{1-\bar{\alpha}_t}\boldsymbol{\epsilon}_{\boldsymbol{\theta}})/\sqrt{\bar{\alpha}_t} and jumps directly to an earlier level t' < t. In rescaled coordinates \bar{\mathbf{x}}_t = \mathbf{x}_t/\sqrt{\bar{\alpha}_t} and \bar{\sigma}_t = \sqrt{(1-\bar{\alpha}_t)/\bar{\alpha}_t}:
\bar{\mathbf{x}}_{t'} = \bar{\mathbf{x}}_t + (\bar{\sigma}_{t'} - \bar{\sigma}_t)\,\boldsymbol{\epsilon}_{\boldsymbol{\theta}}(\mathbf{x}_t, t)
Analyzing DDIM Truncation Error
Because DDIM is a first-order Euler solver in the \bar{\sigma} clock, its stride error depends on how much \hat{\boldsymbol{\epsilon}}(\mathbf{x}_t, t) = \mathbb{E}[\boldsymbol{\epsilon} \mid \mathbf{x}_t] curves along the probability-flow trajectory:
10 vs. 50 vs. 1,000 Strides
Comparing 10, 50, and 1,000 equally spaced DDIM strides with the exact noise prediction \hat{\epsilon} = -\sqrt{1-\bar{\alpha}_t}\nabla\log p_t on \mathcal{N}(0, 1) data and on the standardized bimodal mixture:
N(0,1) data, 10 strides: every sample times 0.832
N(0,1) data, 50 strides: every sample times 0.964
N(0,1) data, 1000 strides: every sample times 0.998
mixture, 10 strides vs 1000: mean |gap| 0.083, KS 0.077
mixture, 50 strides vs 1000: mean |gap| 0.015, KS 0.016
End-to-End Multi-Level Learning
Fitting our 17-feature linear model by ridge least squares at each of the 50 noise levels visited by a 50-stride DDIM sampler on the lopsided 0.8 / 0.2 mixture:
exact scores: light mode 0.195, means -0.96 +0.94, widths 0.228 0.244, KS to p0 0.03
fitted scores: light mode 0.202, means -0.97 +0.94, widths 0.226 0.274, KS to p0 0.02
truth: light mode 0.2, means -0.97 +0.97, widths 0.243; KS between the clouds 0.02
Multi-Level Regression Solves Mode Weighting: Where single-level Langevin with the exact score left 48\% of samples in the light mode, 50-stride DDIM on least-squares fitted scores recovers the true 20\% light-mode mass (0.197) and matches the exact-score DDIM trajectory to \text{KS} = 0.02.
Conditional Steering
By Bayes’ rule, \nabla\log p_t(y \mid \mathbf{x}) = \mathbf{s}_y - \mathbf{s}_\varnothing. Classifier-Free Guidance (CFG) amplifies this class difference by guidance scale \gamma \ge 1:
\tilde{\mathbf{s}}_{\boldsymbol{\theta}}(\mathbf{x}, t, y) = \mathbf{s}_\varnothing + \gamma\big(\mathbf{s}_y - \mathbf{s}_\varnothing\big) = (1 - \gamma)\mathbf{s}_\varnothing + \gamma\mathbf{s}_y
Guidance Experiment
Running the 1,000-step ancestral reverse chain toward the right mode (y = \mathcal{N}(+0.970, 0.243^2)) across guidance scales \gamma \in \{1, 3, 10\}:
gamma = 1.0: mass right of 0 1.000, mean 0.969, std 0.247
gamma = 3.0: mass right of 0 1.000, mean 1.031, std 0.231
gamma = 10.0: mass right of 0 1.000, mean 1.077, std 0.233
exact conditional: mean 0.970, std 0.243
At \gamma = 1 (exact Bayes’ rule), the chain recovers the exact class mean 0.970 and standard deviation 0.243. At \gamma = 3 and \gamma = 10, linear extrapolation pushes samples away from the opposing left mode, shifting the terminal mean outward to 1.03 and 1.08.
Concept Check
Test your understanding of score matching and diffusion samplers:
Answers:
Key Takeaways
| Method / Theorem | Mathematical Formula | Role & Practical Characteristics |
|---|---|---|
| Hyvärinen’s Identity (ISM) | \mathbb{E}_p\big[\frac{1}{2}\|\mathbf{s}_{\boldsymbol{\theta}}\|^2 + \nabla\cdot\mathbf{s}_{\boldsymbol{\theta}}\big] + C | Removes unknown \nabla\log p, but costs O(d) backward passes for \nabla\cdot\mathbf{s}_{\boldsymbol{\theta}} |
| Vincent’s Theorem (DSM) | \frac{1}{2}\mathbb{E}_{\mathbf{x}_0, \boldsymbol{\epsilon}}\big\|\mathbf{s}_{\boldsymbol{\theta}}(\mathbf{x}_t, t) + \boldsymbol{\epsilon}/\sigma_t\big\|^2 | Trace-free regression onto -\boldsymbol{\epsilon}/\sigma_t; optimal floor is \frac{d}{\sigma_t^2} - \mathbb{E}\|\nabla\log p_t\|^2 |
| Tweedie’s Formula | \mathbb{E}[\mathbf{x}_0 \mid \tilde{\mathbf{x}}] = \tilde{\mathbf{x}} + \sigma^2\nabla\log p_\sigma(\tilde{\mathbf{x}}) | Converts any score or noise predictor into the MMSE clean-point denoiser |
| DDPM Forward & Loss | \mathbf{x}_t = \sqrt{\bar{\alpha}_t}\mathbf{x}_0 + \sqrt{1-\bar{\alpha}_t}\boldsymbol{\epsilon}, w(t) = \sigma_t^2 | Exact discrete VP-SDE transitions; \sigma_t^2 weight cancels the 1/\sigma_t^2 floor |
| DDIM Deterministic Stride | \bar{\mathbf{x}}_{t'} = \bar{\mathbf{x}}_t + (\bar{\sigma}_{t'} - \bar{\sigma}_t)\boldsymbol{\epsilon}_{\boldsymbol{\theta}}(\mathbf{x}_t, t) | Forward Euler on the VP probability-flow ODE in (\bar{\sigma}, \bar{\mathbf{x}}) coordinates |
| Classifier-Free Guidance | \tilde{\mathbf{s}} = (1 - \gamma)\mathbf{s}_\varnothing + \gamma\mathbf{s}_y | Amplifies class direction via Bayes’ rule; \gamma > 1 breaks the 1/\sigma_t^2 curvature bound |