Denoising and the Score
Dive into Deep Learning · §17.1
Denoising and the Score Function
posterior mean · denoising score matching · Tweedie’s formula · Fisher divergence blindness · Langevin dynamics
Why Denoising Is the Gateway to Generative Modeling
Motivation
Removing Gaussian noise \tilde{\mathbf{x}} = \mathbf{x}_0 + \sigma\boldsymbol\epsilon by least squares teaches a network the data’s local geometry:
- Nonlinear manifold projection: At noise level \sigma = 0.5 (\textrm{MSE} = 0.25 per pixel), a 4-layer convnet reduces Fashion-MNIST test error to 0.031—beating the optimal 784 \times 784 linear Wiener filter (0.045) by 30\%.
- Denoising \iff score estimation: By Tweedie’s formula, the optimal least-squares denoiser \mathbb{E}[\mathbf{x}_0 \mid \tilde{\mathbf{x}}] is directly proportional to the gradient of the log-density \nabla_{\tilde{\mathbf{x}}} \log p_\sigma(\tilde{\mathbf{x}}).
- The single-level question: If a denoiser implicitly learns the score vector field \nabla \log p_\sigma, can we generate fresh images by following that field uphill from noise?
noisy: per-pixel squared error 0.2493 (standard error 0.0004)
Wiener: per-pixel squared error 0.0447 (standard error 0.0005)
network, seed 0: per-pixel squared error 0.0312 (standard error 0.0004)
network, seed 1: per-pixel squared error 0.0303 (standard error 0.0004)
The Score Vector Field Points Uphill in Log-Density
Running Benchmark: 2D Gaussian Mixture
To test learned denoisers and samplers against exact ground truth, we use a three-mode Gaussian mixture in \mathbb{R}^2 with means (-2.5, -1.5), (2.5, -1.5), (0, 2.5), weights \pi_k = (0.5, 0.3, 0.2), and width s = 0.5:
- Exact score field: The score \nabla_{\mathbf{x}} \log p(\mathbf{x}) weights each mode’s restoring pull by its posterior responsibility r_k(\mathbf{x}) = \pi_k \mathcal{N}(\mathbf{x}; \boldsymbol\mu_k, s^2\mathbf{I}) / p(\mathbf{x}): \nabla_{\mathbf{x}} \log p(\mathbf{x}) = \sum_{k=1}^3 r_k(\mathbf{x})\,\frac{\boldsymbol\mu_k - \mathbf{x}}{s^2}
- Closed-form Gaussian convolution: Adding isotropic noise \sigma\boldsymbol\epsilon simply inflates each component variance from s^2 to s^2 + \sigma^2.
- Where mode weights live: Near mode k, r_k(\mathbf{x}) \approx 1 regardless of \pi_k; the mixture weights alter the score only in the low-density valleys between modes.
Quantitative Yardsticks on the Three-Mode Mixture
Evaluating 2D Samplers
- Mode weights (
weights): Fraction of points assigned to each mode by nearest mean; a calibrated sampler recovers [0.500, 0.300, 0.200] within binomial error (\pm 0.011).
- Stray fraction (
stray): Points lying farther than 3s = 1.5 from every mode center; for a 2D Gaussian, radial distance squared is exponential with tail e^{-4.5} \approx 1.1\%.
- Within-mode spread (
spread): Mean squared distance of non-stray points to their nearest mean (\approx 2s^2 = 0.50, capped at 2.25).
- Negative log-likelihood (
NLL): True mixture entropy is \approx 2.47 nats/point (a target to match, since collapsing onto mode centers artificially lowers NLL).
Every 2D experiment in this chapter evaluates generated point sets against four ground-truth benchmarks on 2{,}000 draws:
{'weights': [0.5, 0.317, 0.183], 'stray': 0.012, 'spread': 0.488} NLL 2.504
Least-Squares Denoising Learns the Posterior Mean
Optimal Denoising
\mathbb{E}\big[\|D(\tilde{\mathbf{x}}) - \mathbf{x}_0\|^2 \,\big|\, \tilde{\mathbf{x}}\big] = \big\|D(\tilde{\mathbf{x}}) - \mathbb{E}[\mathbf{x}_0 \mid \tilde{\mathbf{x}}]\big\|^2 + \operatorname{tr}\mathrm{Cov}[\mathbf{x}_0 \mid \tilde{\mathbf{x}}]
- Orthogonal decomposition: Conditioning on \tilde{\mathbf{x}} = \mathbf{x}_0 + \sigma\boldsymbol\epsilon, the cross term vanishes by iterated expectation.
- Bayes-optimal denoiser: Pointwise in \tilde{\mathbf{x}}, squared error is uniquely minimized by the posterior mean D^\star(\tilde{\mathbf{x}}) = \mathbb{E}[\mathbf{x}_0 \mid \tilde{\mathbf{x}}].
Denoising Score Matching: Vincent’s Identity
From Conditional to Marginal Scores
J_{\mathrm{DSM}}(\boldsymbol\theta) = \frac{1}{2}\mathbb{E}_{\mathbf{x}_0, \boldsymbol\epsilon}\!\left[\Big\|\mathbf{s}_{\boldsymbol\theta}(\mathbf{x}_0 + \sigma\boldsymbol\epsilon) + \frac{\boldsymbol\epsilon}{\sigma}\Big\|^2\right]
- Known conditional score: For p_\sigma(\tilde{\mathbf{x}} \mid \mathbf{x}_0) = \mathcal{N}(\mathbf{x}_0, \sigma^2\mathbf{I}), the conditional score is \nabla_{\tilde{\mathbf{x}}}\log p_\sigma(\tilde{\mathbf{x}} \mid \mathbf{x}_0) = (\mathbf{x}_0 - \tilde{\mathbf{x}})/\sigma^2 = -\boldsymbol\epsilon/\sigma.
- Vincent’s theorem (2011): Differentiating p_\sigma(\tilde{\mathbf{x}}) = \int p(\mathbf{x}_0)p_\sigma(\tilde{\mathbf{x}} \mid \mathbf{x}_0)\,d\mathbf{x}_0 shows \nabla_{\tilde{\mathbf{x}}}\log p_\sigma(\tilde{\mathbf{x}}) = \mathbb{E}[-\boldsymbol\epsilon/\sigma \mid \tilde{\mathbf{x}}], so regressing onto -\boldsymbol\epsilon/\sigma fits \nabla\log p_\sigma exactly.
Worked Example: Analytical Shrinkage on Gaussians
Tweedie’s Formula in Closed Form
\mathbb{E}[\mathbf{x}_0 \mid \tilde{\mathbf{x}}] = \tilde{\mathbf{x}} - \frac{\sigma^2}{v + \sigma^2}(\tilde{\mathbf{x}} - \boldsymbol\mu) = \boldsymbol\mu + \frac{v}{v + \sigma^2}(\tilde{\mathbf{x}} - \boldsymbol\mu)
- James–Stein / Wiener shrinkage: For \mathbf{x}_0 \sim \mathcal{N}(\boldsymbol\mu, v\mathbf{I}), Tweedie’s step shrinks noisy offsets toward the mode center \boldsymbol\mu by the signal-to-total-variance ratio v/(v + \sigma^2).
- Quantitative test (v = s^2 = 0.25, \sigma = 0.3): Shrinkage factor is 0.25/0.34 = \mathbf{0.735}, reducing 2D point MSE by 27\% from 2\sigma^2 = 0.180 to 0.132.
Verifying Tweedie’s Step with a Trained Score MLP
Empirical Verification
We train a 2-layer MLP (TimeMLP(128, 2)) on J_{\mathrm{DSM}} at \sigma = 0.3 for 2{,}000 Adam steps and evaluate Tweedie’s formula on 20{,}000 fresh test draws:
noisy point: squared error 0.1782
posterior mean: squared error 0.1317
Tweedie, trained score: squared error 0.1328
Hidden amplification of score errors: The excess denoising MSE over the exact posterior mean is \mathbb{E}\|\sigma^2(\hat{\mathbf{s}} - \nabla \log p_\sigma)\|^2 = \sigma^4\,\mathbb{E}\|\hat{\mathbf{s}} - \nabla \log p_\sigma\|^2. Because \sigma^4 = 0.0081, a tiny 0.001 denoising gap masks a 0.06 Fisher score error.
Why Learn a Score Instead of an Energy Density?
Sidestepping the Partition Function
An energy-based model (EBM) defines a density p_{\boldsymbol\theta}(\mathbf{x}) = \exp(-E_{\boldsymbol\theta}(\mathbf{x})) / Z_{\boldsymbol\theta} using a scalar network E_{\boldsymbol\theta}(\mathbf{x}):
- Intractable normalizer Z_{\boldsymbol\theta}: Ensuring total probability mass equals 1 requires integrating over all of \mathbb{R}^d: Z_{\boldsymbol\theta} = \int \exp(-E_{\boldsymbol\theta}(\mathbf{x}))\,d\mathbf{x}
- Costly likelihood gradients: Maximizing \log p_{\boldsymbol\theta}(\mathbf{x}) requires \nabla_{\boldsymbol\theta} \log Z_{\boldsymbol\theta} = -\mathbb{E}_{p_{\boldsymbol\theta}}[\nabla_{\boldsymbol\theta} E_{\boldsymbol\theta}], which demands expensive MCMC sampling inside every gradient step.
- Score invariance: Differentiating \log p_{\boldsymbol\theta}(\mathbf{x}) with respect to \mathbf{x} eliminates \log Z_{\boldsymbol\theta} completely: \nabla_{\mathbf{x}} \log p_{\boldsymbol\theta}(\mathbf{x}) = -\nabla_{\mathbf{x}} E_{\boldsymbol\theta}(\mathbf{x}).
Three Score-Matching Objectives Compared
From Fisher Divergence to Denoising
- Exact Fisher divergence D_{\mathrm{F}}(p, \mathbf{s}_{\boldsymbol\theta}) = \frac{1}{2}\mathbb{E}_p[\|\mathbf{s}_{\boldsymbol\theta} - \nabla\log p\|^2]: Ideal objective, but intractable because the ground-truth data score \nabla\log p(\mathbf{x}) is unknown.
- Explicit Score Matching (Hyvärinen, 2005): Eliminates \nabla\log p via integration by parts, but the Jacobian divergence \nabla \cdot \mathbf{s}_{\boldsymbol\theta} requires O(d) backward passes.
- Denoising Score Matching (Vincent, 2011): Fast O(1) noise regression pass in any dimension d, trading the Jacobian trace for a controllable smoothing bias at scale \sigma.
The Single-Noise-Level Dilemma: Variance vs. Bias
Statistical Trade-Off in σ
\text{Bias}(\sigma) = \frac{1}{2}\mathbb{E}_p\big[\|\nabla \log p_\sigma - \nabla \log p\|^2\big] = \frac{d\,\sigma^4}{2s^2(s^2 + \sigma^2)^2}
- Small \sigma (exploding target variance): The conditional target -\boldsymbol\epsilon/\sigma has variance d/\sigma^2 \to \infty as \sigma \to 0, inflating estimator variance and trapping chains in local modes.
- Large \sigma (smoothing bias): Regression is well conditioned, but \nabla\log p_\sigma underestimates the sharp clean score \nabla\log p (bias climbs from 0.0004 at \sigma = 0.05 to 2.6 at \sigma = 1.0).
Pathological Gradients Near Low-Dimensional Manifolds
Geometric Breakdown at Small σ
\nabla_{\tilde{\mathbf{x}}} \log p_\sigma(\tilde{\mathbf{x}}) = \begin{bmatrix} \partial_{\tilde{x}_1} \log q_\sigma(\tilde{x}_1) \\ -\tilde{x}_2 / \sigma^2 \end{bmatrix}
- Anisotropic Gaussian thickening: For data supported on the 1D line x_2 = 0 inside \mathbb{R}^2, the tangential score \partial_{\tilde{x}_1}\log q_\sigma is O(1) while the normal score is -\tilde{x}_2/\sigma^2.
- Manifold cliff hazard: As \sigma \to 0, a network must fit an infinitely steep normal cliff (slope -1/\sigma^2) while averaging out O(1/\sigma) noise tangential to the manifold.
The U-Curve of Score Estimation Error Across σ
Empirical Bias–Variance Sweep
Training TimeMLP models across seven noise levels \sigma \in [0.05, 1.0] and evaluating their Fisher divergence against the clean score \nabla \log p reveals a classic U-curve:
- Bias-dominated regime (\sigma \ge 0.3): Both training seeds lock tightly onto the analytical smoothing bias curve—the network learns \nabla \log p_\sigma accurately, but p_\sigma \neq p.
- Variance-dominated regime (\sigma \le 0.1): Smoothing bias vanishes, but 1/\sigma target noise causes test error to rebound sharply and vary widely across seeds.
- Single-level sweet spot: Error is minimized near \sigma \approx s/3 \in [0.1, 0.2] (around 0.15–0.25).
trained, seed 1: [0.423, 0.189, 0.215, 0.3, 0.409, 1.077, 2.559]
trained, seed 2: [0.541, 0.285, 0.255, 0.252, 0.363, 1.019, 2.532]
exact smoothed: [0.0004, 0.0059, 0.0271, 0.0755, 0.2781, 0.9924, 2.5538]
Why the Fisher Divergence Is Blind to Mode Weights
Structural Limit of Local Scores
Compare our true mixture \pi = (0.5, 0.3, 0.2) against an equal-weight mixture \pi_{\textrm{eq}} = (1/3, 1/3, 1/3) by 2D numerical quadrature on a 721 \times 721 grid:
sigma = 0.0: Fisher divergence 1.5e-05, KL 0.069 nats
sigma = 0.5: Fisher divergence 1.4e-03, KL 0.069 nats
sigma = 1.0: Fisher divergence 9.5e-03, KL 0.064 nats
- Four orders of magnitude below network error: At \sigma = 0, the KL divergence is 0.069 nats, yet the Fisher divergence is only 1.5 \times 10^{-5} (versus \ge 0.15 for our best trained MLP).
- Why: Within each mode k, \nabla \log p(\mathbf{x}) \approx (\boldsymbol\mu_k - \mathbf{x})/s^2 is independent of \pi_k; the scores differ only in the empty valleys where p(\mathbf{x}) \approx 0.
Langevin Dynamics: Sampling with Only a Score Field
Score-Based MCMC
\mathbf{x}_{k+1} = \mathbf{x}_k + \frac{h}{2}\,\mathbf{s}(\mathbf{x}_k) + \sqrt{h}\,\boldsymbol\xi_k, \qquad \boldsymbol\xi_k \sim \mathcal{N}(\mathbf{0}, \mathbf{I})
- Stationary guarantee vs. step inflation: As h \to 0 with \mathbf{s} = \nabla\log p, uphill drift balances Brownian noise to keep p(\mathbf{x}) invariant (while finite h inflates variance to v/(1 - h/4v)).
- Kramers’ barrier bottleneck: Crossing a \Delta U \approx 10\text{--}12 nat valley between modes requires expected time \tau \propto e^{\Delta U} \approx 10^6 steps.
Langevin Chains on Exact vs. Learned Score Fields
Empirical Mixing Test
Starting 2{,}000 chains from \mathcal{N}(\mathbf{0}, 9\mathbf{I}) (\approx 1/3 per mode), we run 2{,}000 Langevin steps (h = 0.05) across five score fields:
start: {'weights': [0.308, 0.329, 0.363], 'stray': 0.751, 'spread': 1.051}
data: {'weights': [0.5, 0.317, 0.183], 'stray': 0.012, 'spread': 0.488}
p: {'weights': [0.312, 0.335, 0.354], 'stray': 0.0095, 'spread': 0.501}
p_1: {'weights': [0.498, 0.298, 0.204], 'stray': 0.4245, 'spread': 0.986}
p_0.3: {'weights': [0.329, 0.334, 0.338], 'stray': 0.0435, 'spread': 0.627}
net 0.3, seed 1: {'weights': [0.454, 0.262, 0.284], 'stray': 0.0485, 'spread': 0.625}
net 0.3, seed 2: {'weights': [0.458, 0.289, 0.253], 'stray': 0.0525, 'spread': 0.628}
Diagnosing the Single-Level Sampling Breakdown
Mixing vs. Sharpness
- Exact clean score (\nabla\log p): Produces sharp modes (\text{spread} \approx 0.50, \text{strays} = 1\text{--}2\%), but 10\text{--}12 nat valleys trap chains in their initial proportions of about a third each.
- Wide blurred score (\nabla\log p_{1.0}): Fills valleys to recover weights near the true [0.5, 0.3, 0.2], but doubles the within-mode spread to \approx 0.97 (\text{strays} = 41\text{--}42\%).
- Trained MLP scores (\sigma = 0.3): Still blocked by 7\text{--}9 nat barriers; the inter-mode drift presumably comes from approximation errors in the empty valleys.
High-Dimensional Denoising: Convnets vs. Wiener
Scaling to 784-D Garment Images
D_{\mathrm{lin}}(\tilde{\mathbf{x}}) = \boldsymbol\mu + \boldsymbol\Sigma(\boldsymbol\Sigma + \sigma^2\mathbf{I})^{-1}(\tilde{\mathbf{x}} - \boldsymbol\mu)
- Linear subspace vs. curved manifold (\sigma = 0.5 on Fashion-MNIST): The optimal 784 \times 784 Wiener filter shrinks each PCA component by \lambda_i/(\lambda_i + \sigma^2) (\textrm{MSE} = 0.045), whereas our 4-layer convnet projects onto the nonlinear garment manifold (\textrm{MSE} \approx 0.031, lower by more than 20 standard errors).
- Why one-step denoising is not generation: At high noise, \mathbb{E}[\mathbf{x}_0 \mid \tilde{\mathbf{x}}] averages across compatible garments into a blur.
Summary: Denoising, Scores, and Single-Level Limits
Key Takeaways
| Optimal MSE Denoiser |
D^\star(\tilde{\mathbf{x}}) = \mathbb{E}[\mathbf{x}_0 \mid \tilde{\mathbf{x}}] |
Posterior average over clean points |
Replaces adversarial game with direct regression |
| Denoising Score Matching |
\frac{1}{2}\mathbb{E}\big[\|\mathbf{s}_{\boldsymbol\theta}(\mathbf{x}_0 + \sigma\boldsymbol\epsilon) + \boldsymbol\epsilon/\sigma\|^2\big] |
O(1) backprop pass; no Z_{\boldsymbol\theta} or Jacobian trace |
Fits blurred marginal score \nabla \log p_\sigma(\tilde{\mathbf{x}}) |
| Tweedie’s Formula |
\hat{\mathbf{x}}_0 = \tilde{\mathbf{x}} + \sigma^2\hat{\mathbf{s}} = \tilde{\mathbf{x}} - \sigma\hat{\boldsymbol\epsilon} |
Affine map linking \hat{\mathbf{x}}_0, \hat{\mathbf{s}}, and \hat{\boldsymbol\epsilon} |
Unifies denoising, noise, and score predictors |
| Small-\sigma Regime |
Target -\boldsymbol\epsilon/\sigma, normal slope -1/\sigma^2 |
Loss floor \propto d/\sigma^2; Kramers time \propto e^{\Delta U} |
Sharp local modes, but chains trap & miss weights |
| Large-\sigma Regime |
Blurred density p_\sigma = p * \mathcal{N}(\mathbf{0}, \sigma^2\mathbf{I}) |
Score bias \propto \sigma^4; mode variance s^2 + \sigma^2 |
Fast global mode mixing, but heavily blurred samples |
| Next (§17.2 Paths) |
\mathbf{x}_t = \alpha_t\mathbf{x}_0 + \sigma_t\boldsymbol\epsilon (t \in [0, 1]) |
Anneals from pure noise (t=1) to data (t=0) |
Combines coarse-level mixing with fine-level sharpness |