ai papers
Understanding Score-Based Generative Modeling Through Stochastic Differential Equations
Junyoung Park · 2023-04-22 · 16 min
Introduction
Today's paper formulates score-based generative modeling through continuous-time SDEs. Along with DDPM, the best-known diffusion approach, it is often called a foundational diffusion paper—though its difficulty hardly feels foundational. Unlike DDPM and DDIM, which rely on stepwise prediction, its sampler solves differential equations numerically. The paper's strength is that it unifies and varies many score-based approaches, enabling a discussion of more effective generative sampling.
The mathematics is much harder than DDPM, and stochastic differential equations feel more complex because the scope of a solution changes substantially with its constraints. Still, understanding this paper seems necessary to think beyond methods such as DDPM and DDIM that are confined to stepwise sampling.
Score based generative process
Suppose a dataset has marginal probability distribution . To start from an easy-to-sample distribution and create data resembling real observations, we must find the relationship between the two distributions. Deep generative modeling aims to solve this task with deep learning.
One representative method indirectly aligns an intractable probability distribution with the maximum lower bound on likelihood, the ELBO, as in a Variational Autoencoder.
A GAN (Generative Adversarial Network) improves sampling quality through adversarial training between a decoder that creates fake images from randomly sampled noise and an encoder that distinguishes real images.
Flow-based modeling instead defines an invertible relationship between distributions as a flow and uses it to connect latent and data distributions.
Unlike these models, a score-based model learns the direction—the gradient—from a given noise sample toward the real data distribution and defines a process that gradually produces data samples from noise.
What is score?
I briefly discussed the score in the DDPM post; to restate it, the score is the gradient of a probability density function. Given a distribution over the original data's continuous variables, the direction in which a noisy sample at step should move to become plausible is the gradient of . Connecting this to an energy-based approach lets us define the score as the gradient of log-likelihood.
Two generative models built from these ideas arrived with dramatic impact: SMLD (Score Matching with Langevin Dynamics) and DDPM (Denoising Diffusion Probabilistic Model). Once generalized, both reduce to the same differential equation with different coefficients, so they can be viewed as solving the same problem. The derivation later makes this explicit.
Score estimation = Solving SDE
How can the mechanism of diffusion models be connected to stochastic differential equations? Consider the figure above. In DDPM or SMLD, data becomes noise simply by adding very small amounts of diffusion. Conventionally this noise is added over discrete states . If instead the distribution's gradual transformation into noise is defined over continuous time, it can be modeled as a solution to an Itô SDE.
This becomes clearer later when it is derived in relation to the DDPM and SMLD processes, so for now we can simply accept the idea.
A stochastic differential equation is one type of differential equation, so the problem is defined through relationships among rates of change . A solution can be understood as the trajectory traced by those rates for . An SDE differs in that it adds a stochastic term to the deterministic solution , though we can ignore that detail for the moment.
is a diffusion term that depends only on time, independently of 's actual trajectory, while defines the direction along that trajectory. The solution is therefore a function that returns the position at a given time , represented by the functional curve shown as the solid black line above.
Reversing the perspective, a trajectory depends on its direction. We can therefore define a new problem that predicts while traveling backward from to .
In this expression, the diffusion term is identical to the forward SDE. The score term, highlighted in bold, changes the trajectory associated with the drift term from the forward direction to the reverse direction.
The forward SDE and reverse SDE have different starting and ending points but must trace the same trajectory. If the forward SDE—the trajectory's form—is known, it can provide the solution to the reverse SDE. Predicting the score is thus equivalent to solving the reverse SDE, which generalizes generation to any sampler implemented as an SDE solver. The paper introduces two types: a PC (Predictor–Corrector) sampler and a deterministic sampler. As their names suggest, the PC sampler uses a score-based model to improve sample quality, while the deterministic sampler removes all randomness to support fast sampling and latent manipulation.
Training and sampling with SMLD/DDPM
I have not properly defined SMLD: it is an NCSN (Noise Conditional Score Network). NCSN and DDPM are both score-based generative models, but their papers develop the SDE problem through different methods. Let us examine how SMLD and DDPM train their score estimators and perform sampling.
Denoising score matching with Langevin Dynamics(SMLD)
Suppose a perturbation kernel with variance is added to . This kernel follows noise distribution . Consider increasing noise scales . The smallest scale is small enough that , while the largest is large enough that .
Because the true distribution is intractable, denoising score matching cannot predict directly. Instead, it predicts the conditional score of a noisy sample with a very small amount of added noise, , which approximates the target. Across many noise levels, from severe corruption down to data nearly identical to the original, score estimator has the following objective.
With enough data and model capacity, a trained score-based model predicts the scores required for sampling. Sampling is somewhat complex, but because it uses annealed Langevin dynamics, it can be understood as a nested loop. The noise level decreases from to across each , and Langevin MCMC steps run for every noise kernel—a total of steps. For a distribution corrupted by noise level ,
run the process above times with step size , analogous to a gradient-ascent learning rate, and random standard-normal vector . After sampling steps, lower the noise to and run another MCMC steps, initializing .
for sigma in sigma_N, ..., sigma_1:
for step in 1, ..., M:
noise = N(0, I) # Random noise
x = x + epsilon * model(x, sigma) + sqrt(2*epsilon)*noise
With a sufficiently large number of samples and a very small step size , the generated sample follows the original data distribution.
Denoising diffusion probabilistic models(DDPM)
DDPM defines a variance schedule whose noise scale increases step by step.
For each training data point (image) , it defines a perturbation kernel under the variance schedule as the following Markov chain.
Applying this across the full sequence yields the following marginal.
As in the SMLD example, this can also be expressed as a perturbed data distribution, where denotes the marginal obtained above.
In DDPM, the variable's variance is fixed at throughout noise addition, so . Although DDPM originally expresses the reverse process differently, its variational Markov chain can be written in score-matching form, like SMLD, as follows.
The evidence lower bound consequently has a different weighted sum. The expression below closely resembles the SMLD objective. Its coefficient is the variance of the th perturbation kernel . In SMLD, too, the coefficient was the perturbation kernel's variance, revealing the common structure of the two equations.
More precisely, for noisy score estimate , it is proportional to .
Given that minimizes this loss, sampling proceeds backward through the Markov chain.
The authors call this ancestral sampling, naming the gradual backward sampling through reverse process . DDPM's original loss has a different structure, shown below, but the reformulation above demonstrates that DDPM is in fact training a score-estimation model.
The key point is that in both SMLD and DDPM, just as inference—the forward process—can be expressed as an SDE, the reverse process can likewise be expressed as solving an SDE through score matching.
Extending Diffusion Modeling
Existing methods can be summarized as applying perturbation kernels at several noise scales to the data. This paper seeks to generalize score-based generation from predetermined discrete noise scales to every continuous noise scale,
which amounts to solving the SDE as a continuous-variable equation, as illustrated below.
As stated earlier, both the forward and reverse diffusion processes can be expressed as SDEs.
Corrupting a Dataset Into Noise With a Differential Equation
At its most basic, diffusion modeling takes an arbitrary i.i.d. dataset and uses a diffusion process indexed by a continuous or discrete time variable to create , which approaches Gaussian noise. As introduced above, the diffusion process that transforms data distribution into prior distribution can be modeled as the solution to a stochastic differential equation.
is a standard Wiener process, also called Brownian motion. is the drift coefficient over , and is the diffusion coefficient. Assuming and are both Lipschitz in state and time—that their derivatives are bounded—the SDE has a unique strong solution under bounded rates of change (reference).
Sampling by Solving the Differential Equation in Reverse
If the prior is known or predefined, we can sample . For example, defining the prior as a multivariate Gaussian lets us obtain by Gaussian sampling. A derivation shows that the reverse of a diffusion SDE is itself an SDE, written as follows (reference).
is merely different notation for the same Wiener process used in the forward process. Noise generation traces the differential equation's trajectory as increases in tiny increments; the reverse equation traces it as decreases in tiny increments. If the score of marginal distribution is known, an arbitrary noise sample can be transformed into .
Predicting the Score to Solve the Reverse SDE
Sampling ultimately requires predicting the score. Since the score of the original data distribution's marginal is unavailable, we optimize time-dependent score-matching model with a loss like those of SMLD and DDPM. SMLD and DDPM compute over a discrete perturbation sequence of length ; generalizing it to continuous variable gives
As noted earlier, choose . The score-matching method need not be denoising score matching; alternatives such as sliced score matching or finite-difference score matching also work.
Extending VE and VP SDEs
The noise-perturbation schemes of SMLD and DDPM can be viewed as discretized SDEs. With noise scales, SMLD's Markov-chain forward process is
As approaches infinity, the noise scale becomes a continuous-time function , and standard Gaussian variable likewise becomes . Replacing the Markov chain with continuous stochastic process gives
Assuming is very small, a first-order Taylor approximation turns this into a differential equation—the ordinary Euler method.
Likewise, DDPM follows the forward process
so as approaches infinity,
Here is the noise scheduled at each original Markov step, so the infinitesimal variance change is multiplied by . Again applying the Euler method yields the differential equation
The paper calls SMLD's differential equation a VE (Variance Exploding) SDE because variance continually increases, and DDPM's a VP (Variance Preserving) SDE because variance is preserved.
Because the coefficients have an affine form, the expression can be developed into a differential equation for variance, using the following equation.
This derivation is possible because it assumes a Gaussian distribution rather than a nonlinear case. Solving the ODE gives the covariance function of .
This shows that variance at every is bounded relative to . If , as in DDPM, it remains for all . The authors' new sub-VP SDE is
Solving the covariance ODE in the same way gives the covariance of the new differential equation.
The VP SDE can be viewed as an upper bound on the sub-VP SDE. Since a diffusion process may take any form , approaching it as a continuous function rather than beginning with discrete diffusion enabled the authors to propose this SDE. Sub-VP reportedly performs well in some likelihood experiments.
Stochastic Differential Equations Used in the Paper
We have introduced three different SDEs. Their perturbation kernels can be written as Gaussian marginal distributions as follows.
The sub-VP derivation looks complicated, but it simply changes DDPM's original noise construction from
to a form that squares the variance term:
Solving the reverse SDE
We have repeatedly established that if a score model gives at every time point, the theory lets us construct the reverse SDE. Numerically solving that reverse SDE to find is sampling.
Solving an SDE numerically amounts to predicting the form—the trajectory—of its solution function. Methods include Euler–Maruyama and stochastic Runge–Kutta, among others, though I do not know their details. The important point is that given a score predictor, any SDE solver can generate samples.
Ancestral sampling in DDPM is a special case and cannot be applied when the SDE changes even slightly, making it difficult to generalize. To relax this limitation, the authors propose the reverse-diffusion sampler below, which reportedly performs slightly better than ordinary ancestral sampling in SMLD and DDPM.
Defining an infinitesimal time change ever closer to zero should reduce the gap between ancestral sampling and the true SDE solution. The original ancestral sampler seems weaker because it depends on without accounting for the SDE's original form.
Predictor-corrector samplers
Ordinarily, solving an SDE performs only score prediction. Predictor–Corrector sampling adds an MCMC approach, namely Langevin dynamics. After the numerical SDE predicts the solution at the next time step, correction sampling adjusts that point using the score estimate. The numerical SDE solver acts as the predictor, and the score model acts as the corrector.
For both SMLD and DDPM, combining Predictor and Corrector substantially improves sampling efficiency over using either one alone.
Probability flow and Neural ODE
A score-based model lets us solve the SDE numerically. In fact, every diffusion process has an ODE with the same marginal likelihood . If the original diffusion SDE is
then the ODE with the same marginal likelihood is
as shown above. The proof is too complicated for me to understand yet. In any case, define . The ODE with Wiener-process term removed,
has the same marginal likelihood as the original SDE, meaning that sampling yields the same result. Rather than forcing an approximation of discrete likelihood as DDPM does below,
we can calculate the actual likelihood.
Because an ODE can ignore the random term entirely, it has the advantage of a one-to-one mapping between a specific data point and its endpoint along the path. This resembles an invertible flow-based model or neural ODE, making image editing and interpolation easy through latent representations. Each image maps to a unique latent, so the forward SDE can also be viewed as an encoder; the forward process is in fact independent of parameter learning.
Finally, removing stochasticity makes sampling with a neural ODE fast.
Conditional sampling
Another benefit of solving the continuous SDE is that it can generate not only unconditional samples but also samples from conditional probability . This requires class probability at each time step.
The class-conditional probability is learned by training an encoder under supervised learning on noisy samples, just as in the class-guidance paper Diffusion Beats GANs. The remaining methods appear in the appendix; I did not think they needed a detailed discussion here.