VerifiedarXiv:2011.1345628 min
Diffusion · Theory

Score SDE: Score-Based Generative Modeling through Stochastic Differential Equations

Gradually noising data is a stochastic differential equation, and running that equation backward with a learned score generates new data.

Score matching with Langevin dynamics and denoising diffusion turn out to be two discretizations of one family of equations. The same learned score then gives a sampler with an error-correcting step, a deterministic ODE with exact likelihoods, and conditional generation from an unconditional model.

Explaining the paperScore-Based Generative Modeling through Stochastic Differential EquationsSong, Sohl-Dickstein, Kingma, Kumar, Ermon, Poole · Stanford & Google Brain · ICLR 2021 · arXiv:2011.13456 ↗

In 2019 a score-matching model reached an FID (a distance between generated and real image statistics; lower is better) of 25.32 on CIFAR-10; in 2020 a denoising diffusion model reached 3.17, by what looked like a different method. Treating both as limits of continuous noising processes, the paper gets the FID down to 2.20.

By late 2020 two families of generative models trained on noised images. Score matching with Langevin dynamics (SMLD), the line of NCSN, added Gaussian noise at a ladder of growing scales, trained one network to estimate the gradient of the log-density at every scale, and generated by walking down the ladder with a noisy gradient-following sampler. DDPM defined a 1000-step chain that shrinks an image slightly and adds a little noise at each step, trained a network to undo one step, and generated by running the chain in reverse.

Song and coauthors let the number of noise scales go to infinity. Infinitely many, infinitely close scales make the noising a continuous process driven by random noise, which is a stochastic differential equation (SDE). A 1982 result of Brian Anderson gives the SDE that runs any such process backward in time, and that reverse equation contains exactly one unknown: the gradient of the log-density of the noised data, called the score, at every noise level. SMLD and DDPM were both already training networks to estimate that quantity.

The page follows the paper's order:

The forward SDE: noising data as a continuous process

A 32×32 color image is a vector of 3072 numbers, a point in 3072-dimensional space. Real images occupy a tiny region of that space: pick 3072 random numbers and you get static. Generating an image means producing a point inside that region, starting from something easy to sample.

The paper builds the path in the opposite direction first. A process x(t)\mathbf{x}(t) runs over time t∈[0,T]t \in [0, T]. At t=0t = 0 it is a data sample, drawn from p0=pdatap_0 = p_{\text{data}}. Noise is mixed in continuously, and by t=Tt = T the distribution pTp_T is a fixed Gaussian, the prior, which carries no information about the data and is trivial to sample. The rule for one infinitesimal step is an Itô SDE:

dx=f(x,t) dt+g(t) dwd\mathbf{x} = \mathbf{f}(\mathbf{x}, t)\,dt + g(t)\,d\mathbf{w}
(5)

f(x,t)\mathbf{f}(\mathbf{x}, t) is the drift, a deterministic velocity that can depend on position. g(t)g(t) is the diffusion coefficient, a scalar that sets how much noise enters at time tt. w\mathbf{w} is a standard Wiener process (Brownian motion): over a step of length Δt\Delta t, its increment is Gaussian with variance Δt\Delta t in every coordinate. The paper takes gg to be a scalar that does not depend on x\mathbf{x} for ease of presentation and generalizes to a d×dd \times d matrix G(x,t)\mathbf{G}(\mathbf{x}, t) in its Appendix A. The forward SDE has no trainable parameters. The designer picks f\mathbf{f} and gg so that pTp_T ends up close to a known Gaussian.

To simulate (5) on a computer, replace dtdt with a small step Δt\Delta t and dwd\mathbf{w} with Δt z\sqrt{\Delta t}\,\mathbf{z}, z∼N(0,I)\mathbf{z} \sim \mathcal{N}(0, I) (the Euler-Maruyama method). For a worked step, take the forward SDE that DDPM turns into, with f=−12β(t)x\mathbf{f} = -\tfrac{1}{2}\beta(t)\mathbf{x}, g=β(t)g = \sqrt{\beta(t)}, and the paper's schedule β(t)=0.1+19.9 t\beta(t) = 0.1 + 19.9\,t on t∈[0,1]t \in [0, 1]. At t=0.5t = 0.5, β=10.05\beta = 10.05, and with Δt=0.001\Delta t = 0.001 one step is

x←x−12(10.05)(0.001) x+10.05×0.001  z=0.99498 x+0.1002 z.\mathbf{x} \leftarrow \mathbf{x} - \tfrac{1}{2}(10.05)(0.001)\,\mathbf{x} + \sqrt{10.05 \times 0.001}\;\mathbf{z} = 0.99498\,\mathbf{x} + 0.1002\,\mathbf{z}.x←x−12(10.05)(0.001) x+10.05×0.001  z=0.99498 x+0.1002 z.\begin{aligned} \mathbf{x} &\leftarrow \mathbf{x} - \tfrac{1}{2}(10.05)(0.001)\,\mathbf{x} + \sqrt{10.05 \times 0.001}\;\mathbf{z} \\ &= 0.99498\,\mathbf{x} + 0.1002\,\mathbf{z}. \end{aligned}

A pixel at 0.8 moves to 0.796 plus a Gaussian kick of standard deviation 0.10. A thousand such steps from t=0t = 0 to t=1t = 1 multiply the original signal by e−5.025e^{-5.025} (half of ∫01β=10.05\int_0^1 \beta = 10.05 in the exponent), which is 0.0066, so what remains at t=1t = 1 is 0.66% of the image plus noise of variance 1−e−10.051 - e^{-10.05}, a standard Gaussian to four decimals.

Drag tt in Figure 1 to run this SDE on three clusters of 2D points. The clusters spread and merge into one blob at the origin, and the white path is one particle's simulated trajectory, which wanders because every step adds a fresh kick.

Figure 1 · the forward SDE
t = 0.00
Three clusters of data under the VP SDE with β(t)=0.1+19.9t\beta(t) = 0.1 + 19.9t. Drag tt from 0 to 1: the clusters wash out into the prior N(0,I)\mathcal{N}(0, I) (dashed circles at one and two standard deviations). The white line is one particle's Euler-Maruyama path up to the current tt.

At t=1t = 1 the cloud no longer depends on which cluster a particle started in. Going back has to restore that information at every noise level, and the next section shows that the one quantity it needs is the score.

How the reverse-time SDE generates data

A random process played backward in time is in general a different kind of process. Anderson (1982) proved that diffusions are an exception: the time-reversal of a diffusion is again a diffusion, with the same noise coefficient and a drift that can be written down. For the forward SDE (5), the reverse-time SDE is

dx=[f(x,t)−g(t)2 ∇xlog⁡pt(x)] dt+g(t) dwˉd\mathbf{x} = \big[\mathbf{f}(\mathbf{x}, t) - g(t)^2\,\nabla_{\mathbf{x}}\log p_t(\mathbf{x})\big]\,dt + g(t)\,d\bar{\mathbf{w}}
(6)

Time runs from TT down to 00, so dtdt is a negative step, and wˉ\bar{\mathbf{w}} is a Wiener process for time flowing backward. ptp_t is the distribution of the noised data at time tt, the cloud in Figure 1 at that slider position. The noise term is the same g(t)g(t) as in the forward SDE. The drift is the forward drift minus g(t)2g(t)^2 times one new quantity, ∇xlog⁡pt(x)\nabla_{\mathbf{x}}\log p_t(\mathbf{x}). The coefficient is the full g(t)2g(t)^2, with no factor of one half; the half appears later, in the ODE (13).

∇xlog⁡pt(x)\nabla_{\mathbf{x}}\log p_t(\mathbf{x}) is the score of ptp_t: the gradient of the log-density with respect to the data point x\mathbf{x}, not with respect to any model parameters. It is a vector field with one arrow per point in data space, and each arrow points in the direction in which the noised density increases fastest. With the score known at every tt, generation is simulation: draw x(T)\mathbf{x}(T) from the prior and integrate (6) down to t=0t = 0.

The score does not need the density's normalizing constant. A model density is usually written p~(x)/Z\tilde p(\mathbf{x})/Z, where ZZ is the integral of p~\tilde p over all 3072 dimensions and cannot be computed for a neural network. Taking the gradient of the log removes it:

∇xlog⁡p~(x)Z=∇xlog⁡p~(x)−∇xlog⁡Z=∇xlog⁡p~(x),\nabla_{\mathbf{x}}\log\frac{\tilde p(\mathbf{x})}{Z} = \nabla_{\mathbf{x}}\log \tilde p(\mathbf{x}) - \nabla_{\mathbf{x}}\log Z = \nabla_{\mathbf{x}}\log \tilde p(\mathbf{x}),

because ZZ is a constant. A network that outputs a 3072-dimensional vector can represent a score directly, with no constraint on its architecture. Normalizing flows, which model the density itself, have to restrict their layers to invertible maps with cheap Jacobian determinants so that the density stays normalized and computable.

For one concrete reverse step, use the noising SMLD turns into, which has f=0\mathbf{f} = 0 and noise levels σi\sigma_i. Discretizing (6) between adjacent levels gives the paper's reverse-diffusion update for this case (Algorithm 2, lines 3 and 5):

xi=xi+1+(σi+12−σi2) ∇xlog⁡pσi+1(xi+1)+σi+12−σi2  z.\mathbf{x}_i = \mathbf{x}_{i+1} + (\sigma_{i+1}^2 - \sigma_i^2)\,\nabla_{\mathbf{x}}\log p_{\sigma_{i+1}}(\mathbf{x}_{i+1}) + \sqrt{\sigma_{i+1}^2 - \sigma_i^2}\;\mathbf{z}.

Take one-dimensional data distributed as N(0,1)\mathcal{N}(0, 1). Noised to level σ\sigma, it is N(0,1+σ2)\mathcal{N}(0, 1 + \sigma^2), whose score is −x/(1+σ2)-x/(1 + \sigma^2). The CIFAR-10 schedule spaces 1000 levels geometrically from 0.01 to 50, so adjacent levels differ by a factor of 1.00856. Stepping from σi+1=10\sigma_{i+1} = 10 to σi=9.915\sigma_i = 9.915 gives σi+12−σi2=1.69\sigma_{i+1}^2 - \sigma_i^2 = 1.69. A particle at x=5x = 5 has score −5/101=−0.0495-5/101 = -0.0495, so the drift moves it 1.69×0.0495=0.0841.69 \times 0.0495 = 0.084 toward zero, and the noise adds a kick with standard deviation 1.69=1.30\sqrt{1.69} = 1.30. Each single step is mostly noise; the inward drift adds up over hundreds of steps and shrinks the spread from 50 at the prior to 1 at the data.

Figure 2 draws the score of a noised mixture of six clusters. Move the noise level and watch where the arrows point.

Figure 2 · the score field
σ = 1.00
Arrows show the score ∇log⁡pσ(x)\nabla\log p_\sigma(\mathbf{x}) of six data clusters noised to level σ\sigma. At small σ\sigma each arrow points at the nearest cluster; at large σ\sigma the clusters merge into one broad bump and every arrow points toward the overall center. Generation starts at large σ\sigma and ends at small.

Generation therefore needs ∇xlog⁡pt(x)\nabla_{\mathbf{x}}\log p_t(\mathbf{x}) at every tt, sharp near the end of generation and smooth at the start, estimated from samples of the data alone.

Learning the score by denoising

Nobody has ground-truth score vectors to regress on, since ptp_t is unknown. Score matching (Hyvärinen, 2005) gives an objective whose minimizer is the score anyway, and its denoising form (Vincent, 2011) needs nothing but noised training pairs. The paper trains a time-conditioned network sθ(x,t)\mathbf{s}_{\boldsymbol\theta}(\mathbf{x}, t) with

θ∗=arg⁡min⁡θ Et{λ(t) Ex(0) Ex(t)∣x(0)[ ∥sθ(x(t),t)−∇x(t)log⁡p0t(x(t)∣x(0))∥22 ]}\boldsymbol{\theta}^* = \arg\min_{\boldsymbol{\theta}}\,\mathbb{E}_t\Big\{\lambda(t)\,\mathbb{E}_{\mathbf{x}(0)}\,\mathbb{E}_{\mathbf{x}(t)\mid\mathbf{x}(0)}\big[\,\big\lVert \mathbf{s}_{\boldsymbol{\theta}}(\mathbf{x}(t), t) - \nabla_{\mathbf{x}(t)}\log p_{0t}(\mathbf{x}(t)\mid\mathbf{x}(0))\big\rVert_2^2\,\big]\Big\}θ∗=arg⁡min⁡θ Et{λ(t) Ex(0) Ex(t)∣x(0)[ ∥sθ(x(t),t)−∇x(t)log⁡p0t(x(t)∣x(0))∥22 ]}\begin{aligned} &\boldsymbol{\theta}^* = \arg\min_{\boldsymbol{\theta}}\,\mathbb{E}_t\Big\{\lambda(t)\,\mathbb{E}_{\mathbf{x}(0)}\,\mathbb{E}_{\mathbf{x}(t)\mid\mathbf{x}(0)}\big[\,\big\lVert \mathbf{s}_{\boldsymbol{\theta}}(\mathbf{x}(t), t) \\ &\qquad - \nabla_{\mathbf{x}(t)}\log p_{0t}(\mathbf{x}(t)\mid\mathbf{x}(0))\big\rVert_2^2\,\big]\Big\} \end{aligned}
(7)

Read from the inside out: draw a clean sample x(0)\mathbf{x}(0) from the data, draw tt uniformly from [0,T][0, T], noise the sample with the forward process to get x(t)\mathbf{x}(t), and regress the network's output onto the score of the transition kernel p0t(x(t)∣x(0))p_{0t}(\mathbf{x}(t) \mid \mathbf{x}(0)), the distribution of x(t)\mathbf{x}(t) given that particular starting point. λ(t)>0\lambda(t) > 0 weights the noise levels.

The target is not the score of ptp_t, so why does the minimizer equal it? The noised distribution is a mixture, one kernel per data point, averaged:

pt(x)=∫p0(x0) p0t(x∣x0) dx0.p_t(\mathbf{x}) = \int p_0(\mathbf{x}_0)\,p_{0t}(\mathbf{x} \mid \mathbf{x}_0)\,d\mathbf{x}_0 .

The gradient of the log of a mixture is the average of the components' gradients, each weighted by the posterior probability that this component produced x\mathbf{x}:

∇xlog⁡pt(x)=E[∇xlog⁡p0t(x∣x0) ∣ x(t)=x].\nabla_{\mathbf{x}}\log p_t(\mathbf{x}) = \mathbb{E}\big[\nabla_{\mathbf{x}}\log p_{0t}(\mathbf{x} \mid \mathbf{x}_0) \,\big|\, \mathbf{x}(t) = \mathbf{x}\big].

So each training target is a noisy sample whose conditional mean, at that x(t)\mathbf{x}(t), is the true score, and the squared-error regression converges to that conditional mean. Vincent showed the per-pair objective and the objective with the true score differ by a constant that does not depend on θ\boldsymbol\theta, so they have the same gradients. At the optimum the network equals ∇xlog⁡pt(x)\nabla_{\mathbf{x}}\log p_t(\mathbf{x}) for almost all x\mathbf{x} and tt. For Gaussian kernels the identity has a geometric reading (Tweedie's formula): σ2\sigma^2 times the score of the noised data points from x\mathbf{x} to E[x0∣x]\mathbb{E}[\mathbf{x}_0 \mid \mathbf{x}], the average clean point that could have produced it.

Figure 3 shows the averaging with seven clean points. Drag the white probe to a noisy location, then press draw a training pair a few times to see single targets.

Figure 3 · the denoising target and the score
σ = 0.59
Seven clean points on the heat map of their noised density. Arrows are drawn multiplied by σ2\sigma^2, so a training pair's target ends exactly on its clean point and the score ends on the posterior-weighted average of all seven. Rings and dashed lines brighten with the posterior weight. At small σ\sigma one point takes almost all the weight and target and score agree; at large σ\sigma the weights even out and the score points at the center of the data while each single target still points at its own point.

The target is cheap whenever the drift f\mathbf{f} is affine in x\mathbf{x}, because the kernel is then Gaussian with a closed-form mean μt(x0)\boldsymbol\mu_t(\mathbf{x}_0) and standard deviation σt\sigma_t. The score of N(μ,σ2I)\mathcal{N}(\boldsymbol\mu, \sigma^2 I) at x\mathbf{x} is (μ−x)/σ2(\boldsymbol\mu - \mathbf{x})/\sigma^2, with the variance σ2\sigma^2 in the denominator. Writing x(t)=μ+σz\mathbf{x}(t) = \boldsymbol\mu + \sigma\mathbf{z}, the target is −z/σ-\mathbf{z}/\sigma, the drawn noise divided by the noise level.

The weighting fixes a scale problem. The expected squared norm of that target is d/σ2d/\sigma^2 for dd dimensions. On CIFAR-10 (d=3072d = 3072) with noise levels from 0.01 to 50, that runs from 3.07×1073.07 \times 10^7 down to 1.23, a spread of seven orders of magnitude, and an unweighted loss would spend almost all its gradient on the smallest noise levels. The paper sets λ(t)∝1/E∥∇log⁡p0t∥2\lambda(t) \propto 1/\mathbb{E}\lVert\nabla\log p_{0t}\rVert^2, which here is λ=σ2\lambda = \sigma^2, and the loss becomes ∥σ sθ+z∥2\lVert \sigma\,\mathbf{s}_{\boldsymbol\theta} + \mathbf{z}\rVert^2, a quantity of order dd at every noise level. The official code computes exactly that:

# one training step of the score network (VE SDE, CIFAR-10 settings)
x0 = next(loader)                     # clean images, shape (128, 3, 32, 32)
t = rand(128) * (1 - 1e-5) + 1e-5     # one time per image, t ~ U(1e-5, 1)
mean, std = marginal_prob(x0, t)      # VE: mean = x0, std ~ 0.01 * 5000**t
z = randn_like(x0)
x_t = mean + std[:, None, None, None] * z        # noised images, same shape
s = score_net(x_t, t)                 # predicted score, shape (128, 3, 32, 32)
# target score of the kernel is -z / std; lambda(t) = std**2 turns
# lambda * |s - (-z/std)|^2 into |s * std + z|^2
loss = ((s * std[:, None, None, None] + z) ** 2).mean()
loss.backward()                       # gradient reaches score_net's weights only

Training has no sampling loop, no adversary and no chain to unroll: one noise draw, one forward pass and one squared error per image. The objective works for any SDE with an affine drift, and the paper notes that sliced score matching (Song et al., 2019) can replace the denoising target when the kernel has no closed form.

VE, VP and sub-VP: SMLD and DDPM as two SDEs

Choosing f\mathbf{f} and gg chooses a model family. The paper takes the noising chains of SMLD and DDPM, lets their number of steps NN go to infinity, and reads off the SDE each one becomes.

SMLD noises a clean sample by adding independent Gaussian noise of growing size and never rescales the signal:

xi=xi−1+σi2−σi−12  zi−1,i=1,…,N\mathbf{x}_i = \mathbf{x}_{i-1} + \sqrt{\sigma_i^2 - \sigma_{i-1}^2}\;\mathbf{z}_{i-1}, \qquad i = 1, \dots, N
(8)

with σ0=0\sigma_0 = 0, so after ii steps the total added variance is σi2\sigma_i^2. As N→∞N \to \infty the chain becomes an SDE with no drift:

dx=d [σ2(t)]dt  dwd\mathbf{x} = \sqrt{\frac{d\,[\sigma^2(t)]}{dt}}\;d\mathbf{w}
(9)

Nothing pulls the signal back, so the variance grows with σ(t)\sigma(t) without bound, and the paper names (9) the Variance Exploding (VE) SDE. In the experiments σ(t)=σmin⁡(σmax⁡/σmin⁡)t\sigma(t) = \sigma_{\min}(\sigma_{\max}/\sigma_{\min})^t with σmin⁡=0.01\sigma_{\min} = 0.01 and, for CIFAR-10, σmax⁡=50\sigma_{\max} = 50. The kernel has mean x0\mathbf{x}_0 and variance σ(t)2\sigma(t)^2 per coordinate (Eq. 31), and the prior is N(0,σmax⁡2I)\mathcal{N}(0, \sigma_{\max}^2 I), wide enough that pixel values in [0,1][0, 1] are lost in it. Because σ(t)\sigma(t) tends to σmin⁡=0.01\sigma_{\min} = 0.01 as t→0t \to 0 while the chain starts from σ0=0\sigma_0 = 0, the VE SDE is undefined at t=0t = 0, so the paper solves it on [10−5,1][10^{-5}, 1]; that is the 1e-5 in the training code above.

DDPM shrinks the signal at every step as it adds noise:

xi=1−βi  xi−1+βi  zi−1,i=1,…,N\mathbf{x}_i = \sqrt{1 - \beta_i}\;\mathbf{x}_{i-1} + \sqrt{\beta_i}\;\mathbf{z}_{i-1}, \qquad i = 1, \dots, N
(10)

Scaling by 1−βi\sqrt{1 - \beta_i} multiplies the variance by 1−βi1 - \beta_i, the fresh noise adds βi\beta_i, and the two sum to 1, so unit-variance data keeps unit variance at every step. Write βi=βˉi/N\beta_i = \bar\beta_i / N, where βˉi=β(i/N)\bar\beta_i = \beta(i/N) samples a continuous schedule β(t)\beta(t) on [0,1][0, 1]; as N→∞N \to \infty the limit is

dx=−12β(t) x dt+β(t)  dwd\mathbf{x} = -\tfrac{1}{2}\beta(t)\,\mathbf{x}\,dt + \sqrt{\beta(t)}\;d\mathbf{w}
(11)

the Variance Preserving (VP) SDE. The drift −12β(t)x-\tfrac{1}{2}\beta(t)\mathbf{x} pulls toward the origin at the rate that offsets the injected noise. The paper uses βˉmin⁡=0.1\bar\beta_{\min} = 0.1 and βˉmax⁡=20\bar\beta_{\max} = 20 as the endpoints of a linear β(t)\beta(t), which is the β(t)=0.1+19.9 t\beta(t) = 0.1 + 19.9\,t used above. With N=1000N = 1000 these give DDPM's own schedule of β1=10−4\beta_1 = 10^{-4} rising linearly to βN=0.02\beta_N = 0.02. Its kernel has mean x0e−12∫0tβ\mathbf{x}_0 e^{-\frac{1}{2}\int_0^t \beta} and variance 1−e−∫0tβ1 - e^{-\int_0^t \beta}, and its prior is N(0,I)\mathcal{N}(0, I). The names VE and VP are this paper's; neither SMLD nor DDPM used them.

The paper also proposes a new SDE, the sub-VP SDE. It keeps the VP drift and scales down the noise term:

dx=−12β(t) x dt+β(t)(1−e−2∫0tβ(s) ds)  dwd\mathbf{x} = -\tfrac{1}{2}\beta(t)\,\mathbf{x}\,dt + \sqrt{\beta(t)\big(1 - e^{-2\int_0^t \beta(s)\,ds}\big)}\;d\mathbf{w}
(12)

The extra factor is 0 at t=0t = 0 and approaches 1 as ∫β\int\beta grows, so sub-VP has the same mean as VP, injects less noise early, and reaches the same N(0,I)\mathcal{N}(0, I) prior. The paper proves its variance is at most the VP variance at every time. For unit-variance data, writing u=e−∫0tβu = e^{-\int_0^t\beta}, Appendix B's formula gives

Σsub-VP(t)=1−u+u2,\Sigma_{\text{sub-VP}}(t) = 1 - u + u^2,

which is 1 at both ends (u=1u = 1 and u→0u \to 0) and has its minimum, 0.75, at u=1/2u = 1/2. That dip is our derivation from the paper's formula, not a number the paper prints. In experiments sub-VP always gave higher likelihoods than VP; the paper reports this result without proposing a mechanism for it. Scrub Figure 4 to compare the three variances.

Figure 4 · VE, VP and sub-VP
t = 0.50
Variance of noised unit-variance data over time, log scale. VE (the limit of SMLD) grows from 1 to about 2500 as σ(t)\sigma(t) goes from 0.01 to 50. VP (the limit of DDPM) stays at 1. sub-VP dips to 0.75 mid-way and returns to 1, below VP at every time. Scrub tt to read the three values.

With one codebase, swapping the forward SDE changes only the kernel's mean and standard deviation in training and the drift and diffusion in sampling. The paper uses that to compare SMLD-style and DDPM-style models with the same samplers and to find better architectures for each (NCSN++ for VE, DDPM++ for VP and sub-VP), which is how SMLD, previously well behind DDPM on FID, catches up with it.

The probability-flow ODE

The reverse SDE adds fresh noise at every step, so two runs from the same prior sample end at different images. The paper shows that every diffusion of the form (5) has a deterministic counterpart, an ordinary differential equation, whose solutions have the same marginal densities ptp_t at every time:

dx=[f(x,t)−12g(t)2 ∇xlog⁡pt(x)] dtd\mathbf{x} = \big[\mathbf{f}(\mathbf{x}, t) - \tfrac{1}{2}g(t)^2\,\nabla_{\mathbf{x}}\log p_t(\mathbf{x})\big]\,dt
(13)

The paper calls it the probability flow ODE. Compared with the reverse SDE (6), the noise term is gone and the score's coefficient is exactly half, 12g(t)2\tfrac{1}{2}g(t)^2 instead of g(t)2g(t)^2. Individual trajectories differ from the SDE's; the distribution of particles at each tt is the same.

Both coefficients come out of the Fokker-Planck equation, which describes how the density ptp_t of an SDE evolves. For (5) with scalar gg, it reads

∂pt∂t=−∇⋅(f pt)+12g(t)2 ∇2pt.\frac{\partial p_t}{\partial t} = -\nabla\cdot\big(\mathbf{f}\,p_t\big) + \tfrac{1}{2}g(t)^2\,\nabla^2 p_t .

The first term moves probability along the drift; the second spreads it. The 12\tfrac{1}{2} comes from the second-order Taylor term in Itô's formula, applied to a Wiener increment whose variance is dtdt. Since ∇2p=∇⋅(p ∇log⁡p)\nabla^2 p = \nabla\cdot(p\,\nabla\log p), the spreading term can be rewritten as a flow:

∂pt∂t=−∇⋅([f−12g2 ∇log⁡pt] pt).\frac{\partial p_t}{\partial t} = -\nabla\cdot\Big(\big[\mathbf{f} - \tfrac{1}{2}g^2\,\nabla\log p_t\big]\,p_t\Big).

This is the continuity equation for particles moving deterministically with velocity f−12g2∇log⁡pt\mathbf{f} - \tfrac{1}{2}g^2\nabla\log p_t, which is the ODE (13); Appendix D.1 of the paper does this calculation in general form, an idea it credits to Maoutsa et al. (2020). The reverse SDE keeps a noise term, and in reversed time that noise spreads the density by +12g2∇2pt+\tfrac{1}{2}g^2\nabla^2 p_t while the reversed process has to contract it by the same amount. The drift has to cancel the noise's spreading and also supply the contraction, two halves, so its score coefficient is the full g2g^2 of Anderson's result. The argument checks that (6) and (13) agree with each other; Anderson (1982) has the rigorous proof of (6).

Figure 5 runs both on a one-dimensional mixture, where ptp_t is known exactly. The upper panel integrates the reverse SDE, the lower one the ODE, from the same 3000 prior draws with 400 steps. Play it, then switch to the swapped coefficients.

Figure 5 · same marginals, different paths
t = 0.40
Paths from the prior (t=1t = 1, left) to the data (t=0t = 0, right) under the VP SDE, with a histogram of 3000 particles at the current time against the exact ptp_t. With the paper's coefficients the SDE paths are jagged, the ODE paths smooth, and both histograms track ptp_t (gap 0.03 to 0.06, the size of sampling noise). Swapped, the SDE with 12g2\tfrac{1}{2}g^2 ends far too wide and the ODE with g2g^2 collapses toward a point.

With a learned score in place of the true one, (13) is a neural ODE (Chen et al., 2018): a deterministic, invertible map from data to prior whose velocity is a network, and four properties follow from that.

First, the ODE gives exact likelihoods. As the flow carries a small volume of probability, the density inside changes by the divergence of the velocity f~=f−12g2sθ\tilde{\mathbf{f}} = \mathbf{f} - \tfrac{1}{2}g^2\mathbf{s}_{\boldsymbol\theta} (the instantaneous change-of-variables formula):

log⁡p0(x(0))=log⁡pT(x(T))+∫0T∇⋅f~θ(x(t),t) dt\log p_0(\mathbf{x}(0)) = \log p_T(\mathbf{x}(T)) + \int_0^T \nabla\cdot\tilde{\mathbf{f}}_{\boldsymbol\theta}(\mathbf{x}(t), t)\,dt
(39)

Take 1D data N(0,1)\mathcal{N}(0, 1) under the VE SDE. With f=0\mathbf{f} = 0 and g2 dt=d[σ2]g^2\,dt = d[\sigma^2], the ODE (13) becomes, after the 12\tfrac{1}{2} cancels against the 2 in d[σ2]=2σ dσd[\sigma^2] = 2\sigma\,d\sigma,

dxdσ=−σ ∇xlog⁡pσ(x),\frac{d\mathbf{x}}{d\sigma} = -\sigma\,\nabla_{\mathbf{x}}\log p_\sigma(\mathbf{x}),

a form the paper does not print but which follows from (13) directly. DDIM's deterministic sampler is an Euler step of this equation in rescaled coordinates, and Karras et al. (2022, EDM) write their ODE in this form. Here the score is −x/(1+σ2)-x/(1 + \sigma^2), so dx/dσ=σx/(1+σ2)dx/d\sigma = \sigma x/(1 + \sigma^2) and xx is proportional to 1+σ2\sqrt{1 + \sigma^2}. The data point x(0)=1x(0) = 1 flows to x(50)=2501=50.01x(50) = \sqrt{2501} = 50.01, one standard deviation of the data to one standard deviation of the prior. The divergence σ/(1+σ2)\sigma/(1 + \sigma^2) integrates to 12ln⁡2501=3.912\tfrac{1}{2}\ln 2501 = 3.912 nats. The prior density at 50.01 is log⁡N(50.01;0,2501)=−5.331\log \mathcal{N}(50.01; 0, 2501) = -5.331, and adding 3.912 gives −1.419-1.419, which is log⁡N(1;0,1)\log \mathcal{N}(1; 0, 1) exactly. A VAE's ELBO only bounds this number from below. The divergence of a real network over 3072 dimensions is too expensive to compute exactly, so the paper estimates it with the unbiased Skilling-Hutchinson trace estimator and averages runs to reach the needed accuracy: exact in the sense of no bound, but computed with random probes.

Second, the encoding is unique. The forward SDE has no trainable parameters, so for a given data distribution, integrating (13) forward maps each data point to one latent in the prior, the same for any model that learns the score accurately. Third, mixing two latents and decoding gives images in between, the interpolation familiar from flows and VAEs. Fourth, a black-box ODE solver (the paper uses RK45) picks its own step sizes and cuts score-network evaluations by more than 90% without visible loss in sample quality.

Predictor-corrector sampling

A discretized reverse SDE carries error: after each finite step the samples are distributed slightly differently from ptp_t, and the errors accumulate. Smaller steps reduce the error at a proportional cost. The paper adds a different kind of step that uses the score to pull the samples back toward ptp_t at a fixed noise level.

That step is Langevin dynamics, a Markov chain Monte Carlo method that needs only the score of its target. In this paper's convention, one Langevin step at level tt is

x←x+ϵ sθ(x,t)+2ϵ  z,z∼N(0,I).\mathbf{x} \leftarrow \mathbf{x} + \epsilon\,\mathbf{s}_{\boldsymbol\theta}(\mathbf{x}, t) + \sqrt{2\epsilon}\;\mathbf{z}, \qquad \mathbf{z} \sim \mathcal{N}(0, I).

The score term alone is gradient ascent on log⁡pt\log p_t and would pull every sample onto a mode. The noise term spreads them out. With step ϵ\epsilon on the drift and 2ϵ\sqrt{2\epsilon} on the noise, the two balance so that, as ϵ→0\epsilon \to 0 and the number of steps grows, the samples are distributed as ptp_t (under regularity conditions). The 2ϵ\sqrt{2\epsilon} pairing is specific to this convention; the NCSN paper writes the same dynamics as α2∇log⁡p+α z\tfrac{\alpha}{2}\nabla\log p + \sqrt{\alpha}\,\mathbf{z}, so its α\alpha equals 2ϵ2\epsilon here.

A finite step does not land exactly on ptp_t. For a Gaussian mode of variance s2s^2, the update is

x←(1−ϵs2)x+2ϵ z,x \leftarrow \Big(1 - \frac{\epsilon}{s^2}\Big)x + \sqrt{2\epsilon}\,z,

and the variance vv it settles at solves

v=(1−ϵs2)2v+2ϵ⇒v=s21−ϵ/(2s2),v = \Big(1 - \frac{\epsilon}{s^2}\Big)^2 v + 2\epsilon \quad\Rightarrow\quad v = \frac{s^2}{1 - \epsilon/(2s^2)},

larger than s2s^2 for any ϵ>0\epsilon > 0, and unbounded once ϵ≥2s2\epsilon \ge 2s^2. Small steps have small bias but need many iterations to move the samples; large steps move fast and settle on the wrong spread. Figure 6 starts from a predictor output packed too tightly around each mode. Raise MM, the number of Langevin steps, then try the smallest and largest ϵ\epsilon.

Figure 6 · the Langevin corrector
M = 0
ε = 0.015
Samples (4000) against the exact ptp_t at one noise level. At M=0M = 0 they sit too close to the modes (gap 0.41). At ϵ=0.015\epsilon = 0.015, ten corrector steps bring the gap to about 0.05. At ϵ=0.0005\epsilon = 0.0005 sixty steps are not enough. At ϵ=0.09\epsilon = 0.09, near the narrow mode's 2s2=0.0972s^2 = 0.097, the samples settle too wide and leak out of the narrow mode. At ϵ=0.12\epsilon = 0.12, past it, the narrow mode empties into the wide one within about ten steps.

A predictor-corrector (PC) sampler alternates the two. The predictor is any numerical solver of the reverse SDE and takes one step down in time; the corrector runs MM Langevin steps at the new time with the same score network. The earlier methods are special cases: SMLD is an identity predictor (no time step from the SDE) with annealed Langevin as the corrector, and DDPM is its ancestral sampler as the predictor with an identity corrector. DDPM's ancestral sampling is one particular discretization of the reverse VP SDE; the paper's reverse-diffusion predictor discretizes it the way the forward SDE is discretized and does slightly better.

The corrector's step size comes from a signal-to-noise ratio rr (0.16 for VE on CIFAR-10), with norms averaged over the batch:

ϵ=2 (r ∥z∥/∥sθ∥)2.\epsilon = 2\,\big(r\,\lVert\mathbf{z}\rVert / \lVert\mathbf{s}_{\boldsymbol\theta}\rVert\big)^2 .

If the network's score has the size of the kernel score, ∥s∥≈d/σ\lVert\mathbf{s}\rVert \approx \sqrt{d}/\sigma, while ∥z∥≈d\lVert\mathbf{z}\rVert \approx \sqrt{d}, so ϵ≈2r2σ2\epsilon \approx 2r^2\sigma^2, which is 0.0512 σ20.0512\,\sigma^2: the step scales with the noise variance, which is how NCSN set its Langevin steps. The full loop for VE, as in the paper's Algorithm 2:

# PC sampling for the VE SDE (Algorithm 2), N = 1000, M = 1
x = randn(shape) * sigma[N]                    # prior N(0, 50^2 I)
for i in range(N - 1, -1, -1):                 # noise level runs down to sigma[0]
    # predictor: one reverse-diffusion step of Eq. (6)
    d = sigma[i + 1] ** 2 - sigma[i] ** 2
    x = x + d * score_net(x, sigma[i + 1]) + sqrt(d) * randn_like(x)
    # corrector: M Langevin steps at the new level, step size from r = 0.16
    for _ in range(M):
        g = score_net(x, sigma[i])
        z = randn_like(x)
        eps = 2 * (r * norm(z) / norm(g)) ** 2
        x = x + eps * g + sqrt(2 * eps) * z
return x

Table 1 of the paper compares samplers on CIFAR-10 at 1000 and 2000 score evaluations. For the VE model with the reverse-diffusion predictor, 1000 predictor steps give FID 4.79, 2000 give 4.74, 2000 corrector-only steps give 20.43, and PC1000 (1000 of each, 2000 evaluations) gives 3.60. For the VP model the same four are 3.21, 3.19, 19.06 and 3.18. Adding one corrector step per predictor step always improved on P1000 and was typically better than doubling the predictor steps; corrector-only was worst in both columns. With the probability-flow ODE as the predictor, the VE FID drops from 15.41 to 3.51 when correctors are added.

Conditional generation: class labels, inpainting, colorization

To sample from p(x∣y)p(\mathbf{x} \mid \mathbf{y}) for some condition y\mathbf{y}, the reverse SDE needs the conditional score. Bayes' rule gives

log⁡pt(x∣y)=log⁡pt(x)+log⁡pt(y∣x)−log⁡pt(y),\log p_t(\mathbf{x} \mid \mathbf{y}) = \log p_t(\mathbf{x}) + \log p_t(\mathbf{y} \mid \mathbf{x}) - \log p_t(\mathbf{y}),

and the last term has no x\mathbf{x} in it, so its gradient vanishes. The conditional reverse SDE is

dx={f(x,t)−g(t)2[∇xlog⁡pt(x)+∇xlog⁡pt(y∣x)]} dt+g(t) dwˉd\mathbf{x} = \big\{\mathbf{f}(\mathbf{x}, t) - g(t)^2\big[\nabla_{\mathbf{x}}\log p_t(\mathbf{x}) + \nabla_{\mathbf{x}}\log p_t(\mathbf{y}\mid\mathbf{x})\big]\big\}\,dt + g(t)\,d\bar{\mathbf{w}}dx={f(x,t)−g(t)2[∇xlog⁡pt(x)+∇xlog⁡pt(y∣x)]} dt+g(t) dwˉ\begin{aligned} d\mathbf{x} = \big\{\mathbf{f}(\mathbf{x}, t) - g(t)^2\big[&\nabla_{\mathbf{x}}\log p_t(\mathbf{x}) \\ &+ \nabla_{\mathbf{x}}\log p_t(\mathbf{y}\mid\mathbf{x})\big]\big\}\,dt + g(t)\,d\bar{\mathbf{w}} \end{aligned}
(14)

The first gradient is the unconditional score already trained. The second is the gradient of how likely the condition is given the noisy sample, and both are multiplied by the same g(t)2g(t)^2. Equation (14) has no extra scale factor on the second term; the guidance scale came later, with Dhariwal and Nichol (2021).

Where the second gradient comes from depends on the task. For class labels, pt(y∣x)p_t(\mathbf{y} \mid \mathbf{x}) is a classifier that has to work on noised inputs, so the paper trains a separate time-dependent classifier on samples from the forward SDE, with a cross-entropy loss mixed over time steps. The score model is not retrained. For inpainting (imputation) and colorization the paper needs no auxiliary model: the known pixels are replaced at each step by a copy of their true values noised with the forward kernel to the current time, and the unconditional score of the combined image moves the unknown pixels along the reverse SDE. Appendix I.2 derives this as an approximation to the conditional score. Colorization is the same imputation in an orthogonally transformed color space. The demonstrations are class-conditional samples on 32×32 CIFAR-10 and inpainting and colorization on 256×256 LSUN.

In Figure 7, pick a class. The unconditional score sends particles to all three clusters; selecting a class adds the gradient of a classifier's log-probability to the same score.

Figure 7 · conditional generation
With all classes the guidance term is zero and the unconditional score spreads particles over the three clusters. Choosing a class adds ∇log⁡pt(y∣x)\nabla\log p_t(y \mid \mathbf{x}) to that score and every particle ends at the chosen cluster. The score is the same in all four settings; only the added classifier gradient changes.

Every class-conditional sampling step costs a backward pass through the classifier, to get ∇xlog⁡pt(y∣x)\nabla_{\mathbf{x}}\log p_t(\mathbf{y} \mid \mathbf{x}), on top of the score evaluation, and the classifier has to be accurate on images at every noise level. Classifier-free guidance later removed the separate classifier by training one network with and without the condition.

Results and limits

The best sample quality came from NCSN++ cont. (deep, VE): on unconditional CIFAR-10, an Inception score of 9.89 and an FID of 2.20, with PC samplers. The Inception score rewards samples that a classifier labels confidently and that cover many classes (higher is better). The path there was incremental: NCSN++ with PC sampling reached 2.45, training it with the continuous objective (7) brought 2.38, and doubling the residual blocks per resolution ("deep") brought 2.20.

CIFAR-10 modelFID ↓IS ↑bits/dim ↓
NCSN (2019)25.328.87–
NCSNv2 (2020)10.878.40–
DDPM (2020)3.179.46–
StyleGAN2-ADA, conditional2.4210.14–
DDPM++ (VP), PC2.789.64–
NCSN++ (VE), PC2.459.73–
NCSN++ cont. (deep, VE)2.209.89–
DDPM++ cont. (deep, sub-VP), ODE2.92–2.99

An FID of 2.20 without labels beat the best class-conditional model then reported, StyleGAN2-ADA at 2.42, on FID. The conditional GAN's Inception score of 10.14 is still higher than 9.89; unconditional StyleGAN2-ADA scores 2.92 and 9.83.

The best likelihood came from a different model, DDPM++ cont. (deep, sub-VP): 2.99 bits per dimension, computed with the probability-flow ODE, with an ODE-sampled FID of 2.92. Bits per dimension is the average number of bits the model needs to encode one pixel channel of a test image, so lower means test images are more probable under the model. The paper calls 2.99 a record for uniformly dequantized CIFAR-10, a comparison that leaves out models evaluated with variational dequantization or on discrete data, and it was reached without maximum-likelihood training. The two sets of numbers also use different checkpoints: Table 3 (2.20 and 9.89) reports the checkpoint with the lowest FID during training, sampled with PC; Table 2 (2.99 and 2.92) reports the last checkpoint, sampled with a black-box ODE solver. VE SDEs gave better samples and worse likelihoods than VP and sub-VP, so the choice of SDE depends on which number matters.

At higher resolution, an NCSN++-style model trained with (7) produced 1024×1024 CelebA-HQ faces, sampled with 2000 PC steps, the first high-fidelity images at that size from a score-based model. The authors note the samples are not perfect, with visible flaws in facial symmetry.

The paper names sampling speed as its main limitation: PC1000 calls the score network 2000 times per sample, and the 1024×1024 sampler runs 2000 predictor steps plus a corrector step after each, where a GAN generates with one forward pass. The paper leaves combining stable score-model training with fast sampling to future work, and lists the samplers' many hyperparameters (step counts, signal-to-noise ratios) as a second limitation. The follow-ups split along those lines: deterministic samplers built on the ODE, such as DDIM, cut the step count; Flow Matching trains an ODE's velocity field directly; and Latent Diffusion runs the same machinery in a compressed latent space. DiffusionBlocks reads the blocks of an ordinary residual network as steps of a reverse diffusion.

Provenance Verified against primary literatureHow we verify
Anderson (1982)The reverse of a diffusion is a diffusion; its drift gains the term -g(t)^2 times the score of p_t. Source of Eq. (6).
Hyvarinen (2005); Vincent (2011)Score matching, which needs no normalizing constant, and its denoising form, which makes Eq. (7) a plain regression.
Song & Ermon (2019), NCSN / SMLDNoise conditional score network plus annealed Langevin dynamics. Its noising chain, Eq. (8), discretizes the VE SDE.
Ho et al. (2020), DDPMIts noising chain, Eq. (10), discretizes the VP SDE, and its ancestral sampler is one discretization of the reverse VP SDE (Sec. 4.1, App. E).
Chen et al. (2018); Maoutsa et al. (2020)The probability-flow ODE with a learned score is a neural ODE; the deterministic-flow idea is credited to Maoutsa et al. (App. D.1).
Official code, score_sde_pytorchlosses.py lines 84-92: t ~ U(1e-5, 1), x_t = mean + std z, loss |s std + z|^2 when likelihood_weighting is False. CIFAR-10 configs: sigma 0.01 to 50, beta 0.1 to 20, 1000 scales, snr 0.16, one corrector step, batch 128.
correctionTwo coefficients that are easy to merge stay distinct here. The reverse-time SDE, Eq. (6), multiplies the score by the full g(t)^2; the probability-flow ODE, Eq. (13), multiplies it by exactly half, g(t)^2/2. A reverse SDE written with the 1/2 imports it from the diffusion term of the forward Fokker-Planck equation, and it samples the wrong distribution (Figure 5 shows both swaps failing). The earlier version of this page also overstated two scope points, now corrected: ODE likelihoods are exact in the sense of no bound, but the divergence is estimated with the unbiased Skilling-Hutchinson trace estimator (App. D.2), so the computation is not sampling-free; and class-conditional generation needs a separately trained, noise-aware classifier, while the score model itself is not retrained.

Questions you might still have

?

If the reverse SDE and the probability-flow ODE produce the same distributions, why keep both?
They share the marginal densities at every time, not the paths. The SDE adds fresh noise at each step, which can wash out earlier errors; in the paper's Table 1 the reverse-diffusion SDE predictor gives FID 4.79 for the VE model at 1000 steps where the ODE predictor gives 15.41. The ODE is deterministic and invertible, which gives exact likelihoods, a unique latent per image, interpolation, and adaptive solvers that cut network evaluations by over 90%.

?

Why does the ODE have half the score coefficient of the SDE?
In the Fokker-Planck equation the noise spreads the density by one half g squared times the Laplacian of p. The ODE replaces that spreading with a deterministic velocity, which costs one half g squared times the score. The reverse SDE keeps its noise, so its drift has to cancel the noise's spreading and also undo the forward spreading: two halves, the full g squared. Writing one half in the reverse SDE samples a distribution that is too wide.

?

Is this just DDPM in continuous time?
DDPM is one point in it: its noising chain is a discretization of the VP SDE and its ancestral sampler is one discretization of the reverse VP SDE. The paper adds the VE SDE (the limit of SMLD/NCSN), the new sub-VP SDE, the probability-flow ODE with exact likelihoods, predictor-corrector sampling, and conditional generation from an unconditional score model.

?

How does conditional generation work without retraining?
The conditional reverse SDE adds the gradient of log p_t(y | x) to the unconditional score. For class labels that gradient comes from a separately trained classifier that works on noised images, so something is trained, but the score model is reused as is. Inpainting and colorization need no extra model: at each step the known pixels are replaced by their true values noised to the current time, and the unconditional score moves the rest, an approximation the paper derives in its Appendix I.2.

?

What does the score network actually predict for an image?
A tensor the same shape as the input, 3 by 32 by 32 for CIFAR-10, at a given noise level. Trained with the denoising loss, it approximates minus the added noise divided by the noise level, averaged over all clean images that could have produced this noisy one. Multiplied by sigma squared it points from the noisy image to that average clean image.

?

How slow is sampling?
The CIFAR-10 PC sampler takes 1000 predictor steps and one Langevin corrector step after each, 2000 score-network evaluations per batch of samples. The 1024 by 1024 CelebA-HQ sampler uses 2000 predictor steps. An adaptive ODE solver reduces the count by more than 90%, and later samplers such as DDIM reduce it further.

Footnotes & further reading

  1. The paper: Song, Sohl-Dickstein, Kingma, Kumar, Ermon, Poole, Score-Based Generative Modeling through Stochastic Differential Equations (ICLR 2021). Code (JAX), code (PyTorch).
  2. The time-reversal of a diffusion: Anderson, Reverse-time diffusion equation models (Stochastic Processes and their Applications 12(3), 1982).
  3. The two ancestors: Song & Ermon, Generative Modeling by Estimating Gradients of the Data Distribution (NCSN / SMLD, 2019), and Ho et al., Denoising Diffusion Probabilistic Models (DDPM, 2020), which builds on Sohl-Dickstein et al., Deep Unsupervised Learning using Nonequilibrium Thermodynamics (2015).
  4. Score matching and its denoising form: Hyvärinen, Estimation of Non-Normalized Statistical Models by Score Matching (2005), and Vincent, A Connection Between Score Matching and Denoising Autoencoders (2011).
  5. The probability-flow ODE as a neural ODE: Chen et al., Neural Ordinary Differential Equations (2018); the deterministic-flow construction for Fokker-Planck equations in Maoutsa et al. (2020).
  6. Guidance after this paper: Dhariwal & Nichol, Diffusion Models Beat GANs on Image Synthesis (2021), which adds a scale to the classifier gradient, and Ho & Salimans, Classifier-Free Diffusion Guidance (2022).
  7. What the framework grew into: DDIM (2020); Latent Diffusion (Stable Diffusion); the VE-style parameterization and samplers of Karras et al., EDM (2022); Flow Matching (2022); and the residual-network reading in DiffusionBlocks.