Quant Memo
Advanced

Score-Based Models and Langevin Dynamics

Rather than learning a data distribution directly, a score-based model learns the direction that makes any point more probable, and then generates samples by repeatedly nudging random noise along that direction with a bit of extra randomness mixed in.

Prerequisites: Denoising Diffusion Probabilistic Models, Gradient Descent

Most generative models try to learn p(x)p(x), the probability of seeing data point xx, directly. That's harder than it sounds — p(x)p(x) has to integrate to 11 over all possible xx, a global constraint that makes direct estimation awkward for complex, high-dimensional data. A score-based model sidesteps the problem by learning something that doesn't need to integrate to anything in particular: the score, xlogp(x)\nabla_x \log p(x) — the direction, at any point xx, in which probability increases fastest. Knowing that direction everywhere turns out to be enough to generate samples from p(x)p(x), without ever computing p(x)p(x) itself.

The analogy: finding the top of a hill in the fog

Imagine standing somewhere on a hillside in thick fog, unable to see the whole landscape (you don't know p(x)p(x)), but able to feel which way is uphill from wherever you're standing (you know xlogp(x)\nabla_x \log p(x) at your current location). Repeatedly stepping uphill, using only that local feel, eventually gets you near a peak — a high-probability region — even though you never once saw the whole hill's shape. That's the entire strategy: learn the local "which way is more probable" direction everywhere, and let repeated small steps do the rest.

Langevin dynamics: uphill steps plus noise

Pure gradient ascent on logp(x)\log p(x) would march straight to the single highest peak and get stuck there, generating the same point every time. Langevin dynamics fixes this by adding a controlled amount of noise to every step:

xt+1=xt+ϵ2xlogp(xt)+ϵzt,ztN(0,I)x_{t+1} = x_t + \frac{\epsilon}{2} \nabla_x \log p(x_t) + \sqrt{\epsilon}\, z_t, \qquad z_t \sim \mathcal{N}(0, I)

In words: take a small step in the uphill (higher-probability) direction, sized by step size ϵ\epsilon, and then add a small random jolt scaled by ϵ\sqrt{\epsilon}. The uphill pull keeps you near high-probability regions; the noise stops you from collapsing onto one exact point, and, run for long enough with a properly decreasing ϵ\epsilon, the sequence of xtx_t's ends up distributed according to p(x)p(x) itself, not just clustered near its mode.

Worked example 1: one Langevin step by hand

Say xt=3x_t = 3, and the learned score at that point is xlogp(xt)=1.5\nabla_x \log p(x_t) = -1.5 (probability increases toward smaller xx here), step size ϵ=0.1\epsilon = 0.1, and a sampled noise zt=0.4z_t = 0.4. Then:

xt+1=3+0.12(1.5)+0.1(0.4)=30.075+0.126=3.051x_{t+1} = 3 + \frac{0.1}{2}(-1.5) + \sqrt{0.1}(0.4) = 3 - 0.075 + 0.126 = 3.051

The deterministic pull moved xx down by 0.0750.075 (toward higher probability), but the noise term pushed it up by more, landing slightly higher than xtx_t overall — a reminder that any single step can move either way; it's the accumulated pull over many steps, with ϵ\epsilon shrinking over time, that converges toward the true distribution's shape rather than one point.

Worked example 2: why the score doesn't need p(x)p(x)'s normalizing constant

Suppose the true (unnormalized) density is p~(x)=5e2(x1)2\tilde{p}(x) = 5\,e^{-2(x-1)^2}, so the true, normalized density is p(x)=p~(x)/Zp(x) = \tilde{p}(x)/Z for some constant ZZ that makes it integrate to 1. The score is:

xlogp(x)=x[logp~(x)logZ]=xlogp~(x)=4(x1)\nabla_x \log p(x) = \nabla_x \big[\log \tilde{p}(x) - \log Z\big] = \nabla_x \log \tilde{p}(x) = -4(x-1)

The constant logZ\log Z vanished under the gradient, since it doesn't depend on xx. At x=1x=1: score =0=0 (already at the peak). At x=2x=2: score =4=-4 (strong pull back toward 11). This is the whole reason score-based models are tractable where direct likelihood models struggle — the impossible-to-compute normalizing constant ZZ simply never appears in the quantity being learned.

Path explorer
13055time →
end (bold path) 100.38spread of ends 58.966 independent paths, same settings

Each wiggly path above is exactly a Langevin trajectory with the deterministic drift term switched off — pure noise. Score-based sampling is this same random walk with a learned "pull toward higher probability" added back in at every step.

start (noise) peak of p(x)
The score field points inward toward higher-density contours everywhere; a Langevin path (jagged line) follows that pull while noise keeps it from converging to a single point, tracing out samples from the whole distribution over time.

A score-based model learns xlogp(x)\nabla_x \log p(x) — the local uphill direction of probability — without ever needing p(x)p(x)'s normalizing constant, and Langevin dynamics generates samples by repeatedly stepping along that direction with injected noise, which keeps the process exploring the full distribution instead of collapsing onto its single peak.

What this means in practice

Score-based models are the mathematical backbone of modern diffusion models: a diffusion model is, at its core, a network trained to estimate the score at every noise level, with sampling implemented as a discretized Langevin-like process run from pure noise down to a clean sample. Understanding the score/Langevin view explains why diffusion sampling needs many small steps rather than one big jump — each step is only a local, noisy uphill move, not a leap to the answer.

The score is only reliable where the model has actually seen data — in low-density regions far from the training distribution, the learned score can be essentially arbitrary, since there was little signal there to learn from. This is why naive Langevin sampling struggles to mix between separate high-probability regions separated by a low-density gap, and why practical diffusion models add noise at multiple scales rather than relying on the score at the original data scale alone.

Related concepts

Practice in interviews

Further reading

  • Song & Ermon, Generative Modeling by Estimating Gradients of the Data Distribution (2019)
ShareTwitterLinkedIn