Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Learn which way is uphill, not how high the hill is
Imagine standing somewhere in a landscape of probability. The score is an arrow pointing in the direction in which probability increases fastest, towards the nearest cluster of data. To sample from the data you only need those arrows: follow them, with a little random jitter, and you end up where the data live. The arrows do not care about the total amount of probability, so the impossible normalising constant drops out. Score matching learns the arrows from data samples alone. Denoising score matching does it by blurring each data point and learning to point back at the original. The demo draws the arrows for four clusters: add noise and the field becomes smoother and points somewhere sensible even in the empty middle.
Every diffusion model trains with denoising score matching: predicting the noise that was added to an image is the same as estimating the score of the noisy image distribution. The same idea drives denoisers for satellite imagery and score-based priors for inverse problems such as MRI and radar image reconstruction.
- 1. The score of p is…
- 2. The score does not depend on…
- 3. Denoising score matching learns to…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.Fisher divergence and three estimators
- Score: s(x) = ∇_x log p(x). For an EBM, s = −∇_x E, independent of Z.
- Fisher divergence: J(θ) = ½ E_p |s_θ(x) − ∇_x log p(x)|². Unusable directly, because the data score is unknown.
- Implicit score matching (Hyvärinen, 2005): under mild boundary conditions J(θ) = E_p[½|s_θ(x)|² + tr ∇_x s_θ(x)] + const. Needs only samples, but the trace costs D backward passes in D dimensions.
- Sliced score matching (Song, Garg, Shi and Ermon, 2019): project onto random directions v: E[½(vᵀs_θ)² + vᵀ(∇_x s_θ) v], one Jacobian–vector product per projection.
- Denoising score matching (Vincent, 2011): with x̃ = x + σz, minimise E|s_θ(x̃) + (x̃ − x)/σ²|² = E|s_θ(x̃) + z/σ|². Its minimiser is the score of the noised density p_σ = p ∗ N(0, σ²I), not of p.
- Tweedie’s formula (Robbins, 1956; Efron, 2011): E[x | x̃] = x̃ + σ² ∇log p_σ(x̃). A score model is a denoiser and vice versa, so “predict the noise” (ε-prediction in DDPM) is DSM in disguise.
- The σ trade-off: small σ means a nearly unbiased target but a regression target with variance of order D/σ²; large σ means an easy, smooth target for the wrong density. Noise-conditional models (NCSN, diffusion) learn all σ at once.
- 1. Implicit score matching replaces the unknown data score by…
- 2. The DSM minimiser is…
- 3. Tweedie's formula gives E[x | x̃] as…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Score-matching losses in code
Exercise 1.
- The divergence tr ∇s by central differences.
- Hyvärinen’s implicit score-matching loss.
- The denoising score-matching loss.
- Tweedie’s denoiser from a score.
Practice.
- Prefer DSM (one forward pass, no Jacobian) for neural networks; use sliced SM if you need the clean score.
- Parameterise s_θ(x̃, σ) = −ε_θ(x̃, σ)/σ and weight the loss by σ² so every noise level contributes comparably.
- Use several noise draws per data point at small σ; the target’s variance grows like 1/σ².
- Evaluate score models where you will use them: along sampler trajectories, not only on the data.
- In JAX or PyTorch, compute tr ∇s with Hutchinson probes only for small models; for images it is prohibitively expensive.
import torch
def dsm_loss(score_net, x, sigma):
z = torch.randn_like(x); x_noisy = x + sigma * z
s = score_net(x_noisy, sigma) # model of ∇ log p_σ
return 0.5 * ((sigma * s + z) ** 2).flatten(1).sum(1).mean() # σ²-weighted DSM
def tweedie_denoise(score_net, x_noisy, sigma):
return x_noisy + sigma ** 2 * score_net(x_noisy, sigma)- 1. Why weight the DSM loss by σ²?
- 2. At small σ, DSM needs…
- 3. Sliced score matching replaces the trace with…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Implicit score matching. Expand ½E|s_θ − s_p|² = E[½|s_θ|²] − E[s_θ·s_p] + const. In one dimension E[s_θ s_p] = ∫ s_θ p′ dx = [s_θ p] − ∫ s_θ′ p dx, and the boundary term vanishes when s_θ p → 0 at infinity, so −E[s_θ s_p] = E[s_θ′]. In D dimensions the same step for each coordinate gives E[tr ∇s_θ].
Denoising score matching. ∇log p_σ(x̃) = E[∇_x̃ log q(x̃|x) | x̃] (differentiate p_σ = ∫ q(x̃|x) p(x) dx). Hence E|s_θ(x̃) − ∇log q(x̃|x)|² = E|s_θ(x̃) − ∇log p_σ(x̃)|² + C, where C does not depend on θ: DSM and the Fisher divergence to p_σ have the same minimiser. With q = N(x, σ²I), ∇log q = −(x̃ − x)/σ².
Variance. The DSM target −z/σ has squared size D/σ² while the signal it averages to is at most of order |∇log p_σ|. For σ much smaller than the data’s scale the least-squares fit sees mostly noise unless the number of samples grows like 1/σ².
Low-density regions. All estimators weight errors by p (or p_σ), so where data are rare the score is barely constrained. For Langevin sampling started far from data this matters: the first moves use exactly those poorly learned arrows (Song and Ermon, 2019).
Exercise check. With 2000 samples, implicit SM had test Fisher divergence 1.135 (seed 0) but 46.7 in low-density regions; DSM with σ = 1 matched the smoothed score to 0.013 on data and 0.076 in low-density regions.
- 1. The ISM boundary term vanishes when…
- 2. DSM's minimiser is ∇log p_σ because…
- 3. Score estimates are weakest…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.How well do implicit and denoising score matching recover a 2-D score, and how does the noise level trade bias against variance?
History.
- A. Hyvärinen, “Estimation of non-normalized statistical models by score matching” (JMLR, 2005).
- P. Vincent, “A connection between score matching and denoising autoencoders” (Neural Computation, 2011).
- B. Efron, “Tweedie’s formula and selection bias” (JASA, 2011), after H. Robbins (1956).
- Y. Song, S. Garg, J. Shi and S. Ermon, “Sliced score matching: a scalable approach to density and score estimation” (UAI 2019).
- Y. Song and S. Ermon, “Generative modeling by estimating gradients of the data distribution” (NeurIPS 2019).
Exercise 2.
- Data: four equal Gaussian blobs (std 0.4) at (±1.5, ±1.5); 2000 training and 2000 test points.
- Model: s(x) = Ψ(x) W with Ψ = 49 RBFs (7×7 grid on [−3, 3]², width 0.6) plus x₁, x₂ and 1: 52 features, 104 parameters. Every loss is quadratic in W, so each fit is one linear solve (ridge 10⁻³).
- Estimators: implicit SM (exact divergence of the features); DSM with σ = 0.05, 0.2, 0.5, 1.0, one noise draw per point.
- Evaluation: Fisher divergence E|s − ∇log p|² on test data, on about 2240 low-density points (uniform on [−4, 4]² where the mixture is below 10⁻³ of its peak), and for DSM also against its own target ∇log p_σ.
| measure | seed 0 | seed 1 | seed 2 |
|---|---|---|---|
| mean | ∇log p | ² on test data | 12.561 |
| ISM Fisher divergence | 1.135 | 0.993 | 1.299 |
| DSM σ = 0.05 / 0.2 / 0.5 / 1.0 | 12.483 / 1.694 / 4.854 / 9.972 | 9.618 / 1.780 / 4.634 / 9.620 | 10.371 / 1.716 / 5.022 / 10.398 |
| DSM against ∇log p_σ, σ = 0.2 / 0.5 / 1.0 | 0.852 / 0.153 / 0.013 | 0.820 / 0.182 / 0.013 | 1.013 / 0.210 / 0.017 |
| low-density: ISM / DSM σ = 0.2 | 46.706 / 61.263 | 55.978 / 74.875 | 50.599 / 65.527 |
| low-density: DSM σ = 1.0 against ∇log p_σ | 0.076 | 0.078 | 0.084 |
- ISM works where data are: its error is 8–10% of the score’s mean squared size.
- DSM’s sweet spot: σ = 0.2 came within 0.4–0.8 of ISM; σ = 0.05 was almost pure noise (error 78–99% of |∇log p|², nearly as bad as predicting zero), and σ ≥ 0.5 was dominated by bias.
- Noisy scores are easy: at σ = 1 DSM learned ∇log p_σ almost exactly, even in low-density regions (0.08), whereas the clean score’s low-density error was 47 or more for every estimator (the true score is large there, with mean squared size about 180).
- Implication: learn several noise levels and use the large-σ scores to travel and the small-σ scores to refine. That is NCSN.
- Caveat: a linear-in-features model with an exact closed-form fit; neural networks add optimisation error, but the bias–variance pattern in σ is the same.
Score matching in AI, robotics and aerospace
- Diffusion models: DDPM’s noise-prediction loss is σ-weighted DSM summed over noise levels; every image, video, audio and molecule diffusion model trains this way.
- Inverse problems: a score model learned on clean images is a prior; combined with a measurement likelihood it reconstructs MRI, CT, radar and deblurred satellite images (posterior sampling with Tweedie-based guidance).
- Denoising: Tweedie’s formula turns a score model into an optimal denoiser for sensor imagery; conversely, plug-and-play methods use pretrained denoisers as implicit scores.
- Unnormalised models in science: score matching fits exponential-family and graphical models whose normalising constants are intractable, e.g. in neuroscience and statistical physics.
References
- A. Hyvärinen (2005). “Estimation of non-normalized statistical models by score matching.” Journal of Machine Learning Research 6.
- P. Vincent (2011). “A connection between score matching and denoising autoencoders.” Neural Computation 23(7).
- B. Efron (2011). “Tweedie’s formula and selection bias.” Journal of the American Statistical Association 106.
- Y. Song, S. Garg, J. Shi and S. Ermon (2019). “Sliced score matching: a scalable approach to density and score estimation.” UAI.
- Y. Song and S. Ermon (2019). “Generative modeling by estimating gradients of the data distribution.” NeurIPS.
- J. Ho, A. Jain and P. Abbeel (2020). “Denoising diffusion probabilistic models.” NeurIPS.