Search

Search papers and lessons

Score Matching

Level 4 · 11 min read

Score matching fits a model to the gradient of a data density's log, which skips the normalising constant, using an integration-by-parts identity to avoid the unknown target.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Discrete math
  • Linear algebra
  • Generative models
  • Inference
  • Sign in to see your progress

TL;DR

Score matching trains a model to predict the score of a data distribution: the gradient of its log-density, which is an arrow at each point pointing toward more likely values. It works without ever computing the normalising constant of the density, and without knowing the true score. An integration-by-parts identity turns the unknown target into a quantity you can compute from the model alone. Denoising score matching goes further: it adds known noise to the data and matches the score of that noise instead, so training needs only pairs of clean and noisy examples.

Where you'll see it: in score-based generative models and in the denoisers inside diffusion models, where a network learns which direction to push a noisy image to make it look more like real data.

Intuition

Learning the arrows instead of the hill

Picture the probability density of images as a landscape, with likely images on high ground. The score is the slope at every point: an arrow saying which way is uphill. If you know the arrow everywhere, you can climb to the peak from any starting point, which is exactly what sampling needs.

Why learn the arrows and not the landscape itself? Because the landscape must be normalised: its total volume has to equal 1. For most useful models that normalising constant is impossible to compute. But the constant does not depend on the point xx, so it vanishes when you take the slope. The arrows are computable even when the height is not.

−4−3−2−101234−4−2024xscore
  • σ = 1
  • σ = 2
The score of a zero-mean Gaussian points back toward the centre. The wider Gaussian (σ = 2) has a gentler slope, so its arrows are shorter.

Why the target is the problem

To learn the arrows, you need to compare your model's arrows with the true ones. But the true arrows are exactly what you do not know, because you only have samples from the data. Score matching is the observation that you can compare the two without ever seeing the true arrows. The next sections show how.

The math

Setup

Let p(x)p(x) be the data density on Rd\mathbb{R}^d. The model is an unnormalised density qθ(x)q_\theta(x) with parameters θ\theta, and the model's score is

sθ(x)=∇xlog⁡qθ(x).s_\theta(x) = \nabla_x \log q_\theta(x).

Here ∇x\nabla_x is the Gradient with respect to the input, so sθ(x)s_\theta(x) is a vector with dd entries. The true score of the data is sp(x)=∇xlog⁡p(x)s_p(x) = \nabla_x \log p(x). Because a normalising constant ZθZ_\theta has no xx in it, ∇xlog⁡(qθ(x)/Zθ)=∇xlog⁡qθ(x)\nabla_x \log (q_\theta(x)/Z_\theta) = \nabla_x \log q_\theta(x). The model's score never needs ZθZ_\theta.

The explicit objective

The natural loss compares the two arrows, averaged over the data. The squared distance between them is

JESM(θ)=12 Ep[ ∥sθ(X)−sp(X)∥2 ].J_{\text{ESM}}(\theta) = \tfrac{1}{2}\,\mathbb{E}_{p}\left[\,\lVert s_\theta(X) - s_p(X) \rVert^2\,\right].

This is the explicit score matching objective. It is correct but useless as written, because sps_p is unknown.

Integration by parts removes the unknown

Expand the square:

12∥sθ−sp∥2=12∥sθ∥2−sθ⋅sp+12∥sp∥2.\tfrac{1}{2}\lVert s_\theta - s_p \rVert^2 = \tfrac{1}{2}\lVert s_\theta \rVert^2 - s_\theta \cdot s_p + \tfrac{1}{2}\lVert s_p \rVert^2.

The last term does not depend on θ\theta, so it can be dropped during training. The cross term is where the magic happens. Use sp=∇xp/ps_p = \nabla_x p / p to write

Ep[sθ⋅sp]=∫p(x) sθ(x)⋅∇xp(x)p(x) dx=∫sθ(x)⋅∇xp(x) dx.\mathbb{E}_p\left[s_\theta \cdot s_p\right] = \int p(x)\, s_\theta(x) \cdot \frac{\nabla_x p(x)}{p(x)}\, dx = \int s_\theta(x) \cdot \nabla_x p(x)\, dx.

Now use Integration by Parts in each coordinate. For coordinate ii, ∫sθ,i ∂ip dx=[sθ,i p]−∫(∂isθ,i) p dx\int s_{\theta,i}\, \partial_i p \, dx = [s_{\theta,i}\, p] - \int (\partial_i s_{\theta,i})\, p\, dx. The boundary term vanishes because the density falls to zero at infinity. Summing over ii gives

Ep[sθ⋅sp]=− Ep[∇x⋅sθ(X)],\mathbb{E}_p\left[s_\theta \cdot s_p\right] = -\,\mathbb{E}_p\left[\nabla_x \cdot s_\theta(X)\right],

where ∇x⋅sθ=∑i∂isθ,i\nabla_x \cdot s_\theta = \sum_i \partial_i s_{\theta,i} is the divergence of the model's score, the trace of its Jacobian matrix. Substituting back:

JESM(θ)=Ep[12∥sθ(X)∥2+∇x⋅sθ(X)]+C,J_{\text{ESM}}(\theta) = \mathbb{E}_p\left[\tfrac{1}{2}\lVert s_\theta(X) \rVert^2 + \nabla_x \cdot s_\theta(X)\right] + C,

with the constant C=12Ep[∥sp(X)∥2]C = \tfrac{1}{2}\mathbb{E}_p[\lVert s_p(X) \rVert^2] independent of θ\theta. Every term on the right involves only the model and samples from the data. The unknown target has disappeared. Minimising this over θ\theta gives the same answer as minimising the original objective.

Denoising score matching

Suppose the data is corrupted by Gaussian noise: x~=x+σε\tilde{x} = x + \sigma \varepsilon with ε∼N(0,I)\varepsilon \sim \mathcal{N}(0, I). The noisy density pσ(x~)=∫p(x) N(x~; x,σ2I) dxp_\sigma(\tilde{x}) = \int p(x)\, \mathcal{N}(\tilde{x};\, x, \sigma^2 I)\, dx has its own score, but it is still unknown. The conditional density N(x~; x,σ2I)\mathcal{N}(\tilde{x};\, x, \sigma^2 I) is known, though, and its score is simple:

∇x~log⁡N(x~; x,σ2I)=−x~−xσ2=−εσ.\nabla_{\tilde{x}} \log \mathcal{N}(\tilde{x};\, x, \sigma^2 I) = -\frac{\tilde{x} - x}{\sigma^2} = -\frac{\varepsilon}{\sigma}.

Vincent showed that the loss

JDSM(θ)=12 Ex,x~[ ∥sθ(x~)+x~−xσ2∥2 ]J_{\text{DSM}}(\theta) = \tfrac{1}{2}\,\mathbb{E}_{x,\tilde{x}}\left[\,\left\lVert s_\theta(\tilde{x}) + \frac{\tilde{x} - x}{\sigma^2} \right\rVert^2\,\right]

has the same minimiser as explicit score matching against the noisy density pσp_\sigma, up to a constant. The trick is that the target −(x~−x)/σ2-(\tilde{x} - x)/\sigma^2 is computable from one clean example and its noisy copy. No divergence term is needed, and no knowledge of pp is needed. The minimiser is sθ(x~)=∇x~log⁡pσ(x~)s_\theta(\tilde{x}) = \nabla_{\tilde{x}} \log p_\sigma(\tilde{x}).

The noise is a tool here. Small σ\sigma gives a score close to the data's own, but it is only trustworthy near the data, where the noisy samples live. Large σ\sigma covers the space but blurs the target. Modern methods train one network over many noise levels at once.

Worked examples

Example
The Gaussian, computed both ways

Let the data be p=N(0,4)p = \mathcal{N}(0, 4), so σ2=4\sigma^2 = 4 and its score is sp(x)=−x/4s_p(x) = -x/4. The model is qθ=N(0,θ2)q_\theta = \mathcal{N}(0, \theta^2) with score sθ(x)=−x/θ2s_\theta(x) = -x/\theta^2. Take θ=1\theta = 1. The variable XX has E[X2]=4\mathbb{E}[X^2] = 4.

Direct form. The difference of the scores is sθ−sp=−x+x/4=−0.75 xs_\theta - s_p = -x + x/4 = -0.75\,x.

  1. Square it: 0.5625 x20.5625\,x^2.
  2. Take expectations: E[0.5625 X2]=0.5625×4=2.25\mathbb{E}[0.5625\,X^2] = 0.5625 \times 4 = 2.25.
  3. Halve: J=12×2.25=1.125J = \tfrac{1}{2} \times 2.25 = 1.125.

Integration-by-parts form. Compute the pieces separately.

  1. 12E[∥sθ∥2]=12E[X2]/θ4=12×4=2\tfrac{1}{2}\mathbb{E}[\lVert s_\theta \rVert^2] = \tfrac{1}{2}\mathbb{E}[X^2]/\theta^4 = \tfrac{1}{2} \times 4 = 2.
  2. The divergence is ∇⋅sθ=−1/θ2=−1\nabla \cdot s_\theta = -1/\theta^2 = -1.
  3. The constant is C=12E[X2/16]=12×0.25=0.125C = \tfrac{1}{2}\mathbb{E}[X^2/16] = \tfrac{1}{2} \times 0.25 = 0.125.
  4. Sum: 2+(−1)+0.125=1.1252 + (-1) + 0.125 = 1.125.

Both forms agree. The integration-by-parts form is what a training loop can actually compute, since it never mentions sps_p. Now try θ=2\theta = 2. The model score becomes −x/4-x/4, identical to the data score. The first form gives zero directly, and the second gives 12×4/16−1/4+0.125=0.125−0.25+0.125=0\tfrac{1}{2} \times 4/16 - 1/4 + 0.125 = 0.125 - 0.25 + 0.125 = 0. The loss is minimised exactly when the model matches the data.

Example
One noisy point

Take a noise level σ=0.5\sigma = 0.5, so σ2=0.25\sigma^2 = 0.25. A clean data point is x=2x = 2, and its noisy copy is x~=2.5\tilde{x} = 2.5.

  1. The target score is −(x~−x)/σ2=−(0.5)/0.25=−2-(\tilde{x} - x)/\sigma^2 = -(0.5)/0.25 = -2. It points back toward the clean point.
  2. Suppose the network outputs sθ(x~)=−1.5s_\theta(\tilde{x}) = -1.5.
  3. The error is −1.5−(−2)=0.5-1.5 - (-2) = 0.5.
  4. The per-example loss is 12×0.52=0.125\tfrac{1}{2} \times 0.5^2 = 0.125.
  5. The network thinks the pull back toward the data is weaker than it is. Training pushes its output toward −2-2.
Example
Predicting noise instead of the score

Diffusion models usually parameterise the denoiser as a noise predictor εθ(x~)\varepsilon_\theta(\tilde{x}), not as a score. The two are linked by sθ(x~)=−εθ(x~)/σs_\theta(\tilde{x}) = -\varepsilon_\theta(\tilde{x})/\sigma.

  1. At noise level σ=0.1\sigma = 0.1, the network predicts εθ=−0.3\varepsilon_\theta = -0.3 at some noisy point.
  2. The score is sθ=−(−0.3)/0.1=3s_\theta = -(-0.3)/0.1 = 3.
  3. The arrow points in the positive direction, which is back toward the data the noise came from.
  4. The true noise in this example is ε=−0.25\varepsilon = -0.25, so the true score is −(−0.25)/0.1=2.5-(-0.25)/0.1 = 2.5. The network is close but a bit too strong.

This is why the denoising loss on ε\varepsilon and the score matching loss are the same thing, up to a weight that depends on σ\sigma.

Tradeoffs and failure modes

Score matching is attractive because it needs no normalising constant and no sampler during training. It has real limits.

SituationProblemBetter choice
Data lies on a thin, low-dimensional surfaceThe score is badly defined away from the surface, and there are few samples thereAdd noise at several levels so the training data covers the space
Only small noise is usedGood estimates near the data, poor estimates in low-density regions, so sampling can get stuck between modesAnnealed training over a schedule of decreasing noise levels
Exact explicit score matching on high-dimensional inputsThe divergence term needs a Jacobian trace, which costs many backward passesDenoising score matching, or a stochastic trace estimator
Needing exact likelihoodsA score does not give the normalised density, so likelihoods are not directFlow models, or an ODE that tracks the density change
Sampling from the learned scorePlain Langevin dynamics is slow and sensitive to step sizeBetter samplers, such as predictor-corrector methods or probability-flow ODEs

Some practical warnings. The denoising target is noisy by construction, so the loss is high-variance and needs many examples. The learned score is only as good as its training distribution; if you sample far from the training noise levels, the arrows can be arbitrary. And the choice of σ\sigma schedule matters as much as the network architecture for final sample quality.

History

Aapo Hyvärinen introduced score matching in 2005 in the Journal of Machine Learning Research. His problem was estimating models whose normalising constant could not be computed, and the integration-by-parts identity was his key move. Pascal Vincent showed in 2011 that the denoising version of the loss, where you add Gaussian noise to the data and match the noise, is equivalent to training a denoising autoencoder. This connection made the method far more practical, because the target no longer needed a divergence.

Yang Song and Stefano Ermon in 2019 used noise-conditional score networks with annealed Langevin dynamics to generate images. They trained one score network across many noise levels and sampled by descending through them. This was the first strong image generator built on scores.

Jonathan Ho and colleagues in 2020 published denoising diffusion probabilistic models, which turned a related noise-prediction loss into a state-of-the-art image generator. Song and colleagues later in 2020 unified the two lines in a continuous-time stochastic differential equation framework. The score is now the central object in most of that work, and the diffusion models in common use are, in essence, score matching at many noise levels.

Check yourself

QuestionWhy can you compute the score of an unnormalised model even though you cannot compute its normalising constant?Show answer

The score is the gradient with respect to xx of the log-density. The normalising constant ZθZ_\theta is a single number that does not depend on xx, so its gradient is zero. Taking log⁡(qθ/Zθ)=log⁡qθ−log⁡Zθ\log (q_\theta / Z_\theta) = \log q_\theta - \log Z_\theta and differentiating removes log⁡Zθ\log Z_\theta entirely. Only the unnormalised qθq_\theta is needed.

QuestionThe data is p=N(0,1)p = \mathcal{N}(0, 1), so sp(x)=−xs_p(x) = -x. A model has score sθ(x)=−2xs_\theta(x) = -2x. Using the integration-by-parts form, find JJ up to the constant and the full loss.Show answer

The tractable part is 12E[∥sθ∥2]+E[∇⋅sθ]=12E[4X2]+E[−2]=2−2=0\tfrac{1}{2}\mathbb{E}[\lVert s_\theta\rVert^2] + \mathbb{E}[\nabla \cdot s_\theta] = \tfrac{1}{2}\mathbb{E}[4X^2] + \mathbb{E}[-2] = 2 - 2 = 0. Here E[X2]=1\mathbb{E}[X^2] = 1, so the first term is 12×4=2\tfrac{1}{2} \times 4 = 2, and the divergence is −2-2. The constant is C=12E[X2]=0.5C = \tfrac{1}{2}\mathbb{E}[X^2] = 0.5. The full loss is 0+0.5=0.50 + 0.5 = 0.5. Check directly: 12E[(−2x+x)2]=12E[X2]=0.5\tfrac{1}{2}\mathbb{E}[(-2x + x)^2] = \tfrac{1}{2}\mathbb{E}[X^2] = 0.5. The model is not correct, and the loss shows it.

QuestionWhy does denoising score matching avoid both the unknown data score and the divergence term?Show answer

The noisy sample is generated from a known Gaussian kernel around the clean point. Its score, −(x~−x)/σ2-(\tilde{x} - x)/\sigma^2, can be computed directly from the pair. Vincent showed that matching this known target gives the same minimiser as matching the unknown noisy density. Since the target is an explicit function of the data pair, no integration by parts is needed, so no trace of a Jacobian appears.

QuestionA diffusion denoiser at noise level σ=0.2\sigma = 0.2 predicts noise εθ=0.6\varepsilon_\theta = 0.6 at a noisy point. What score does it imply, and in which direction does it push the sample?Show answer

The implied score is sθ=−εθ/σ=−0.6/0.2=−3s_\theta = -\varepsilon_\theta/\sigma = -0.6/0.2 = -3. The score is negative, so the sample is pushed toward smaller values, back toward the clean data the noise was added to. The magnitude is large because the noise level is small, so the arrow is steep.

Sources