Learn the gradient of the log-density instead of the density: score matching and its denoising form, Tweedie's formula, sampling with Langevin dynamics, and the noise levels that turn it into the same model as DDPM.
Picture a density as a landscape and stand at any point. An arrow tells you which way is uphill and how steep the slope is. For N(μ,σ2I) the arrow at x is −(x−μ)/σ2: it points back to the mean, longer the farther away you are. Knowing the arrow everywhere, you could walk uphill with a little random jitter and end up spread over the landscape in proportion to its height, that is, sampling from it.
The arrow is the score, s(x)=∇xlogp(x), for a density p on Rd. The DDPM note reached a noise-predicting network by bounding a likelihood. The same network can be reached by learning the score of noised data directly, and sampling by following it.
Many models define a density only up to a constant, like the energy-based models: pθ(x)=e−Eθ(x)/Zθ, with a normalizer Zθ=∫e−Eθ(x)dx that cannot be computed in high dimension. Maximum likelihood needs logZθ. The score does not:
∇xlogpθ(x)=−∇xEθ(x)−=0∇xlogZθ,
because Zθ depends on θ but not on x. The score still carries all the information about p where p is positive and smooth on a connected region, such as all of Rd: two such densities with the same score everywhere have log-densities that differ by a constant, so the densities differ by a constant factor, which normalization fixes.
When the score is parameterized through an energy, sθ=−∇xEθ, fitting it trains an energy-based model with neither its normalizer nor samples from it (energy-based models). Diffusion models instead let a network sθ:Rd→Rd output the vector field directly. Its Jacobian need not be symmetric, so it need not be the gradient of any scalar function: an approximate score with no energy behind it.
The natural loss is the Fisher divergence, the mean squared distance between the model’s score and the data’s:
J(θ)=21Epsθ(x)−∇xlogp(x)2.
It needs the true score, which is unknown. Hyvärinen (2005) removes it by integration by parts. In one dimension, expand the square. The term 21Ep(logp)′2 does not depend on θ. The cross term becomes
by the chain rule (logp)′=p′/p, integration by parts, and the assumption that sθ(x)p(x)→0 at ±∞. In d dimensions the same step is taken in each coordinate xi for the component sθ,i, and the derivatives ∂sθ,i/∂xi add up to the trace of the Jacobian, ∇⋅sθ:
J(θ)=Ep[∇⋅sθ(x)+21∥sθ(x)∥2]+const.
The expectation is over data and involves only the model. Hyvärinen’s Theorem 1 states it with its conditions: p differentiable, Ep∥sθ∥2 and Ep∥∇logp∥2 finite, and p(x)sθ(x)→0 as ∥x∥→∞.
Worked example. Fit a Gaussian score sθ(x)=−a(x−μ), a=1/σ2, to the data {1,2,4,5}. Then sθ′=−a, and the sample objective is the average of 21a2(x−μ)2−a. Its derivative in μ vanishes at μ=xˉ=3. Its derivative in a vanishes when a⋅(x−μ)2=1, so σ2=41(4+1+1+4)=2.5. Score matching recovers the sample mean and variance, the maximum likelihood estimates, without touching the normalizing constant; for the Gaussian the two estimators coincide on every sample (Hyvärinen, 2005, §3.1).
The cost is the trace. For a network sθ:Rd→Rd, ∇⋅sθ needs d backward passes, one per diagonal entry of the Jacobian: fine for d=2, impossible for an image with d=3⋅64⋅64.
Sliced score matching (Song et al., 2019) projects onto random directions v with E[vv⊤]=I:
JSSM(θ)=EpEv[v⊤∇sθ(x)v+21(v⊤sθ(x))2].
Averaged over v this is Hyvärinen’s objective, because E[v⊤Av]=tr(AE[vv⊤])=tr(A) and E[(v⊤s)2]=s⊤E[vv⊤]s=∥s∥2. One Jacobian–vector product replaces d backward passes (Song and Ermon, 2019, §2.1).
Denoising score matching (Vincent, 2011) removes the derivative of the model from the loss altogether, at a price: it fits the score of noised data. Draw x∼p and perturb it with a kernel q(x~∣x) whose score we know, so that the noised data have the density q(x~)=∫q(x~∣x)p(x)dx. The Fisher divergence to the noised data, the explicit objective, and the denoising objective are
The steps use, in order: q∇logq=∇q; the definition of q(x~); differentiation under the integral sign, which needs ∇x~q(x~∣x) to be integrable against p(x); the same identity q∇logq=∇q for the kernel; and the definition of the expectation over the joint. Subtracting the expanded objectives leaves JESM−JDSM=C1−C2. The kernel’s form never entered, only that logq(x~∣x) is differentiable in x~.
The usual kernel is Gaussian, x~=x+σε with ε∼N(0,I); write qσ for the resulting density of the noised data. The kernel’s score is
∇x~logN(x~;x,σ2I)=−σ2x~−x=−σε,
and the objective is a regression of the network’s output at the noised point onto −ε/σ:
There is a second way to see why the regression works. A squared-error regression learns the conditional mean of its target, here E[−ε/σ∣x~]. That conditional mean is the score of qσ. Differentiate qσ(x~)=∫p(x)N(x~;x,σ2I)dx under the integral, divide by qσ(x~), and recognize Bayes’ theorem, p(x∣x~)=p(x)N(x~;x,σ2I)/qσ(x~):
Rearranged, this is Tweedie’s formula (Efron, 2011, Eq. 1.4):
E[x∣x~]=x~+σ2∇logqσ(x~)
The best guess of the clean point is the noisy point moved along the score of the noised data. With ε=(x~−x)/σ, it also gives E[ε∣x~]=−σ∇logqσ(x~), so the optimal regression target E[−ε/σ∣x~] is the score itself. Denoising and score estimation are the same task.
Worked example. Data x∼N(0,1) and σ=0.5. Then x~∼N(0,1.25), whose score is −x~/1.25=−0.8x~. Tweedie gives E[x∣x~]=x~−0.25⋅0.8x~=0.8x~, the familiar shrinkage of a Gaussian posterior mean. Directly: ε and x~ are jointly Gaussian with Cov(ε,x~)=0.5, so E[ε∣x~]=0.5x~/1.25=0.4x~, and E[−ε/σ∣x~]=−0.8x~, the score.
Denoising score matching estimates the score of qσ, not of p. A small σ keeps the two close, but the target −ε/σ then scatters widely around its mean, the score. Per coordinate, the law of total variance splits Var(εi)=1 into the average scatter and the variance of the mean, and Tweedie gives that mean, E[εi∣x~]=−σ∂ilogqσ(x~):
In the worked example above, 1/0.25−0.8=3.2 against the bound 4. The subtracted term stays bounded as σ→0. For differentiable p that vanishes at infinity, moving the derivative from the kernel onto p by integration by parts gives ∂ilogqσ(x~)=E[∂ilogp(x)∣x~], and Jensen’s inequality bounds its mean square by Ep[(∂ilogp)2], finite for smooth data. So the scatter grows like 1/σ2 (Murphy, 2023, §24.3.2). It does not depend on θ and does not move the minimizer, but it is noise in every gradient estimate. A large σ gives an easy regression of a blurred density. The way out, below, is many values of σ at once.
Given a score, Langevin dynamics produces samples:
xk+1zk=xk+2ηs(xk)+ηzk,∼N(0,I).
The update is one Euler step of the Langevin diffusion, whose stationary density is p; the MCMC samplers note derives both. The drift climbs the density and the noise keeps the chain from collapsing onto a mode: without the noise term the update is gradient ascent on logp and ends at a mode, not at a sample. Under regularity conditions the distribution of xk becomes exactly p as η→0 and the number of steps grows without bound (Song and Ermon, 2019, §2.2).
With a finite step the chain is biased. For p=N(0,1), s(x)=−x, and a stationary variance v must reproduce itself under one step, which gives (derived here)
v=1−η/41.
With η=0.1 the chain settles at variance 1.026 instead of 1; with η=0.5, at 1.143. A Metropolis–Hastings correction would remove the bias, at the cost of evaluating the density, which is what the score was meant to avoid.
Shrinking the step during the run. The bias also vanishes if the step decreases along the chain, but only if it decreases slowly. Welling and Teh (2011), Eq. 2, use the Robbins–Monro conditions, ∑kηk=∞ and ∑kηk2<∞, for example ηk∝(b+k)−γ with γ∈(0.5,1]. The first condition lets the chain travel any distance from its start. A geometric decay ηk=η0ρk breaks it: the steps add up to η0/(1−ρ), and the chain freezes before it has mixed. In the Gaussian example the starting point survives in xK with the factor ∏k(1−ηk/2); with η0=0.01 and ρ=0.9, after ten steps that factor is still 0.968.
Temperature. Some samplers scale the noise by a temperature τ, xk+1=xk+2ηs(xk)+τηzk. Since 2η∇logp=2τ2η∇logp1/τ2, this is the plain sampler for the density proportional to p1/τ2 with step τ2η, so a temperature below 1 concentrates the samples near the modes. In the Gaussian example the stationary variance becomes τ2/(1−η/4): 0.256 for τ=0.5 and η=0.1, close to the variance 0.25 of p4∝N(0,1/4).
Two problems appear as soon as the data are more than one blob (Song and Ermon, 2019, §3).
The score is unknown where there are no data. Score matching weights its error by p(x), so in regions of low density the learned score is barely trained, and a chain started there, as every chain started from noise is, follows arbitrary arrows. When the data lie near a low-dimensional manifold, as images do, almost all of space is such a region, and the score of the data is not even defined off the manifold.
Langevin dynamics cannot balance separated modes. Let p=πp1+(1−π)p2 with p1 and p2 on disjoint supports. On the support of p1, ∇logp=∇logπ+∇logp1=∇logp1, since π is a constant; the same holds on the other support. The score does not depend on π at all, so neither do samples that use only the score. Modes that merely overlap a little behave almost the same way. Take p=0.8N(−4,1)+0.2N(4,1). The score is ∑krk(x)(μk−x), with rk(x) the posterior probability of component k at x. Near each mode one component dominates, its responsibility is close to 1, and the weights drop out. They show only in the gap: at x=0 the two components have the same likelihood, so the responsibilities equal the weights and the score is 0.8⋅(−4)+0.2⋅4=−2.4. But the density there is 1.3×10−4, and chains rarely visit. With the exact score, 4000 chains started uniformly on [−8,8] end with 0.52 of them in the left mode after 2000 steps of size 0.05: about the share that started on the left, instead of the true 0.8.
Noise addresses both problems (Song and Ermon, 2019, §4). The noised density qσ is positive everywhere, so its score is defined everywhere, and at large σ the noised training points spread over a much wider region, so the network is trained where chains from noise begin. A large σ also fills the gap between the modes, so that their weights become visible to a chain.
Figure
Noise fills the gap between the modes
The mixture 0.8 N(−4, 1) + 0.2 N(4, 1) before and after adding N(0, σ²) noise. Without noise the density at 0 is 0.00013: no training points fall between the modes. Noise of σ = 3 lifts it to 0.057 and leaves one broad hump, so a score trained there sees the gap.
Song and Ermon (2019) train one network for many noise levels at once. Choose a geometric sequence σ1>σ2>⋯>σL, give the network the level as a second input, and minimize a weighted average of denoising objectives (their Eq. 5–6):
Why the weight σi2? Moving it inside the norm gives σ2ℓ=21E∥σsθ+ε∥2. By Tweedie, the optimal σsθ is −E[ε∣x~], a quantity of the size of a standard normal at every level, so every weighted term is of the same order and no level dominates the sum. Song and Ermon arrived at the same weight from the observation that trained networks have ∥sθ∥∝1/σ.
The network sees the level. In the NCSN of Song and Ermon every normalization layer has a learned scale and shift for each level (§4.1). In a small fully connected network the level can select a learned vector that is added to a hidden layer, h↦Wh+b+ei; diffusion networks receive their timestep in the same ways (DDPM note). Training draws one level per example: the average over levels is Ei∼Uniform{1,…,L}[σi2ℓ(θ;σi)], so one uniformly drawn level gives an unbiased estimate, as the random timestep does for DDPM.
Figure
The score field at four noise levels
The exact score of noised data at the levels 1, 0.215, 0.046 and 0.01. The data are 100 Gaussians (std 0.05) along a Swiss roll, dots at their centres. Arrow length grows like log(1 + |s|), scaled per panel; the longest arrow’s value is printed below. At σ = 1 one smooth field; at σ = 0.01 arrows point at the nearest arm.
Sampling, annealed Langevin dynamics (their Algorithm 1), walks down the levels:
Start from a fixed prior distribution, such as uniform noise.
For i=1,…,L, set the step αi=ϵσi2/σL2 and run T Langevin steps with sθ(⋅,σi), starting from where the previous level stopped.
Return the last sample.
At the largest level the chain moves freely and the weights of the modes are visible; each smaller level refines the sample, with a step that shrinks with the noise. For images the paper uses L=10 levels from σ1=1 to σ10=0.01, T=100 steps per level and ϵ=2×10−5 (§5).
In the mixture example, annealing through ten geometric levels from σ=5 to σ=0.1, 100 steps each with ϵ=0.002, with the exact score of each qσ, ends with 0.79 of the chains in the left mode, against the true 0.8. How well this works depends on the choices: the levels must overlap, each must get enough steps of a suitable size, and a learned score adds its own error.
Figure
Without noise, Langevin loses the mode weights
Final positions of 4000 Langevin chains started uniformly on [−8, 8], with the exact score of 0.8 N(−4, 1) + 0.2 N(4, 1) (line: true density). Left: 2000 steps of size 0.05 on the data's score; 0.52 end in the left mode instead of 0.80. Right: 100 steps at each of 10 levels from σ = 5 to 0.1; 0.79.
Denoising score matching at level t therefore regresses sθ(xt,t) onto −ε/1−αˉt. Write the network as sθ(xt,t)=−εθ(xt,t)/1−αˉt, and weight level t by the noise variance 1−αˉt, the σ2 rule above:
Averaged over a uniform t, this is 21Lsimple: DDPM’s loss is noise-conditional denoising score matching with the noise variance as weight, and its noise predictor is a score, εθ=−1−αˉtsθ (Ho, Jain and Abbeel, 2020, §3.2). Tweedie’s formula, applied to the scaled image αˉtx0 observed with noise of variance 1−αˉt, turns it into a guess of the image:
E[x0∣xt]=αˉtxt+(1−αˉt)∇logq(xt).
The sampler matches too. Substituting εθ=−1−αˉtsθ into DDPM’s sampling step:
Apart from the rescaling by 1/αt, which undoes the shrinking of the forward step, each step moves along the learned score and adds fresh noise, with the noise level changing from step to step as in annealing. Ho, Jain and Abbeel describe it as resembling Langevin dynamics (§3.2). The weights differ: βt on the score against noise variance σt2≈βt, where a Langevin step puts half the noise variance on the score. They are the weights of the reverse-time SDE, whose drift splits into two halves: one moves the density back along the forward path, the other, with the noise, is a Langevin step that holds it in place (continuous-time note).
The score matching notebook of the HSE-LAMBDA Deep Generative Models repository works on a 2-D Swiss roll of 10,000 points (noise 0.5, coordinates divided by 10) with a two-layer Softplus network and Adam at 10−3. The notebook documents procedures and has no saved results.
Hyvärinen’s objective with the exact Jacobian, one backward pass per output coordinate, trained for 2000 full-batch steps; the score is drawn on a 50×50 grid with arrows of length log(1+∥s∥).
Gradient ascent against Langevin dynamics from the fixed point (1.5,−1.5). The Langevin step starts at 10−2 and is multiplied by 0.9 after each of its ten steps: a geometric decay whose steps add up to 0.065, so the chain barely leaves its start.
Sliced score matching on 1000 points. The directions are normalized to unit length, so E[vv⊤]=I/d, and both terms are averaged over coordinates; both terms carry the same factor 1/d2, so the minimizer is unchanged.
Denoising score matching with σ=0.01 for 5000 steps: targets of size 1/σ=100, the price of a small σ.
A noise-conditional network with the four levels 1, 0.215, 0.046 and 0.01, the σ2 weight, an additive learned vector per level, and one random level per point. Annealed Langevin sampling is not implemented.
DDPM’s thousand steps and NCSN’s noise levels are two discretizations of one idea: noise the data gradually, learn the score at every noise level, and walk back. Continuous-time diffusion and flow matching lets the number of steps go to infinity, which turns both into one stochastic differential equation with a deterministic counterpart, and from there reaches flow matching.
K. P. Murphy. Probabilistic Machine Learning: Advanced Topics. MIT Press, 2023. §24.3 (score matching, denoising and sliced), §25.3 (score-based generative models and their equivalence to DDPM).
R. Isachenko. Deep Generative Models, MIPT and Yandex School of Data Analysis, 2025. Course repository; lectures 6–7 (score matching, the proof of denoising score matching, NCSN).