Score matching
The score ∇ log p ignores the normalizing constant, Langevin dynamics samples with it, and denoising score matching learns it — which is what DDPM's noise predictor was doing all along.
The score: a gradient of log-density
The topic DDPM: reverse process and objective trained a network to predict the noise mixed into , then generated images by calling it times. It ended with a claim still to be checked. Without the network cannot know which noise was added, so it learns the average — and that average was said to be, up to a scale and a sign, the gradient of the log-density of noisy images. Why does that make noise prediction work so well? This topic builds the object behind the claim and then checks it.
The score of a density is
It is a vector field. At every point it returns a vector of the same size as , pointing where grows fastest. On a slope it is long and points uphill; at a peak it is zero. A score model is therefore a network from to , the same shape as a denoiser.
The derivative is taken with respect to the data , not the parameters. The score-function estimator in The reparameterization trick differentiated with respect to parameters; here the parameters stay fixed and the point moves.
The simplest case is a 1-D Gaussian. Its log-density (see Multivariate Gaussians) is a parabola in :
Its derivative in is the score of a Gaussian. The checks below ask you to take it.
No normalizing constant
Why work with the gradient of instead of itself? Because flexible models usually know their density only up to a constant. Recall the topic Energy-based models: any network defines
The partition function has no closed form for any interesting , so we can evaluate neither nor the likelihood. Normalizing flows avoid the integral by restricting the architecture to invertible layers with cheap Jacobians (see The change of variables).
The score takes another route, the one that topic already used to sample EBMs. The logarithm splits the constant off, and the gradient removes it:
Nothing is lost about the shape. On a connected region where is positive and smooth, the score fixes up to an additive constant: integrate it along a path and you recover differences of . What it drops is the absolute level — exactly the part we could not compute anyway.
Langevin dynamics
A score is useful only if we can sample with it. The topic Sampling unnormalized densities: MALA and HMC already has the sampler: Langevin dynamics needs nothing but . With a score model in that place, the update is the same. Start anywhere and repeat
with a small step size . The drift moves the point uphill in . The noise kicks it around, so the chain keeps exploring instead of sitting on a peak. As and the number of steps grows, the distribution of approaches (Roberts and Tweedie, 1996). The sampler never evaluates , only its score, so the missing never matters. As that topic showed on , a finite step adds a bias of order , so the step size trades accuracy against the number of steps.
Why plain score matching is hard
To run Langevin dynamics we need learned from data alone. The natural loss is the Fisher divergence, the mean squared distance between the two vector fields:
It contains the unknown true score. Hyvärinen (2005) removed it by integration by parts. In 1-D, expand the square. The term does not depend on . In the cross term, cancels the density, and integration by parts moves the derivative onto the model:
assuming at , so that the boundary term vanishes. In dimensions the same step in every coordinate turns into the trace of the Jacobian:
Only the model is left, averaged over training data. In 1-D this already works. Fit to the four points . Then , and the objective is the average of . Its minimum is at and , the sample mean and variance, found without any normalizing constant.
On images two problems appear. The trace needs the diagonal of the Jacobian , and autograd gets it one row at a time: backward passes per example, for a colour image. Normalizing flows hit the same wall with . And the loss is an average over , so where data are scarce, nothing constrains . Langevin chains started from noise begin exactly in those empty regions.
Denoising score matching
Vincent (2011) found a way around the trace: add noise first. Corrupt each training point with Gaussian noise of scale ,
The noisy points follow a blurred copy of the data, . Its score is unknown. But for one pair the noise is a Gaussian around with a known score, . Denoising score matching (DSM) regresses the network onto this target:
No trace and no normalizer: one forward and one backward pass per example.
A single target is noisy, because many clean points could have produced the same . Squared error averages over them, and the optimum is the conditional mean . That mean is the score of . Differentiate under the integral, divide by , and recognize the posterior from Bayes' theorem:
So the regression lands on , and Vincent (2011) showed that equals the Fisher divergence to up to a constant that does not depend on . Rearranged, the identity is Tweedie's formula: the best denoiser is a score step,
A denoiser and a score model carry the same information. The price: DSM learns the score of , not of . A small keeps close to but makes the targets huge and noisy; a large gives calm targets for a blurred distribution.
Many noise levels
One noise level cannot serve both ends. Large noise fills the empty space between clusters of data, so the score is learned everywhere and chains can find their way from noise to the data. Small noise keeps the detail. Song and Ermon (2019) use both. They take a geometric sequence and one network that receives the level as an input, a noise-conditional score network (NCSN). It is trained with DSM at every level:
Targets grow like , and the weight puts every level on the same scale.
Sampling, annealed Langevin dynamics, walks the levels from the largest to the smallest. It runs Langevin steps with , hands the result to the next level, shrinks the step in proportion to , and repeats. The paper's image setup used levels from to with steps each: network calls per sample.
Annealing also gets the weights of separated modes right. Take and start chains uniformly on . Plain Langevin with the exact score leaves about of the chains in the left mode, roughly the share that started on the left. Near each mode one component dominates, so the score there hardly depends on the weights and . They show only in the gap between the modes, where the density is tiny and chains almost never go. Large noise fills the gap and makes the weights visible. Annealing through ten levels from to leaves about , close to the true .
DDPM as a perturbation kernel
Back to DDPM. Its forward process (see DDPM: the forward process) jumps from clean data to step in one go:
This is a perturbation kernel of the DSM kind, except that the data are shrunk by before the noise is added: . Vincent's argument works for any Gaussian kernel, so the kernel's score is a valid DSM target at every step.
The steps of DDPM play the role of the noise levels of NCSN, and tells the network which level it is at, just as does for . Each coordinate of the kernel is a 1-D Gaussian, so you already know how to take its score.
Noise prediction is a score
Substitute into the kernel score:
The DSM target is the noise itself, times a known factor. So write the score model through a noise predictor,
and the DSM loss at step becomes . With the NCSN weight, the noise variance , it is : the simplified DDPM loss, up to the factor (Ho et al., 2020). Song et al. (2021) make the two views one model in continuous time, with stochastic differential equations.
This confirms the claim from the previous topic. A trained cannot recover the particular noise in ; it estimates its conditional mean, and that mean is a scaled score of the noised data: . Sampling reads the same way. The DDPM reverse mean, rewritten with , is
a step along the score followed by fresh noise, much like annealed Langevin with one step per level. Not exactly: the score gets the weight against fresh noise of variance , while a Langevin step gives it half the noise variance, against . The continuous-time view explains the extra half.
Every sample still takes network calls, one per noise level, and itself is an arbitrary choice. What happens when the steps become infinitely many and infinitely small? The chain turns into a process in continuous time, described by a differential equation. The next topic, Continuous-time diffusion and flow matching, takes that limit and finds samplers that need far fewer calls.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.