Skip to content
BIASED

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 εθ(xt,t)\varepsilon_\theta(x_t, t) to predict the noise mixed into xtx_t, then generated images by calling it TT times. It ended with a claim still to be checked. Without x0x_0 the network cannot know which noise was added, so it learns the average E[ε∣xt]\mathbb E[\varepsilon \mid x_t] — 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 pp is

s(x)=∇xlog⁡p(x).s(x) = \nabla_x \log p(x).

It is a vector field. At every point x∈RDx \in \mathbb R^D it returns a vector of the same size as xx, pointing where log⁡p\log p grows fastest. On a slope it is long and points uphill; at a peak it is zero. A score model sθ(x)≈∇xlog⁡p(x)s_\theta(x) \approx \nabla_x \log p(x) is therefore a network from RD\mathbb R^D to RD\mathbb R^D, the same shape as a denoiser.

The derivative is taken with respect to the data xx, 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 xx:

log⁡N(x; μ,σ2)=−(x−μ)22σ2−12log⁡(2πσ2).\log \mathcal N(x;\, \mu, \sigma^2) = -\frac{(x - \mu)^2}{2\sigma^2} - \tfrac12 \log\big(2\pi\sigma^2\big).

Its derivative in xx is the score of a Gaussian. The checks below ask you to take it.

No normalizing constant

Why work with the gradient of log⁡p\log p instead of pp itself? Because flexible models usually know their density only up to a constant. Recall the topic Energy-based models: any network Eθ:RD→RE_\theta: \mathbb R^D \to \mathbb R defines

pθ(x)=e−Eθ(x)Zθ,Zθ=∫e−Eθ(x) dx.p_\theta(x) = \frac{e^{-E_\theta(x)}}{Z_\theta}, \qquad Z_\theta = \int e^{-E_\theta(x)}\, dx .

The partition function ZθZ_\theta has no closed form for any interesting EθE_\theta, so we can evaluate neither pθ(x)p_\theta(x) 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:

log⁡pθ(x)=−Eθ(x)−log⁡Zθ,∇xlog⁡pθ(x)=−∇xEθ(x).\log p_\theta(x) = -E_\theta(x) - \log Z_\theta, \qquad \nabla_x \log p_\theta(x) = -\nabla_x E_\theta(x).

Nothing is lost about the shape. On a connected region where pp is positive and smooth, the score fixes log⁡p\log p up to an additive constant: integrate it along a path and you recover differences of log⁡p\log p. 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 ∇log⁡p\nabla \log p. With a score model in that place, the update is the same. Start anywhere and repeat

xk+1=xk+η2 s(xk)+η zk,zk∼N(0,I),x_{k+1} = x_k + \frac{\eta}{2}\, s(x_k) + \sqrt{\eta}\, z_k, \qquad z_k \sim \mathcal N(0, I),

with a small step size η>0\eta > 0. The drift η2 s(xk)\tfrac{\eta}{2}\, s(x_k) moves the point uphill in log⁡p\log p. The noise η zk\sqrt\eta\, z_k kicks it around, so the chain keeps exploring instead of sitting on a peak. As η→0\eta \to 0 and the number of steps grows, the distribution of xkx_k approaches pp (Roberts and Tweedie, 1996). The sampler never evaluates pp, only its score, so the missing ZθZ_\theta never matters. As that topic showed on N(0,1)\mathcal N(0, 1), a finite step adds a bias of order η\eta, so the step size trades accuracy against the number of steps.

Why plain score matching is hard

To run Langevin dynamics we need sθ≈∇xlog⁡ps_\theta \approx \nabla_x \log p learned from data alone. The natural loss is the Fisher divergence, the mean squared distance between the two vector fields:

J(θ)=12 Ep∥sθ(x)−∇xlog⁡p(x)∥2.J(\theta) = \tfrac12\, \mathbb E_{p}\big\lVert s_\theta(x) - \nabla_x \log p(x) \big\rVert^2 .

It contains the unknown true score. Hyvärinen (2005) removed it by integration by parts. In 1-D, expand the square. The term 12 Ep(log⁡p)′2\tfrac12\, \mathbb E_p (\log p)'^2 does not depend on θ\theta. In the cross term, (log⁡p)′=p′/p(\log p)' = p'/p cancels the density, and integration by parts moves the derivative onto the model:

Ep[sθ (log⁡p)′]=∫sθ(x) p′(x) dx=−∫sθ′(x) p(x) dx=−Ep[sθ′(x)],\mathbb E_p\big[s_\theta\, (\log p)'\big] = \int s_\theta(x)\, p'(x)\, dx = -\int s_\theta'(x)\, p(x)\, dx = -\mathbb E_p\big[s_\theta'(x)\big],

assuming sθ(x) p(x)→0s_\theta(x)\, p(x) \to 0 at ±∞\pm\infty, so that the boundary term vanishes. In DD dimensions the same step in every coordinate turns sθ′s_\theta' into the trace of the Jacobian:

J(θ)=Ep[tr⁡(∇xsθ(x))+12∥sθ(x)∥2]+const.J(\theta) = \mathbb E_{p}\Big[\operatorname{tr}\big(\nabla_x s_\theta(x)\big) + \tfrac12 \lVert s_\theta(x)\rVert^2\Big] + \text{const}.

Only the model is left, averaged over training data. In 1-D this already works. Fit sθ(x)=−a(x−μ)s_\theta(x) = -a(x - \mu) to the four points {1,2,4,5}\{1, 2, 4, 5\}. Then sθ′=−as_\theta' = -a, and the objective is the average of 12a2(x−μ)2−a\tfrac12 a^2 (x - \mu)^2 - a. Its minimum is at μ=3\mu = 3 and 1/a=2.51/a = 2.5, the sample mean and variance, found without any normalizing constant.

On images two problems appear. The trace needs the diagonal of the D×DD \times D Jacobian ∇xsθ\nabla_x s_\theta, and autograd gets it one row at a time: DD backward passes per example, 30723072 for a 32×3232 \times 32 colour image. Normalizing flows hit the same wall with log⁡∣det⁡Jf∣\log\lvert\det J_f\rvert. And the loss is an average over pp, so where data are scarce, nothing constrains sθs_\theta. 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 σ\sigma,

x~=x+σε,ε∼N(0,I).\tilde x = x + \sigma\varepsilon, \qquad \varepsilon \sim \mathcal N(0, I).

The noisy points follow a blurred copy of the data, qσ(x~)=∫p(x) N(x~; x,σ2I) dxq_\sigma(\tilde x) = \int p(x)\, \mathcal N(\tilde x;\, x, \sigma^2 I)\, dx. Its score is unknown. But for one pair (x,x~)(x, \tilde x) the noise is a Gaussian around xx with a known score, −(x~−x)/σ2=−ε/σ-(\tilde x - x)/\sigma^2 = -\varepsilon/\sigma. Denoising score matching (DSM) regresses the network onto this target:

JDSM(θ)=12 Ex, ε∥sθ(x+σε)+εσ∥2.J_{\text{DSM}}(\theta) = \tfrac12\, \mathbb E_{x,\, \varepsilon}\Big\lVert s_\theta(x + \sigma\varepsilon) + \frac{\varepsilon}{\sigma} \Big\rVert^2 .

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 x~\tilde x. Squared error averages over them, and the optimum is the conditional mean E[−ε/σ∣x~]=E[(x−x~)/σ2∣x~]\mathbb E[-\varepsilon/\sigma \mid \tilde x] = \mathbb E[(x - \tilde x)/\sigma^2 \mid \tilde x]. That mean is the score of qσq_\sigma. Differentiate qσq_\sigma under the integral, divide by qσ(x~)q_\sigma(\tilde x), and recognize the posterior p(x∣x~)=p(x) N(x~; x,σ2I)/qσ(x~)p(x \mid \tilde x) = p(x)\, \mathcal N(\tilde x;\, x, \sigma^2 I) / q_\sigma(\tilde x) from Bayes' theorem:

∇log⁡qσ(x~)=1qσ(x~)∫p(x) N(x~; x,σ2I) x−x~σ2 dx=E[x∣x~]−x~σ2.\begin{aligned} \nabla \log q_\sigma(\tilde x) &= \frac{1}{q_\sigma(\tilde x)} \int p(x)\, \mathcal N(\tilde x;\, x, \sigma^2 I)\, \frac{x - \tilde x}{\sigma^2}\, dx \\ &= \frac{\mathbb E[x \mid \tilde x] - \tilde x}{\sigma^2} . \end{aligned}

So the regression lands on ∇log⁡qσ\nabla \log q_\sigma, and Vincent (2011) showed that JDSMJ_{\text{DSM}} equals the Fisher divergence to qσq_\sigma up to a constant that does not depend on θ\theta. Rearranged, the identity is Tweedie's formula: the best denoiser is a score step,

E[x∣x~]=x~+σ2 ∇log⁡qσ(x~).\mathbb E[x \mid \tilde x] = \tilde x + \sigma^2\, \nabla \log q_\sigma(\tilde x).

A denoiser and a score model carry the same information. The price: DSM learns the score of qσq_\sigma, not of pp. A small σ\sigma keeps qσq_\sigma close to pp but makes the targets −ε/σ-\varepsilon/\sigma huge and noisy; a large σ\sigma 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 σ1>σ2>⋯>σL\sigma_1 > \sigma_2 > \dots > \sigma_L and one network sθ(x,σ)s_\theta(x, \sigma) that receives the level as an input, a noise-conditional score network (NCSN). It is trained with DSM at every level:

L(θ)=∑i=1Lσi2⋅12 Ex, ε∥sθ(x+σiε, σi)+εσi∥2.\mathcal L(\theta) = \sum_{i=1}^{L} \sigma_i^2 \cdot \tfrac12\, \mathbb E_{x,\, \varepsilon}\Big\lVert s_\theta(x + \sigma_i\varepsilon,\ \sigma_i) + \frac{\varepsilon}{\sigma_i} \Big\rVert^2 .

Targets grow like 1/σi1/\sigma_i, and the weight σi2\sigma_i^2 puts every level on the same scale.

Sampling, annealed Langevin dynamics, walks the levels from the largest σ\sigma to the smallest. It runs Langevin steps with sθ(⋅,σ1)s_\theta(\cdot, \sigma_1), hands the result to the next level, shrinks the step in proportion to σi2\sigma_i^2, and repeats. The paper's image setup used L=10L = 10 levels from σ1=1\sigma_1 = 1 to σ10=0.01\sigma_{10} = 0.01 with 100100 steps each: 10001000 network calls per sample.

Annealing also gets the weights of separated modes right. Take p=0.8 N(−4,1)+0.2 N(4,1)p = 0.8\,\mathcal N(-4, 1) + 0.2\,\mathcal N(4, 1) and start chains uniformly on [−8,8][-8, 8]. Plain Langevin with the exact score leaves about 52%52\% 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 0.80.8 and 0.20.2. 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 σ=5\sigma = 5 to σ=0.1\sigma = 0.1 leaves about 79%79\%, close to the true 80%80\%.

DDPM as a perturbation kernel

Back to DDPM. Its forward process (see DDPM: the forward process) jumps from clean data to step tt in one go:

xt=αˉt x0+1−αˉt ε,ε∼N(0,I).x_t = \sqrt{\bar\alpha_t}\, x_0 + \sqrt{1 - \bar\alpha_t}\, \varepsilon, \qquad \varepsilon \sim \mathcal N(0, I).

This is a perturbation kernel of the DSM kind, except that the data are shrunk by αˉt\sqrt{\bar\alpha_t} before the noise is added: q(xt∣x0)=N(αˉt x0, (1−αˉt)I)q(x_t \mid x_0) = \mathcal N\big(\sqrt{\bar\alpha_t}\, x_0,\ (1 - \bar\alpha_t) I\big). Vincent's argument works for any Gaussian kernel, so the kernel's score is a valid DSM target at every step.

The TT steps of DDPM play the role of the LL noise levels of NCSN, and tt tells the network which level it is at, just as σi\sigma_i does for sθ(x,σi)s_\theta(x, \sigma_i). 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 xt−αˉt x0=1−αˉt εx_t - \sqrt{\bar\alpha_t}\, x_0 = \sqrt{1 - \bar\alpha_t}\, \varepsilon into the kernel score:

∇xtlog⁡q(xt∣x0)=−ε1−αˉt.\nabla_{x_t} \log q(x_t \mid x_0) = -\frac{\varepsilon}{\sqrt{1 - \bar\alpha_t}} .

The DSM target is the noise itself, times a known factor. So write the score model through a noise predictor,

sθ(xt,t)=−εθ(xt,t)1−αˉt,s_\theta(x_t, t) = -\frac{\varepsilon_\theta(x_t, t)}{\sqrt{1 - \bar\alpha_t}},

and the DSM loss at step tt becomes ∥ε−εθ(xt,t)∥2/(2(1−αˉt))\lVert \varepsilon - \varepsilon_\theta(x_t, t)\rVert^2 / \big(2(1 - \bar\alpha_t)\big). With the NCSN weight, the noise variance 1−αˉt1 - \bar\alpha_t, it is 12∥ε−εθ(xt,t)∥2\tfrac12 \lVert \varepsilon - \varepsilon_\theta(x_t, t)\rVert^2: the simplified DDPM loss, up to the factor 12\tfrac12 (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 εθ\varepsilon_\theta cannot recover the particular noise in xtx_t; it estimates its conditional mean, and that mean is a scaled score of the noised data: E[ε∣xt]=−1−αˉt ∇log⁡qt(xt)\mathbb E[\varepsilon \mid x_t] = -\sqrt{1 - \bar\alpha_t}\, \nabla \log q_t(x_t). Sampling reads the same way. The DDPM reverse mean, rewritten with sθs_\theta, is

μθ(xt,t)=1αt(xt−βt1−αˉt εθ(xt,t))=1αt(xt+βt sθ(xt,t)),\mu_\theta(x_t, t) = \frac{1}{\sqrt{\alpha_t}}\Big(x_t - \frac{\beta_t}{\sqrt{1 - \bar\alpha_t}}\, \varepsilon_\theta(x_t, t)\Big) = \frac{1}{\sqrt{\alpha_t}}\big(x_t + \beta_t\, s_\theta(x_t, t)\big),

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 βt\beta_t against fresh noise of variance σt2≈βt\sigma_t^2 \approx \beta_t, while a Langevin step gives it half the noise variance, η/2\eta/2 against η\eta. The continuous-time view explains the extra half.

Every sample still takes TT network calls, one per noise level, and TT 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.

Full notes

Answers are saved on the server and tied to this browser until you sign in.