Score Matching
A probability density is hard to learn, but its slope is not. Score matching learns the gradient of the log-density directly, which points uphill toward realistic images from anywhere.
Why Does This Exist?
Energy-based models define up to an unknown normalizing constant, and maximum likelihood needs that constant's gradient — an integral over all images nobody can compute. Contrastive workarounds are fiddly and unstable. The field needed a way to learn the shape of the density while ignoring its height.
Score matching learns — the slope, not the surface. Slopes need no normalizing constant (it differentiates away), point toward high-density regions from anywhere, and plug directly into Langevin samplers that walk uphill with noise. Diffusion models are score matching at many noise levels wearing a scheduler. The variational cousin is variational inference; the framework is probabilistic foundations.
The one idea you need first. The score of a density is the gradient of its log: . For a standard Gaussian it is simply : at the score is , a pull of strength back toward the center. Denoising score matching trains a network to predict that pull from noisy samples, using the known noise as the teacher.
Think of It Like This
The smell of bread, no map needed
Lost in an unfamiliar city, you smell fresh bread. You have no map and no address, but the smell strengthens in one direction, so you walk that way, re-sniffing at every corner until you reach the bakery. The smell's gradient guided you; its absolute strength never mattered.
The score is that gradient. It never says how probable an image is in absolute terms (no map), only which local direction gets more realistic (stronger smell). Langevin sampling is the walk: follow the smell, add a little wandering so you visit every bakery in town rather than the nearest one forever.
The analogy stops at the nose: real score fields must be learned from data, and where no data exists the "smell" is a hallucination the sampler will still follow.
How It Actually Works
Training perturbs clean images with Gaussian noise of scale and asks the network, given the noisy image, to predict the noise direction back: the target is . Averaged over noise draws, this equals the true score of the smoothed density — no partition function anywhere. Repeating across noise levels (large for global structure, small for fine detail) yields the noise-conditioned score networks inside diffusion models.
Sampling runs annealed Langevin dynamics: with fresh noise each step. The score climbs, the noise explores, and the schedule cools from chaos to crisp images.
Worked example
One-dimensional standard Gaussian, score . Start a Langevin step at with step size :
- Score pull: , drift .
- Deterministic move: , closer to the center .
- Noise kick: ; with a draw the step lands at .
Repeat: every step leans centerward while the noise keeps the chain exploring instead of collapsing to the mode. That balance — pull plus jitter — is the whole sampler.
Code
The point of this snippet is the sampler in miniature: score pull plus calibrated noise reproduces the worked step.
import mathimport random
def langevin_step(x: float, eps: float, rng: random.Random) -> float: score = -x # score of the standard Gaussian noise = math.sqrt(2.0 * eps) * rng.gauss(0.0, 1.0) return x + eps * score + noise # drift plus exploration
rng = random.Random(7) # fixed seed so the walk is checkableprint(round(langevin_step(1.5, 0.1, rng), 3)) # -> 1.484Watch Out For
No data means invented scores
The score is learned only where noisy training samples land. Far from the data manifold — pure noise starts, adversarial corners — the network extrapolates smoothly and confidently wrong, and the sampler follows those invented arrows into garbage with high self-assurance.
Large initial noise levels exist to cover the gaps: the smoothed densities overlap and every start point has a defined pull. Skipping noise levels to "speed up" sampling strands the chain where the score is fiction.
Step size that outruns the slope
Langevin dynamics with too-large oscillates or diverges instead of sampling: the chain jumps across the mode each step and the stationary distribution drifts from the target. Symptoms look like underfitting (blurry, washed samples) rather than the instability they are.
Tune down until sample statistics stabilize, and keep the noise scale matched to the drift — scaling one without the other silently changes the target distribution.
The Quick Version
- Score matching learns , the uphill direction, dodging the normalizing constant entirely.
- Denoising score matching trains it by predicting known noise added to clean images.
- The worked fixture takes a Langevin step from with drift plus a noise kick.
- Sampling balances score pull against noise jitter across annealed noise levels.
- Scores are fiction far from training data, so keep the noise schedule covering the gaps.
- Oversized Langevin steps masquerade as underfitting while sampling the wrong distribution.