Foundations to Frontier
Tests
H. Generative Models › H6 Energy-Based and Score-Based Models

Score matching and denoising score matching

The score of a density is the gradient of its log, ∇_x log p(x). It does not depend on the normalising constant, so a model of the score sidesteps the partition function that makes energy-based models hard to train. Score matching (Hyvärinen, 2005) fits a model score s_θ to the data score without knowing the data score: integration by parts turns the Fisher divergence into E[½|s_θ|² + tr ∇s_θ], which needs only data samples. Denoising score matching (Vincent, 2011) avoids the costly trace: add Gaussian noise of scale σ and regress s_θ(x̃) onto −(x̃ − x)/σ², which learns the score of the noised density. This page covers the Fisher divergence, implicit, sliced and denoising score matching, Tweedie's formula and the bias–variance trade-off in σ that leads to noise-conditional models and diffusion. In the exercises you check the estimators on Gaussians, then fit a 52-feature linear score model to four 2-D blobs in closed form. Implicit score matching reached a Fisher divergence of 1.135 on test data, where the true score's mean squared size is 12.56. Denoising score matching was best at σ = 0.2 (1.694), mostly noise at σ = 0.05 (12.483) and biased towards the smoothed score at σ = 1 (9.972), although it learned that smoothed score almost perfectly (0.013). In low-density regions every estimator was poor: the implicit score-matching error rose to 46.7.

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.

Score lab
Arrows point along ∇log p_σ (length grows with its size). With σ = 0.3 the mean score size on the axes between the blobs is 4.35. More noise smooths the field and fills the empty middle with useful directions.
Why engineers care

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.

📝 Quick check (Level 0)
0/3 answered
  1. 1. The score of p is…
  2. 2. The score does not depend on…
  3. 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.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Implicit score matching replaces the unknown data score by…
  2. 2. The DSM minimiser is…
  3. 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)
🐍 Exercise 1: implicit and denoising score matching
Write a finite-difference divergence, the ISM and DSM losses and Tweedie's denoiser, and check their minimisers on Gaussians.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Why weight the DSM loss by σ²?
  2. 2. At small σ, DSM needs…
  3. 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.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The ISM boundary term vanishes when…
  2. 2. DSM's minimiser is ∇log p_σ because…
  3. 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.
🐍 Exercise 2 (research): how well do implicit and denoising score matching recover a 2-D score, and how does the noise level trade bias against variance?
Fit a feature-based score model to four blobs with implicit score matching and with denoising score matching at four noise levels, and measure Fisher divergences on and off the data.

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

  1. A. Hyvärinen (2005). “Estimation of non-normalized statistical models by score matching.” Journal of Machine Learning Research 6.
  2. P. Vincent (2011). “A connection between score matching and denoising autoencoders.” Neural Computation 23(7).
  3. B. Efron (2011). “Tweedie’s formula and selection bias.” Journal of the American Statistical Association 106.
  4. Y. Song, S. Garg, J. Shi and S. Ermon (2019). “Sliced score matching: a scalable approach to density and score estimation.” UAI.
  5. Y. Song and S. Ermon (2019). “Generative modeling by estimating gradients of the data distribution.” NeurIPS.
  6. J. Ho, A. Jain and P. Abbeel (2020). “Denoising diffusion probabilistic models.” NeurIPS.