Course contents 28 / 34

28 / 34 · Diffusion Models

Score matching and Langevin dynamics

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.

On this page 8

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)\mathcal N(\mu, \sigma^2 I) the arrow at xx is −(x−μ)/σ2-(x - \mu)/\sigma^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)=∇xlog⁡p(x)s(x) = \nabla_x \log p(x), for a density pp on Rd\mathbb R^d. 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.

Why the score#

Many models define a density only up to a constant, like the energy-based models: pθ(x)=e−Eθ(x)/Zθp_\theta(x) = e^{-E_\theta(x)} / Z_\theta, with a normalizer Zθ=∫e−Eθ(x) dxZ_\theta = \int e^{-E_\theta(x)}\, dx that cannot be computed in high dimension. Maximum likelihood needs log⁡Zθ\log Z_\theta. The score does not:

∇xlog⁡pθ(x)=−∇xEθ(x)−∇xlog⁡Zθ⏟= 0,\nabla_x \log p_\theta(x) = -\nabla_x E_\theta(x) - \underbrace{\nabla_x \log Z_\theta}_{=\,0} ,

because ZθZ_\theta depends on θ\theta but not on xx. The score still carries all the information about pp where pp is positive and smooth on a connected region, such as all of Rd\mathbb R^d: 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θs_\theta = -\nabla_x E_\theta, 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→Rds_\theta: \mathbb R^d \to \mathbb R^d 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.

Fitting a score: Hyvärinen’s trick#

The natural loss is the Fisher divergence, the mean squared distance between the model’s score and the data’s:

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 needs the true score, which is unknown. Hyvärinen (2005) removes it by integration by parts. In one dimension, expand the square. The term 12Ep(log⁡p)′2\tfrac12 \mathbb E_p (\log p)'^2 does not depend on θ\theta. The cross term becomes

Ep[sθ (log⁡p)′]=∫sθ(x) p(x) p′(x)p(x) dx=∫sθ(x) p′(x) dx=[sθ p]−∞∞−∫sθ′(x) p(x) dx=−Ep[sθ′(x)],\begin{aligned} \mathbb E_p\big[s_\theta\, (\log p)'\big] &= \int s_\theta(x)\, p(x)\, \frac{p'(x)}{p(x)}\, dx \\ &= \int s_\theta(x)\, p'(x)\, dx \\ &= \big[s_\theta\, p\big]_{-\infty}^{\infty} - \int s_\theta'(x)\, p(x)\, dx \\ &= -\mathbb E_p\big[s_\theta'(x)\big] , \end{aligned}

by the chain rule (log⁡p)′=p′/p(\log p)' = p'/p, integration by parts, and the assumption that sθ(x) p(x)→0s_\theta(x)\, p(x) \to 0 at ±∞\pm\infty. In dd dimensions the same step is taken in each coordinate xix_i for the component sθ,is_{\theta, i}, and the derivatives ∂sθ,i/∂xi\partial s_{\theta, i} / \partial x_i add up to the trace of the Jacobian, ∇⋅sθ\nabla \cdot s_\theta:

J(θ)=Ep[∇⋅sθ(x)+12∥sθ(x)∥2]+const.J(\theta) = \mathbb E_p\Big[\nabla \cdot s_\theta(x) + \tfrac12 \lVert s_\theta(x) \rVert^2\Big] + \text{const} .

The expectation is over data and involves only the model. Hyvärinen’s Theorem 1 states it with its conditions: pp differentiable, Ep∥sθ∥2\mathbb E_p \lVert s_\theta \rVert^2 and Ep∥∇log⁡p∥2\mathbb E_p \lVert \nabla \log p \rVert^2 finite, and p(x) sθ(x)→0p(x)\, s_\theta(x) \to 0 as ∥x∥→∞\lVert x \rVert \to \infty.

Worked example. Fit a Gaussian score sθ(x)=−a(x−μ)s_\theta(x) = -a(x - \mu), a=1/σ2a = 1/\sigma^2, to the data {1,2,4,5}\{1, 2, 4, 5\}. Then sθ′=−as_\theta' = -a, and the sample objective is the average of 12a2(x−μ)2−a\tfrac12 a^2 (x - \mu)^2 - a. Its derivative in μ\mu vanishes at μ=xˉ=3\mu = \bar x = 3. Its derivative in aa vanishes when a⋅(x−μ)2‾=1a \cdot \overline{(x - \mu)^2} = 1, so σ2=14(4+1+1+4)=2.5\sigma^2 = \tfrac14 (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→Rds_\theta: \mathbb R^d \to \mathbb R^d, ∇⋅sθ\nabla \cdot s_\theta needs dd backward passes, one per diagonal entry of the Jacobian: fine for d=2d = 2, impossible for an image with d=3⋅64⋅64d = 3 \cdot 64 \cdot 64.

Two cheaper objectives#

Sliced score matching (Song et al., 2019) projects onto random directions vv with E[vv⊤]=I\mathbb E[v v^\top] = I:

JSSM(θ)=Ep Ev[v⊤∇sθ(x) v+12(v⊤sθ(x))2].J_{\text{SSM}}(\theta) = \mathbb E_p\, \mathbb E_v \Big[v^\top \nabla s_\theta(x)\, v + \tfrac12 \big(v^\top s_\theta(x)\big)^2\Big] .

Averaged over vv this is Hyvärinen’s objective, because E[v⊤Av]=tr⁡(A E[vv⊤])=tr⁡(A)\mathbb E[v^\top A v] = \operatorname{tr}(A\, \mathbb E[v v^\top]) = \operatorname{tr}(A) and E[(v⊤s)2]=s⊤E[vv⊤] s=∥s∥2\mathbb E[(v^\top s)^2] = s^\top \mathbb E[v v^\top]\, s = \lVert s \rVert^2. One Jacobian–vector product replaces dd 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∼px \sim p and perturb it with a kernel q(x~∣x)q(\tilde x \mid x) whose score we know, so that the noised data have the density q(x~)=∫q(x~∣x) p(x) dxq(\tilde x) = \int q(\tilde x \mid x)\, p(x)\, dx. The Fisher divergence to the noised data, the explicit objective, and the denoising objective are

JESM(θ)=12 Eq(x~)∥sθ(x~)−∇log⁡q(x~)∥2,JDSM(θ)=12 Ep(x) q(x~∣x)∥sθ(x~)−∇x~log⁡q(x~∣x)∥2.\begin{aligned} J_{\text{ESM}}(\theta) &= \tfrac12\, \mathbb E_{q(\tilde x)} \big\lVert s_\theta(\tilde x) - \nabla \log q(\tilde x) \big\rVert^2 , \\ J_{\text{DSM}}(\theta) &= \tfrac12\, \mathbb E_{p(x)\, q(\tilde x \mid x)} \big\lVert s_\theta(\tilde x) - \nabla_{\tilde x} \log q(\tilde x \mid x) \big\rVert^2 . \end{aligned}

The first needs the unknown score of q(x~)q(\tilde x). The second needs only the kernel’s score. They differ by a constant:

  JESM(θ)=JDSM(θ)+C  \boxed{\;J_{\text{ESM}}(\theta) = J_{\text{DSM}}(\theta) + C\;}

with CC independent of θ\theta (Vincent, 2011, §4.2, proved in the Appendix).

Proof that the two objectives differ by a constant

Expand both squares. Each gives three terms: 12∥sθ∥2\tfrac12 \lVert s_\theta \rVert^2, a cross term, and the square of the target score.

The squares of the targets, 12E∥∇log⁡q(x~)∥2\tfrac12 \mathbb E \lVert \nabla \log q(\tilde x) \rVert^2 and 12E∥∇x~log⁡q(x~∣x)∥2\tfrac12 \mathbb E \lVert \nabla_{\tilde x} \log q(\tilde x \mid x) \rVert^2, do not depend on θ\theta; call them C1C_1 and C2C_2.

The terms 12∥sθ(x~)∥2\tfrac12 \lVert s_\theta(\tilde x) \rVert^2 depend on x~\tilde x only, and the marginal of x~\tilde x under p(x) q(x~∣x)p(x)\, q(\tilde x \mid x) is q(x~)q(\tilde x), so they have the same expectation in both objectives.

The cross terms are equal too. Starting from the explicit one:

S(θ)=Eq(x~)[sθ⊤∇log⁡q(x~)]=∫sθ(x~)⊤∇q(x~) dx~=∫sθ(x~)⊤∇x~∫p(x) q(x~∣x) dx dx~=∬p(x) sθ(x~)⊤∇x~q(x~∣x) dx dx~=∬p(x) q(x~∣x) sθ⊤∇x~log⁡q(x~∣x) dx dx~=Ep(x) q(x~∣x)[sθ⊤∇x~log⁡q(x~∣x)].\begin{aligned} S(\theta) &= \mathbb E_{q(\tilde x)}\big[s_\theta^\top \nabla \log q(\tilde x)\big] \\ &= \int s_\theta(\tilde x)^\top \nabla q(\tilde x)\, d\tilde x \\ &= \int s_\theta(\tilde x)^\top \nabla_{\tilde x} \int p(x)\, q(\tilde x \mid x)\, dx\, d\tilde x \\ &= \iint p(x)\, s_\theta(\tilde x)^\top \nabla_{\tilde x} q(\tilde x \mid x)\, dx\, d\tilde x \\ &= \iint p(x)\, q(\tilde x \mid x)\, s_\theta^\top \nabla_{\tilde x} \log q(\tilde x \mid x)\, dx\, d\tilde x \\ &= \mathbb E_{p(x)\, q(\tilde x \mid x)}\big[s_\theta^\top \nabla_{\tilde x} \log q(\tilde x \mid x)\big] . \end{aligned}

The steps use, in order: q∇log⁡q=∇qq \nabla \log q = \nabla q; the definition of q(x~)q(\tilde x); differentiation under the integral sign, which needs ∇x~q(x~∣x)\nabla_{\tilde x} q(\tilde x \mid x) to be integrable against p(x)p(x); the same identity q∇log⁡q=∇qq \nabla \log q = \nabla q for the kernel; and the definition of the expectation over the joint. Subtracting the expanded objectives leaves JESM−JDSM=C1−C2J_{\text{ESM}} - J_{\text{DSM}} = C_1 - C_2. The kernel’s form never entered, only that log⁡q(x~∣x)\log q(\tilde x \mid x) is differentiable in x~\tilde x.

The usual kernel is Gaussian, x~=x+σε\tilde x = x + \sigma \varepsilon with ε∼N(0,I)\varepsilon \sim \mathcal N(0, I); write qσq_\sigma for the resulting density of the noised data. The kernel’s score is

∇x~log⁡N(x~;x,σ2I)=−x~−xσ2=−εσ,\begin{aligned} \nabla_{\tilde x} \log \mathcal N(\tilde x; x, \sigma^2 I) &= -\frac{\tilde x - x}{\sigma^2} \\ &= -\frac{\varepsilon}{\sigma} , \end{aligned}

and the objective is a regression of the network’s output at the noised point onto −ε/σ-\varepsilon / \sigma:

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 .

Tweedie’s formula#

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~]\mathbb E[-\varepsilon / \sigma \mid \tilde x]. That conditional mean is the score of qσq_\sigma. Differentiate qσ(x~)=∫p(x) N(x~;x,σ2I) dxq_\sigma(\tilde x) = \int p(x)\, \mathcal N(\tilde x; x, \sigma^2 I)\, dx under the integral, divide by qσ(x~)q_\sigma(\tilde x), and recognize Bayes’ theorem, 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):

∇log⁡qσ(x~)=1qσ(x~)∫p(x) N(x~;x,σ2I) x−x~σ2 dx=∫p(x∣x~) 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 \\ &= \int p(x \mid \tilde x)\, \frac{x - \tilde x}{\sigma^2}\, dx \\ &= \frac{\mathbb E[x \mid \tilde x] - \tilde x}{\sigma^2} . \end{aligned}

Rearranged, this is Tweedie’s formula (Efron, 2011, Eq. 1.4):

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

The best guess of the clean point is the noisy point moved along the score of the noised data. With ε=(x~−x)/σ\varepsilon = (\tilde x - x)/\sigma, it also gives E[ε∣x~]=−σ ∇log⁡qσ(x~)\mathbb E[\varepsilon \mid \tilde x] = -\sigma\, \nabla \log q_\sigma(\tilde x), so the optimal regression target E[−ε/σ∣x~]\mathbb E[-\varepsilon / \sigma \mid \tilde x] is the score itself. Denoising and score estimation are the same task.

Worked example. Data x∼N(0,1)x \sim \mathcal N(0, 1) and σ=0.5\sigma = 0.5. Then x~∼N(0,1.25)\tilde x \sim \mathcal N(0, 1.25), whose score is −x~/1.25=−0.8 x~-\tilde x / 1.25 = -0.8\, \tilde x. Tweedie gives E[x∣x~]=x~−0.25⋅0.8 x~=0.8 x~\mathbb E[x \mid \tilde x] = \tilde x - 0.25 \cdot 0.8\, \tilde x = 0.8\, \tilde x, the familiar shrinkage of a Gaussian posterior mean. Directly: ε\varepsilon and x~\tilde x are jointly Gaussian with Cov⁡(ε,x~)=0.5\operatorname{Cov}(\varepsilon, \tilde x) = 0.5, so E[ε∣x~]=0.5 x~/1.25=0.4 x~\mathbb E[\varepsilon \mid \tilde x] = 0.5\, \tilde x / 1.25 = 0.4\, \tilde x, and E[−ε/σ∣x~]=−0.8 x~\mathbb E[-\varepsilon / \sigma \mid \tilde x] = -0.8\, \tilde x, the score.

The price of the noise#

Denoising score matching estimates the score of qσq_\sigma, not of pp. A small σ\sigma keeps the two close, but the target −ε/σ-\varepsilon / \sigma then scatters widely around its mean, the score. Per coordinate, the law of total variance splits Var⁡(εi)=1\operatorname{Var}(\varepsilon_i) = 1 into the average scatter and the variance of the mean, and Tweedie gives that mean, E[εi∣x~]=−σ ∂ilog⁡qσ(x~)\mathbb E[\varepsilon_i \mid \tilde x] = -\sigma\, \partial_i \log q_\sigma(\tilde x):

1=E[Var⁡(εi∣x~)]+σ2 E[(∂ilog⁡qσ)2],E[Var⁡(εiσ ∣ x~)]=1σ2−E[(∂ilog⁡qσ)2]≤1σ2.\begin{aligned} 1 &= \mathbb E\big[\operatorname{Var}(\varepsilon_i \mid \tilde x)\big] + \sigma^2\, \mathbb E\big[(\partial_i \log q_\sigma)^2\big] , \\ \mathbb E\Big[\operatorname{Var}\Big(\frac{\varepsilon_i}{\sigma} \,\Big|\, \tilde x\Big)\Big] &= \frac{1}{\sigma^2} - \mathbb E\big[(\partial_i \log q_\sigma)^2\big] \\ &\le \frac{1}{\sigma^2} . \end{aligned}

In the worked example above, 1/0.25−0.8=3.21/0.25 - 0.8 = 3.2 against the bound 44. The subtracted term stays bounded as σ→0\sigma \to 0. For differentiable pp that vanishes at infinity, moving the derivative from the kernel onto pp by integration by parts gives ∂ilog⁡qσ(x~)=E[∂ilog⁡p(x)∣x~]\partial_i \log q_\sigma(\tilde x) = \mathbb E[\partial_i \log p(x) \mid \tilde x], and Jensen’s inequality bounds its mean square by Ep[(∂ilog⁡p)2]\mathbb E_p[(\partial_i \log p)^2], finite for smooth data. So the scatter grows like 1/σ21/\sigma^2 (Murphy, 2023, §24.3.2). It does not depend on θ\theta and does not move the minimizer, but it is noise in every gradient estimate. A large σ\sigma gives an easy regression of a blurred density. The way out, below, is many values of σ\sigma at once.

Sampling with Langevin dynamics#

Given a score, Langevin dynamics produces samples:

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

The update is one Euler step of the Langevin diffusion, whose stationary density is pp; 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 log⁡p\log p and ends at a mode, not at a sample. Under regularity conditions the distribution of xkx_k becomes exactly pp as η→0\eta \to 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)p = \mathcal N(0, 1), s(x)=−xs(x) = -x, and a stationary variance vv must reproduce itself under one step, which gives (derived here)

v=11−η/4.v = \frac{1}{1 - \eta/4} .

With η=0.1\eta = 0.1 the chain settles at variance 1.0261.026 instead of 11; with η=0.5\eta = 0.5, at 1.1431.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=∞\sum_k \eta_k = \infty and ∑kηk2<∞\sum_k \eta_k^2 < \infty, for example ηk∝(b+k)−γ\eta_k \propto (b + k)^{-\gamma} with γ∈(0.5,1]\gamma \in (0.5, 1]. The first condition lets the chain travel any distance from its start. A geometric decay ηk=η0ρk\eta_k = \eta_0 \rho^k breaks it: the steps add up to η0/(1−ρ)\eta_0 / (1 - \rho), and the chain freezes before it has mixed. In the Gaussian example the starting point survives in xKx_K with the factor ∏k(1−ηk/2)\prod_k (1 - \eta_k / 2); with η0=0.01\eta_0 = 0.01 and ρ=0.9\rho = 0.9, after ten steps that factor is still 0.9680.968.

Temperature. Some samplers scale the noise by a temperature τ\tau, xk+1=xk+η2s(xk)+τη zkx_{k+1} = x_k + \frac\eta2 s(x_k) + \tau \sqrt\eta\, z_k. Since η2∇log⁡p=τ2η2∇log⁡p1/τ2\frac\eta2 \nabla \log p = \frac{\tau^2 \eta}{2} \nabla \log p^{1/\tau^2}, this is the plain sampler for the density proportional to p1/τ2p^{1/\tau^2} with step τ2η\tau^2 \eta, so a temperature below 11 concentrates the samples near the modes. In the Gaussian example the stationary variance becomes τ2/(1−η/4)\tau^2 / (1 - \eta/4): 0.2560.256 for τ=0.5\tau = 0.5 and η=0.1\eta = 0.1, close to the variance 0.250.25 of p4∝N(0,1/4)p^4 \propto \mathcal N(0, 1/4).

Why one noise level fails#

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)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−π) p2p = \pi\, p_1 + (1 - \pi)\, p_2 with p1p_1 and p2p_2 on disjoint supports. On the support of p1p_1, ∇log⁡p=∇log⁡π+∇log⁡p1=∇log⁡p1\nabla \log p = \nabla \log \pi + \nabla \log p_1 = \nabla \log p_1, since π\pi is a constant; the same holds on the other support. The score does not depend on π\pi 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.8 N(−4,1)+0.2 N(4,1)p = 0.8\, \mathcal N(-4, 1) + 0.2\, \mathcal N(4, 1). The score is ∑krk(x)(μk−x)\sum_k r_k(x)(\mu_k - x), with rk(x)r_k(x) the posterior probability of component kk at xx. Near each mode one component dominates, its responsibility is close to 11, and the weights drop out. They show only in the gap: at x=0x = 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.40.8 \cdot (-4) + 0.2 \cdot 4 = -2.4. But the density there is 1.3×10−41.3 \times 10^{-4}, and chains rarely visit. With the exact score, 4000 chains started uniformly on [−8,8][-8, 8] end with 0.520.52 of them in the left mode after 2000 steps of size 0.050.05: about the share that started on the left, instead of the true 0.80.8.

Noise addresses both problems (Song and Ermon, 2019, §4). The noised density qσq_\sigma is positive everywhere, so its score is defined everywhere, and at large σ\sigma the noised training points spread over a much wider region, so the network is trained where chains from noise begin. A large σ\sigma 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 density at three noise levelsThree density curves over −10 to 10. Without noise: two peaks at −4 and 4, the left one four times higher, and almost zero density between them. At σ = 1 the peaks are wider and lower; at σ = 3 one broad hump remains, shifted to the left.-8-4048xdata, σ = 0σ = 1σ = 3The mixture density at three noise levelsThree density curves over −10 to 10. Without noise: two peaks at −4 and 4, the left one four times higher, and almost zero density between them. At σ = 1 the peaks are wider and lower; at σ = 3 one broad hump remains, shifted to the left.-8-4048xdata, σ = 0σ = 1σ = 3
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.

Noise-conditional scores and annealing#

Song and Ermon (2019) train one network for many noise levels at once. Choose a geometric sequence σ1>σ2>⋯>σL\sigma_1 > \sigma_2 > \dots > \sigma_L, give the network the level as a second input, and minimize a weighted average of denoising objectives (their Eq. 5–6):

L(θ)=1L∑i=1Lσi2 ℓ(θ;σi),ℓ(θ;σ)=12 Ex,ε∥sθ(x+σε,σ)+εσ∥2.\begin{aligned} \mathcal L(\theta) &= \frac1L \sum_{i=1}^{L} \sigma_i^2\, \ell(\theta; \sigma_i) , \\ \ell(\theta; \sigma) &= \tfrac12\, \mathbb E_{x, \varepsilon} \Big\lVert s_\theta(x + \sigma \varepsilon, \sigma) + \frac{\varepsilon}{\sigma} \Big\rVert^2 . \end{aligned}

Why the weight σi2\sigma_i^2? Moving it inside the norm gives σ2ℓ=12E∥σsθ+ε∥2\sigma^2 \ell = \tfrac12 \mathbb E \lVert \sigma s_\theta + \varepsilon \rVert^2. By Tweedie, the optimal σsθ\sigma s_\theta is −E[ε∣x~]-\mathbb E[\varepsilon \mid \tilde 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/σ\lVert s_\theta \rVert \propto 1/\sigma.

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+eih \mapsto W h + b + e_i; 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)]\mathbb E_{i \sim \mathrm{Uniform}\{1, \dots, L\}}[\sigma_i^2 \ell(\theta; \sigma_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

Score vector fields for four noise levelsFour square panels with arrows on a grid and dots along a spiral. At sigma equal to 1 the arrows converge smoothly toward the middle of the spiral; at smaller sigma they point more and more sharply at the nearest arm, and the largest score magnitude grows from 1.7 to 562.σ = 1max |s| ≈ 1.7σ = 0.215max |s| ≈ 30σ = 0.046max |s| ≈ 314σ = 0.01max |s| ≈ 562Score vector fields for four noise levelsFour square panels with arrows on a grid and dots along a spiral. At sigma equal to 1 the arrows converge smoothly toward the middle of the spiral; at smaller sigma they point more and more sharply at the nearest arm, and the largest score magnitude grows from 1.7 to 562.σ = 1max |s| ≈ 1.7σ = 0.215max |s| ≈ 30σ = 0.046max |s| ≈ 314σ = 0.01max |s| ≈ 562
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:

  1. Start from a fixed prior distribution, such as uniform noise.
  2. For i=1,…,Li = 1, \dots, L, set the step αi=ϵ σi2/σL2\alpha_i = \epsilon\, \sigma_i^2 / \sigma_L^2 and run TT Langevin steps with sθ(⋅,σi)s_\theta(\cdot, \sigma_i), starting from where the previous level stopped.
  3. 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=10L = 10 levels from σ1=1\sigma_1 = 1 to σ10=0.01\sigma_{10} = 0.01, T=100T = 100 steps per level and ϵ=2×10−5\epsilon = 2 \times 10^{-5} (§5).

In the mixture example, annealing through ten geometric levels from σ=5\sigma = 5 to σ=0.1\sigma = 0.1, 100 steps each with ϵ=0.002\epsilon = 0.002, with the exact score of each qσq_\sigma, ends with 0.790.79 of the chains in the left mode, against the true 0.80.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

Histograms of the final chain positionsplain Langevin, σ = 0: share in the left mode 0.52; annealed over 10 noise levels: share in the left mode 0.79; true 0.80.plain Langevin, σ = 0-8-4048share in the left mode: 0.52true 0.80annealed over 10 noise levels-8-4048share in the left mode: 0.79true 0.80Histograms of the final chain positionsplain Langevin, σ = 0: share in the left mode 0.52; annealed over 10 noise levels: share in the left mode 0.79; true 0.80.plain Langevin, σ = 0-8-4048share in the left mode: 0.52true 0.80annealed over 10 noise levels-8-4048share in the left mode: 0.79true 0.80
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.

The same model as DDPM#

DDPM noises an image as xt=αˉt x0+1−αˉt εx_t = \sqrt{\bar\alpha_t}\, x_0 + \sqrt{1 - \bar\alpha_t}\, \varepsilon (forward process). This is a Gaussian kernel too, and its score is

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

Denoising score matching at level tt therefore regresses sθ(xt,t)s_\theta(x_t, t) onto −ε/1−αˉt-\varepsilon / \sqrt{1 - \bar\alpha_t}. Write the network as sθ(xt,t)=−εθ(xt,t)/1−αˉts_\theta(x_t, t) = -\varepsilon_\theta(x_t, t) / \sqrt{1 - \bar\alpha_t}, and weight level tt by the noise variance 1−αˉt1 - \bar\alpha_t, the σ2\sigma^2 rule above:

(1−αˉt)⋅12∥sθ+ε1−αˉt∥2=12∥ε−εθ(xt,t)∥2.\begin{aligned} (1 - \bar\alpha_t) \cdot \tfrac12 \Big\lVert s_\theta + \frac{\varepsilon}{\sqrt{1 - \bar\alpha_t}} \Big\rVert^2 &= \tfrac12 \lVert \varepsilon - \varepsilon_\theta(x_t, t) \rVert^2 . \end{aligned}

Averaged over a uniform tt, this is 12Lsimple\tfrac12 L_{\text{simple}}: DDPM’s loss is noise-conditional denoising score matching with the noise variance as weight, and its noise predictor is a score, εθ=−1−αˉt sθ\varepsilon_\theta = -\sqrt{1 - \bar\alpha_t}\, s_\theta (Ho, Jain and Abbeel, 2020, §3.2). Tweedie’s formula, applied to the scaled image αˉt x0\sqrt{\bar\alpha_t}\, x_0 observed with noise of variance 1−αˉt1 - \bar\alpha_t, turns it into a guess of the image:

E[x0∣xt]=xt+(1−αˉt) ∇log⁡q(xt)αˉt.\mathbb E[x_0 \mid x_t] = \frac{x_t + (1 - \bar\alpha_t)\, \nabla \log q(x_t)}{\sqrt{\bar\alpha_t}} .

The sampler matches too. Substituting εθ=−1−αˉt sθ\varepsilon_\theta = -\sqrt{1 - \bar\alpha_t}\, s_\theta into DDPM’s sampling step:

xt−1=1αt(xt−βt1−αˉt εθ)+σtz=1αt(xt+βt sθ(xt,t))+σtz.\begin{aligned} x_{t-1} &= \frac{1}{\sqrt{\alpha_t}} \Big(x_t - \frac{\beta_t}{\sqrt{1 - \bar\alpha_t}}\, \varepsilon_\theta\Big) + \sigma_t z \\ &= \frac{1}{\sqrt{\alpha_t}} \big(x_t + \beta_t\, s_\theta(x_t, t)\big) + \sigma_t z . \end{aligned}

Apart from the rescaling by 1/αt1/\sqrt{\alpha_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\beta_t on the score against noise variance σt2≈βt\sigma_t^2 \approx \beta_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).

What you should remember#

  • The score ∇xlog⁡p\nabla_x \log p ignores the normalizer; on a connected positive region it fixes pp.
  • Hyvärinen: J=Ep[∇⋅sθ+12∥sθ∥2]+constJ = \mathbb E_p[\nabla \cdot s_\theta + \tfrac12 \lVert s_\theta \rVert^2] + \text{const}, by integration by parts.
  • Denoising score matching regresses onto −ε/σ-\varepsilon/\sigma and equals the Fisher divergence to the noised data up to a constant.
  • Tweedie: E[x∣x~]=x~+σ2∇log⁡qσ(x~)\mathbb E[x \mid \tilde x] = \tilde x + \sigma^2 \nabla \log q_\sigma(\tilde x), so denoising is score estimation.
  • One noise level fails in empty regions and between modes; noise-conditional scores weighted by σ2\sigma^2 and annealed Langevin fix both.
  • DDPM is this with εθ=−1−αˉt sθ\varepsilon_\theta = -\sqrt{1 - \bar\alpha_t}\, s_\theta.

Notebook#

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.50.5, coordinates divided by 10) with a two-layer Softplus network and Adam at 10−310^{-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×5050 \times 50 grid with arrows of length log⁡(1+∥s∥)\log(1 + \lVert s \rVert).
  • Gradient ascent against Langevin dynamics from the fixed point (1.5,−1.5)(1.5, -1.5). The Langevin step starts at 10−210^{-2} and is multiplied by 0.90.9 after each of its ten steps: a geometric decay whose steps add up to 0.0650.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\mathbb E[v v^\top] = I/d, and both terms are averaged over coordinates; both terms carry the same factor 1/d21/d^2, so the minimizer is unchanged.
  • Denoising score matching with σ=0.01\sigma = 0.01 for 5000 steps: targets of size 1/σ=1001/\sigma = 100, the price of a small σ\sigma.
  • A noise-conditional network with the four levels 11, 0.2150.215, 0.0460.046 and 0.010.01, the σ2\sigma^2 weight, an additive learned vector per level, and one random level per point. Annealed Langevin sampling is not implemented.

Next#

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.

References#