JA EN
LearnGenerative Models
·★ MEMBER·PAPER·11 min read

The Mathematics of Diffusion — Generation Seen Through Scores and SDEs

A layer beneath 'add noise, then subtract it': diffusion models restated in the language of the score, the gradient of log-density. Why denoising and score estimation are literally the same job, what the forward SDE, reverse SDE and probability flow ODE actually assert, and how those equations turn into the knobs you set at inference time.

ModalitytextTaskgeneration

Generative Modeling by Estimating Gradients of the Data Distribution


Forget the altitude, remember the slope

Suppose you are somewhere in the mountains at night with no map and no GPS, trying to reach the summit. You have no way of knowing your altitude in metres. You can still climb, because knowing the slope under your feet is enough: step toward whichever direction goes up, repeat.

Generative modelling is in exactly that position. Try to learn the probability density p(x)p(x) of "plausible images" directly, and a normalising constant always gets in the way. Write the model as an energy function Eθ(x)E_\theta(x) and the density looks like this:

pθ(x)=eEθ(x)Zθ,Zθ=eEθ(x)dxp_\theta(x) = \frac{e^{-E_\theta(x)}}{Z_\theta}, \qquad Z_\theta = \int e^{-E_\theta(x)}\,dx
(1)

ZθZ_\theta is the number you divide by so that everything sums to one, and the integral runs over every image there could be. For a 256×256256\times256 colour image that is an integral over roughly 200,000 dimensions. Nobody can compute it.

But take the logarithm of both sides and then differentiate with respect to xx, and ZθZ_\theta vanishes — it does not depend on xx at all.

sθ(x)=xlogpθ(x)=xEθ(x)s_\theta(x) = \nabla_x \log p_\theta(x) = -\nabla_x E_\theta(x)
(2)

This is the score. The name is borrowed from the score function of classical statistics (the gradient of the log-likelihood), but the diffusion version differentiates with respect to the data xx rather than the parameters. In words: x\nabla_x means "differentiate along every component of xx and stack the results", so the score has exactly the same shape as its input. For an image, picture a field of arrows with one arrow per pixel, each saying nudge me this way and the picture becomes slightly more plausible. You give up on knowing the altitude and settle for knowing the slope.

The overall machinery of diffusion — the forward process that adds noise and the reverse process that removes it — is covered in Diffusion Models from the Ground Up. This article goes one layer beneath it and asks why that procedure is correct at all, in the language of scores.

Turning a slope into samples

Suppose you had the score. The recipe for extracting samples from it is Langevin dynamics, and it is one line:

xk+1=xk+ε2sθ(xk)+εzk,zkN(0,I)x_{k+1} = x_k + \frac{\varepsilon}{2}\, s_\theta(x_k) + \sqrt{\varepsilon}\, z_k, \qquad z_k \sim \mathcal{N}(0, I)
(3)

Here ε\varepsilon is the step size and zkz_k is a fresh standard normal draw each iteration. In words: take half a step toward higher density, then stagger by however much the dice say, over and over. Make ε\varepsilon small enough and run long enough, and the distribution of xkx_k approaches pθp_\theta.

That second term is not decoration. Remove it and you are left with plain gradient ascent, which parks itself on the single most probable point and stops. That finds the mode; it does not draw from the distribution. The noise is what keeps the walker wandering the slope, lingering long where density is high and briefly where it is low — and that ratio of dwelling times is the probability.

A concrete case makes it tangible. For p(x)=N(0,σ2)p(x) = \mathcal{N}(0, \sigma^2) we have logp(x)=x2/(2σ2)+const\log p(x) = -x^2/(2\sigma^2) + \text{const}, so differentiating gives

s(x)=xσ2s(x) = -\frac{x}{\sigma^2}
(4)

which says the score is a spring pulling back toward the origin: the further out you are the harder the pull, and the tighter the distribution (small σ\sigma) the stiffer the spring. So Langevin dynamics on a Gaussian is precisely a ball rolling in a parabolic valley with a random shove added at every step. And what happens when you crank the step size up is exactly what happens to that ball.

FIG 1The score of a Gaussian is −x/σ² — the very spring that pulls a ball into a valley. Read the learning-rate slider as the Langevin step size ε and raise it, and you can reproduce the moment a sampler diverges with your own hands

Why "denoising" and "score estimation" are the same job

There is an obvious problem. Nowhere do we have ground-truth labels for the score. If we knew the true xlogp(x)\nabla_x \log p(x), we would not need a generative model in the first place.

Plain score matching (Hyvärinen, 2005) solves this on paper. Integration by parts eliminates the unknown pp from the objective, leaving you to minimise the expectation of 12sθ(x)2+tr(xsθ(x))\frac{1}{2}\|s_\theta(x)\|^2 + \operatorname{tr}(\nabla_x s_\theta(x)). The theory is airtight, but that second term is the trace of a Jacobian, which costs one backward pass per dimension. On a 200,000-dimensional image it is a non-starter.

The way through was to deliberately corrupt the data. We do not know the distribution of clean data, but the conditional distribution after adding Gaussian noise is something we built ourselves, so we know it exactly. Corrupt with x~=x+σz\tilde{x} = x + \sigma z where zN(0,I)z \sim \mathcal{N}(0,I), and pσ(x~x)=N(x~;x,σ2I)p_\sigma(\tilde{x}\mid x) = \mathcal{N}(\tilde{x}; x, \sigma^2 I) — whose score is the spring we just derived:

x~logpσ(x~x)=x~xσ2=zσ\nabla_{\tilde{x}} \log p_\sigma(\tilde{x} \mid x) = -\frac{\tilde{x} - x}{\sigma^2} = -\frac{z}{\sigma}
(5)

which says the direction "back from the corrupted sample toward the original" *is* the conditional score. What denoising score matching (Vincent, 2011) guarantees is that regressing onto this target gives you, at the optimum, the score of the marginal — even though the per-example target is noisy and the marginal was n

What's behind this

§

Members-only from here

371 walkthroughs, 26 textbook chapters, 48 student units and 6 close readings — all included for $4.99/mo, with three new explainers every day. Cancel any time; access runs to the end of the period.

Already a member? Sign in to keep reading

References

  1. Generative Modeling by Estimating Gradients of the Data Distribution. arXiv:1907.05600Paper page·PDF
  2. Score-Based Generative Modeling through Stochastic Differential Equations. arXiv:2011.13456Paper page·PDF
  3. Elucidating the Design Space of Diffusion-Based Generative Models. arXiv:2206.00364Paper page·PDF

This article is written from the source paper above. Where they differ, the original is authoritative.

Comments

Sign in to comment