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 , so it vanishes when you take the slope. The arrows are computable even when the height is not.
- σ = 1
- σ = 2
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 be the data density on . The model is an unnormalised density with parameters , and the model's score is
Here is the Gradient with respect to the input, so is a vector with entries. The true score of the data is . Because a normalising constant has no in it, . The model's score never needs .
The explicit objective
The natural loss compares the two arrows, averaged over the data. The squared distance between them is
This is the explicit score matching objective. It is correct but useless as written, because is unknown.
Integration by parts removes the unknown
Expand the square:
The last term does not depend on , so it can be dropped during training. The cross term is where the magic happens. Use to write
Now use Integration by Parts in each coordinate. For coordinate , . The boundary term vanishes because the density falls to zero at infinity. Summing over gives
where is the divergence of the model's score, the trace of its Jacobian matrix. Substituting back:
with the constant independent of . Every term on the right involves only the model and samples from the data. The unknown target has disappeared. Minimising this over gives the same answer as minimising the original objective.
Denoising score matching
Suppose the data is corrupted by Gaussian noise: with . The noisy density has its own score, but it is still unknown. The conditional density is known, though, and its score is simple:
Vincent showed that the loss
has the same minimiser as explicit score matching against the noisy density , up to a constant. The trick is that the target is computable from one clean example and its noisy copy. No divergence term is needed, and no knowledge of is needed. The minimiser is .
The noise is a tool here. Small gives a score close to the data's own, but it is only trustworthy near the data, where the noisy samples live. Large covers the space but blurs the target. Modern methods train one network over many noise levels at once.
Worked examples
Let the data be , so and its score is . The model is with score . Take . The variable has .
Direct form. The difference of the scores is .
- Square it: .
- Take expectations: .
- Halve: .
Integration-by-parts form. Compute the pieces separately.
- .
- The divergence is .
- The constant is .
- Sum: .
Both forms agree. The integration-by-parts form is what a training loop can actually compute, since it never mentions . Now try . The model score becomes , identical to the data score. The first form gives zero directly, and the second gives . The loss is minimised exactly when the model matches the data.
Take a noise level , so . A clean data point is , and its noisy copy is .
- The target score is . It points back toward the clean point.
- Suppose the network outputs .
- The error is .
- The per-example loss is .
- The network thinks the pull back toward the data is weaker than it is. Training pushes its output toward .
Diffusion models usually parameterise the denoiser as a noise predictor , not as a score. The two are linked by .
- At noise level , the network predicts at some noisy point.
- The score is .
- The arrow points in the positive direction, which is back toward the data the noise came from.
- The true noise in this example is , so the true score is . The network is close but a bit too strong.
This is why the denoising loss on and the score matching loss are the same thing, up to a weight that depends on .
Tradeoffs and failure modes
Score matching is attractive because it needs no normalising constant and no sampler during training. It has real limits.
| Situation | Problem | Better choice |
|---|---|---|
| Data lies on a thin, low-dimensional surface | The score is badly defined away from the surface, and there are few samples there | Add noise at several levels so the training data covers the space |
| Only small noise is used | Good estimates near the data, poor estimates in low-density regions, so sampling can get stuck between modes | Annealed training over a schedule of decreasing noise levels |
| Exact explicit score matching on high-dimensional inputs | The divergence term needs a Jacobian trace, which costs many backward passes | Denoising score matching, or a stochastic trace estimator |
| Needing exact likelihoods | A score does not give the normalised density, so likelihoods are not direct | Flow models, or an ODE that tracks the density change |
| Sampling from the learned score | Plain Langevin dynamics is slow and sensitive to step size | Better 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 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 of the log-density. The normalising constant is a single number that does not depend on , so its gradient is zero. Taking and differentiating removes entirely. Only the unnormalised is needed.
QuestionThe data is , so . A model has score . Using the integration-by-parts form, find up to the constant and the full loss.Show answer
The tractable part is . Here , so the first term is , and the divergence is . The constant is . The full loss is . Check directly: . 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, , 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 predicts noise at a noisy point. What score does it imply, and in which direction does it push the sample?Show answer
The implied score is . 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.