Part XIII — Generative Models: VAEs, Flow Matching, Score Models & Diffusion Transformers · Chapter 5

Score-based generative models

Hook

Last chapter's flow needed an invertible function, engineered by hand or by careful architecture choices, before it could define a density at all. What if a model didn't need to invert anything — just needed to know, at any point, which direction makes the data more likely?

Intuition
step 0: x = -5.0000, score(x) = 8.0000, distance to mode = 8.0000

Click "Take a Langevin step" a few times. The point starts at x=5x=-5 — nowhere near the data — and drifts steadily toward x=3x=3, the single peak of this chapter's target distribution, one gradient step at a time.

Formalize

The score of a distribution is s(x)=xlogp(x)s(x) = \nabla_x \log p(x) — a vector field pointing toward higher density. For a Gaussian N(μ,σ2)\mathcal{N}(\mu,\sigma^2), it has a clean closed form:

s(x)=ddxlogp(x)=xμσ2s(x) = \frac{d}{dx}\log p(x) = -\frac{x-\mu}{\sigma^2}
  • s(x)s(x) — the score function: the gradient of the log-density, a vector field pointing toward higher-probability regions.
  • p(x)p(x) — the probability density of the target distribution.
  • μ\mu — the mean of the Gaussian target distribution.
  • σ2\sigma^2 — the variance of the Gaussian target distribution.
  1. Climbing the score reaches the mode

    Follow it uphill, repeatedly — xx+ηs(x)x \leftarrow x + \eta\, s(x) — and xx climbs toward μ\mu.

  2. Langevin dynamics samples the whole distribution

    Real score-based models add noise to this update (Langevin dynamics) so the process samples the whole distribution instead of always collapsing onto the single peak.

  3. Learned directly, no invertibility required

    A neural network is trained to estimate s(x)s(x) directly from data, without ever needing an invertible architecture or a tractable normalizing constant.

Play

Every bar here comes from a closed-form formula, not a simulation — the distance to the mode shrinks by exactly the same factor, 0.70.7, every single step, no matter how far the trajectory started. That factor is 1η/σ21 - \eta/\sigma^2: it's baked into the step size and the target's own spread, not something that has to be measured empirically.

Worked example

Starting at x0=5x_0=-5, target N(3,1)\mathcal{N}(3, 1), step size η=0.3\eta=0.3:

  1. Step 1

    s(5)=(53)/1=8s(-5) = -(-5-3)/1 = 8. x1=5+0.3(8)=2.6x_1 = -5 + 0.3(8) = -2.6.

  2. Step 2

    s(2.6)=(2.63)/1=5.6s(-2.6) = -(-2.6-3)/1 = 5.6. x2=2.6+0.3(5.6)=0.92x_2 = -2.6 + 0.3(5.6) = -0.92.

  3. The distance to the mode obeys a clean pattern
    • x03=53=8|x_0-3|=|-5-3|=8
    • x13=2.63=5.6|x_1-3|=|-2.6-3|=5.6
    • x23=0.923=3.92|x_2-3|=|-0.92-3|=3.92

    Each one is exactly 0.7×0.7\times the last (5.6/8=0.75.6/8=0.7, 3.92/5.6=0.73.92/5.6=0.7). That's not a coincidence of these particular numbers; it falls straight out of the fact that s(x)s(x) is linear in xx for a Gaussian.

Checkpoint

Find the smallest step count, among the candidates, where the trajectory lands within 0.5 of the mode.

Pick a step count to try it
Summary
xx+ηxlogp(x)x \leftarrow x + \eta\, \nabla_x \log p(x)

This chapter's target was a single Gaussian precisely because its score has a closed form simple enough to hand-verify. Real score-based models estimate s(x)s(x) with a neural network trained via score matching on complicated, multi-modal, high-dimensional data — where no closed form exists at all — and they run the noisy version of this update (not the deterministic climb shown here) so different starting points can land on different modes of the data, not just the nearest peak. The next chapter asks what happens if, instead of learning the score and simulating a path toward the data, a model is trained to predict the straight-line path itself.