Search

Search papers and lessons

Score-Based Generative Modeling through Stochastic Differential Equations

Yang Song, Jascha Sohl-Dickstein, Diederik P. Kingma et al. · 2020

Song et al. unify score-based models and diffusion models as one SDE framework, sampling by reversing the noising SDE, and report record CIFAR-10 sample quality and likelihoods.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Stochastic processes
  • Generative models
  • Linear algebra
  • Discrete math
  • Sign in to see your progress

TL;DR

Song and colleagues show that the two main ways of generating images by adding and removing noise, score matching with Langevin dynamics and denoising diffusion, are discrete versions of one idea: a continuous stochastic differential equation (SDE) that slowly turns data into noise. Generation runs that SDE in reverse, using a neural network that estimates the score, meaning the gradient of the log density at each noise level. The same framework gives new samplers, exact likelihoods, and conditional generation, and it reached record CIFAR-10 sample quality and likelihood for its time.

Where you'll see it: the theory behind modern image, audio and video diffusion models. Its SDE and probability-flow ODE views are the standard way later work describes diffusion models.

The problem

By 2020 there were two strong families of generative models built on noise. Score matching with Langevin dynamics (SMLD) trained a score network at a few fixed noise levels and sampled by running Langevin steps at each level, from the most noisy to the least. Denoising diffusion probabilistic models (DDPM) trained a Markov chain that adds a little Gaussian noise per step and learned to reverse each step.

Both worked, but they were separate methods. Each used a fixed, finite list of noise levels, and each had its own sampler. Changing one part, such as the noise schedule or the sampler, meant rederiving the method from scratch. DDPM also had recently pulled ahead on sample quality, and the paper's authors wanted to know whether the score-based side could catch up with better architectures and better samplers, rather than by adding more noise levels.

Key idea

Replace the finite list of noise levels with a continuum. Define one forward SDE that gradually diffuses a data point into noise, with no trainable parameters:

dx=f(x,t) dt+g(t) dw.\mathrm{d}\mathbf{x} = \mathbf{f}(\mathbf{x}, t)\,\mathrm{d}t + g(t)\,\mathrm{d}\mathbf{w}.

Anderson's 1982 result says the time-reversed process is also a diffusion, and it depends on the forward SDE and on the score of the marginal density at each time. So if a network learns the score ∇xlog⁡pt(x)\nabla_{\mathbf{x}} \log p_t(\mathbf{x}) for every tt, the reverse SDE can turn noise back into data. The SMLD and DDPM noise schedules turn out to be two particular discretisations of two particular SDEs, and the framework lets you design others.

flowchart LR
  D[data x0] -->|forward SDE, no parameters| N[noise x_T]
  N -->|reverse SDE with learned score| S[sample x0]
  SC[score network s_theta of x and t] -.->|estimates the score| S
  SC -.->|trained by denoising score matching| D

The method

The forward SDE

The paper works with an Itô SDE

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

where w\mathbf{w} is a standard Wiener process (Brownian motion; see Stochastic Differential Equation), f\mathbf{f} is the drift, and g(t)g(t) is a scalar diffusion coefficient. Time runs over [0,T][0, T]. The data sit at t=0t = 0, and the process should end at a simple prior, such as a Gaussian with fixed mean and variance, at t=Tt = T.

Three SDEs cover the main cases:

  • VE (variance exploding). dx=d[σ2(t)]/dt dw\mathrm{d}\mathbf{x} = \sqrt{\mathrm{d}[\sigma^2(t)]/\mathrm{d}t}\,\mathrm{d}\mathbf{w}. The variance grows without bound as tt grows. This is the SMLD limit.
  • VP (variance preserving). dx=−12β(t)x dt+β(t) dw\mathrm{d}\mathbf{x} = -\tfrac{1}{2}\beta(t)\mathbf{x}\,\mathrm{d}t + \sqrt{\beta(t)}\,\mathrm{d}\mathbf{w}. Starting from unit variance, the variance stays at one. This is the DDPM limit.
  • sub-VP. The same drift as VP, with a diffusion term β(t)(1−e−2∫0tβ(s) ds)\sqrt{\beta(t)(1 - e^{-2\int_0^t \beta(s)\,\mathrm{d}s})}. Its variance is always below the VP variance at each time. The paper found it does well on likelihoods.

Reversing the SDE

The reverse-time SDE, run from t=Tt = T back to t=0t = 0, is

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

where wˉ\bar{\mathbf{w}} is a Wiener process running backward in time. The only unknown term is the score of the marginal density ptp_t. The forward SDE, the prior and the score are enough to sample.

Training the score

The score network sθ(x,t)\mathbf{s}_\theta(\mathbf{x}, t) is trained with a continuous version of denoising score matching (see Score Matching):

θ∗=arg⁡min⁡θ Et{λ(t) Ex(0)Ex(t)∣x(0)[∥sθ(x(t),t)−∇x(t)log⁡p0t(x(t)∣x(0))∥22]}.\theta^* = \arg\min_\theta \, \mathbb{E}_t\left\{ \lambda(t)\, \mathbb{E}_{\mathbf{x}(0)} \mathbb{E}_{\mathbf{x}(t) \mid \mathbf{x}(0)} \left[ \left\lVert \mathbf{s}_\theta(\mathbf{x}(t), t) - \nabla_{\mathbf{x}(t)} \log p_{0t}(\mathbf{x}(t) \mid \mathbf{x}(0)) \right\rVert_2^2 \right] \right\}.

Here tt is sampled uniformly on [0,T][0, T], λ(t)\lambda(t) is a positive weight, and p0tp_{0t} is the transition kernel of the forward SDE. For affine drifts (VE, VP, sub-VP), p0tp_{0t} is Gaussian with a closed-form mean and variance, so each training pair (x(t),x(0))(\mathbf{x}(t), \mathbf{x}(0)) is cheap to create. This is why the paper's VE, VP and sub-VP SDEs train efficiently.

Example
One training target by hand, VP SDE

Take the VP SDE with constant β(t)=β=2\beta(t) = \beta = 2, a data point x0=2x_0 = 2, and time t=0.5t = 0.5.

  1. Integrate the rate: ∫0tβ ds=2⋅0.5=1\int_0^t \beta\,\mathrm{d}s = 2 \cdot 0.5 = 1.
  2. Mean of the kernel: e−12⋅1⋅2=0.6065⋅2=1.2131e^{-\frac{1}{2}\cdot 1} \cdot 2 = 0.6065 \cdot 2 = 1.2131.
  3. Variance of the kernel: 1−e−1=0.63211 - e^{-1} = 0.6321.
  4. Sample x(t)=1.5x(t) = 1.5 from this Gaussian.
  5. The score of the kernel at that point is −(x(t)−mean)/variance=−(1.5−1.2131)/0.6321=−0.4539-(x(t) - \text{mean}) / \text{variance} = -(1.5 - 1.2131)/0.6321 = -0.4539.
  6. The network is trained to output −0.4539-0.4539 at input x(t)=1.5x(t) = 1.5 and time t=0.5t = 0.5.

Interpretation: the regression target is the score of a known Gaussian, so it needs no sampling of the full data distribution. That is the reason the closed-form kernel matters.

Solving the reverse SDE

Any general-purpose SDE solver can integrate the reverse SDE. The paper gives three options.

  • Reverse diffusion sampler. Discretise the reverse SDE in the same way as the forward SDE. It is simple to derive for a new SDE, and the paper reports it does slightly better than DDPM's ancestral sampling.
  • Predictor-corrector (PC) sampler. Take a step with a numerical SDE solver (the predictor), then take one or more steps of score-based Langevin MCMC at the same time (the corrector). This corrects the sample toward the marginal at that time.
  • Probability flow ODE. Every diffusion has a deterministic process with the same marginals at every time:

dx=[f(x,t)−12g(t)2∇xlog⁡pt(x)]dt.\mathrm{d}\mathbf{x} = \left[\mathbf{f}(\mathbf{x}, t) - \tfrac{1}{2} g(t)^2 \nabla_{\mathbf{x}} \log p_t(\mathbf{x})\right]\mathrm{d}t.

Because it is an ODE, a black-box adaptive solver can integrate it, and the number of score evaluations can be traded against accuracy. The paper reports that a looser error tolerance cut the number of evaluations by over 90 percent without visibly hurting samples.

Example
Cost of a predictor-corrector run

The paper's Table 1 labels a run "PC1000" for 1000 predictor steps and 1000 corrector steps.

  1. Each predictor step needs one score evaluation, so 1000 evaluations.
  2. Each corrector step needs one more score evaluation, so another 1000.
  3. Total: 1000+1000=20001000 + 1000 = 2000 score-network calls for one sample.

Interpretation: when the table compares samplers at equal computation, it means equal score evaluations, and a corrector step costs as much as a predictor step.

Exact likelihoods and latent codes

The probability flow ODE is a neural ODE (see Neural Ordinary Differential Equation). The instantaneous change-of-variables formula then gives the exact log-likelihood of any input, under the dequantised data model used in the paper. Because the forward SDE has no trainable parameters, the map from data to latent code is determined by the data distribution, and the paper uses that to interpolate between images and to edit latents.

Controllable generation

The reverse SDE for a conditional distribution pt(x(t)∣y)p_t(\mathbf{x}(t) \mid \mathbf{y}) adds one term, ∇xlog⁡pt(y∣x)\nabla_{\mathbf{x}} \log p_t(\mathbf{y} \mid \mathbf{x}), to the unconditional score. The unconditional model is not retrained. The paper uses this for class-conditional sampling, inpainting and colourisation. The extra term has to be estimated, for example with a time-dependent classifier, which the paper trains on noisy versions of labelled data.

Results

The main numbers are on CIFAR-10 (32 by 32 pixels, unconditional generation) and on likelihood. Lower FID and higher Inception score (IS) are better. The numbers below are the paper's own, quoted from its text. The FIDs are the lowest value over the course of training, following the convention of earlier work, so they are best-checkpoint numbers. The paper's Table 2 (likelihood and ODE-sampled FID) instead uses the last checkpoint, so the two kinds of figure are not directly comparable. All FIDs use 50,000 samples and a 1000-step sampler with reverse diffusion as the predictor. VE models add one Langevin corrector step per predictor step. VP models skip the corrector, because it "only gives slightly better results but requires double computation".

ModelSettingResult
NCSN++ (VE)Basic architectureFID 2.45
DDPM++ (VP)Basic architectureFID 2.78
DDPM++ cont. (VP)Continuous objectiveFID 2.55
DDPM++ cont. (sub-VP)Continuous objectiveFID 2.61
NCSN++ cont. (deep, VE)Best sample qualityIS 9.89, FID 2.20
DDPM++ cont. (deep, sub-VP)Best likelihood2.99 bits/dim

The results support three claims.

  • The score-based side caught up. With better architectures and the new samplers, SMLD-style VE models matched or beat the DDPM results that had recently led on sample quality. The deep VE model set new records for IS and FID on unconditional CIFAR-10. The paper also notes it beat the previous best conditional model on FID while using no labels.
  • Architecture and objective matter. Switching from the discrete training objective to the continuous one, and increasing depth, improved FID for all three SDEs. The continuous objective improved the VP model from 2.78 to 2.55.
  • Likelihood. The sub-VP model reached 2.99 bits per dimension on uniformly dequantised CIFAR-10, which the paper reports as the best known value for that evaluation at the time. It also notes that VE SDEs typically give better sample quality but worse likelihoods than VP and sub-VP SDEs, and that sub-VP always beat VP on likelihood, so the best SDE depends on the goal.

The paper also reports the first high-fidelity 1024 by 1024 samples from a score-based model, on CelebA-HQ.

Two caveats matter. Sampling is slow: the paper concedes that these samplers remain slower than GANs on the same datasets. And the breadth of samplers brings many hyperparameters, such as the corrector's signal-to-noise ratio, which the conclusion flags as needing better automatic tuning.

Why it mattered

The paper turned a set of separate methods into a single mathematical object. After it, diffusion models could be designed by choosing an SDE and a sampler, rather than by inventing a new noise schedule. The reverse-SDE view, the probability flow ODE and the conditional-sampling recipe became standard tools in later work on image, audio and video generation.

The probability flow ODE mattered for practice as well as theory. It made diffusion models into invertible maps with exact likelihoods, and it opened a route to fast deterministic samplers that later work built on, including ODE-based samplers with far fewer steps than the original 1000.

Reading guide

Read the paper in this order, and use the listed lessons for the prerequisites each section needs:

  1. Section 1, Introduction. Needs Diffusion Model for the idea of learning to reverse a noising process, and Denoising Diffusion Probabilistic Models (DDPM) for the discrete version the paper generalises.
  2. Section 2, Background. Needs Gaussian (Normal) Distribution for the perturbation kernels, Score Matching for the score objective, Langevin Dynamics for the SMLD sampler, and Markov Chain for the DDPM chain.
  3. Section 3.1 and 3.2, the forward and reverse SDEs. Needs Stochastic Differential Equation for the Itô SDE and the Wiener process, and Langevin Dynamics for the score-based MCMC idea.
  4. Section 3.3, score estimation. Needs Score Matching again for the continuous objective.
  5. Section 3.4, the VE, VP and sub-VP examples. Needs Gaussian (Normal) Distribution for the closed-form kernels.
  6. Section 4, solvers and probability flow. Needs Monte Carlo Method for the MCMC corrector and Neural Ordinary Differential Equation for the probability flow ODE and the likelihood calculation.
  7. Section 4.4 and Appendix H, architectures. Needs U-Net for the encoder-decoder backbone the architectures build on.
  8. Section 5, controllable generation. Uses the reverse SDE from Section 3.2 and the score from Section 3.3, so read those first.

Check yourself

QuestionRecall: what is the difference between the VE and VP SDEs in terms of variance?Show answer

The VE SDE has a variance that grows without bound as time increases, which corresponds to the SMLD noise schedule. The VP SDE keeps the variance at one when it starts from unit variance, which corresponds to the DDPM noise schedule. The paper names them by this property.

QuestionComputation: with the VP SDE and constant β=1\beta = 1, what is the variance of the transition kernel at t=2t = 2, and what does the score target look like for a point x(t)x(t)?Show answer

The integral of β\beta from 0 to 2 is 2, so the kernel variance is 1−e−2=1−0.1353=0.86471 - e^{-2} = 1 - 0.1353 = 0.8647. The score target at a point x(t)x(t) is −(x(t)−e−1x0)/0.8647-(x(t) - e^{-1} x_0)/0.8647, where e−12⋅2=e−1=0.3679e^{-\frac{1}{2}\cdot 2} = e^{-1} = 0.3679 scales the data point. The target is negative when x(t)x(t) lies above the kernel mean, pointing back toward it.

QuestionWhy: why does the paper call the probability flow ODE useful even though the SDE already samples from the model?Show answer

The ODE has the same marginal distributions at every time as the SDE, but it is deterministic. A deterministic process can be integrated with a black-box adaptive ODE solver, so the step count can be traded against accuracy. It also gives an invertible map from data to a latent code, which makes exact likelihood computation and latent interpolation possible. The SDE sampler does not offer these.

QuestionApplied: you have a trained score model and want to generate images of one class from an unconditional model, without retraining. What term do you add to the reverse SDE, and what do you need?Show answer

Add ∇xlog⁡pt(y∣x)\nabla_{\mathbf{x}} \log p_t(\mathbf{y} \mid \mathbf{x}) to the unconditional score inside the reverse drift, where y\mathbf{y} is the class label. You need a time-dependent estimate of log⁡pt(y∣x(t))\log p_t(\mathbf{y} \mid \mathbf{x}(t)), which the paper gets from a classifier trained on noisy versions of labelled data. The score model itself is left as it is. The cost is that the classifier must work at every noise level, not just on clean images.

Sources

  • Song et al. (2020), Score-Based Generative Modeling through Stochastic Differential Equations: arxiv.org/abs/2011.13456
  • Ho et al. (2020), Denoising Diffusion Probabilistic Models: arxiv.org/abs/2006.11239
  • Anderson (1982), Reverse-time diffusion equation models, Stochastic Processes and their Applications.

Read the full paper

Yang Song, Jascha Sohl-Dickstein, Diederik P. Kingma et al., 2020. arXiv:2011.13456