Skip to content

6.5 Diffusion and score matching

ready≈ 11.5 hoursNeeds: 6.4, 6.2Unlocks: 6.6
  1. 1 Learn
  2. 2 Bridge & derive
  3. 3 Explore (Forward diffusion slider, Score-field viewer)
  4. 4 Do
  5. 5 Check

Destroy the data, then learn to undo it. 6.4 ended with annealed Langevin: sample a blurred p first, then sharpen. A diffusion model fixes the blurring in advance. The forward process adds a little Gaussian noise at each of T steps, xt = √(1 − βt) xt−1 + √βt ε. Gaussians compose, so any step can be reached in one jump: xt = √ᾱt x0 + √(1 − ᾱt) ε, with ᾱt the product of the (1 − βs). By t = T almost nothing of x0 is left (Ho et al. 2020, section 2).

What to learn. Running the process backwards needs the score ∇log pt of every noised distribution, which we cannot compute for real data. Denoising score matching gets around this: the score of q(xt | x0), which is −ε / √(1 − ᾱt), averages to the true score over all the x0 that could have produced xt. So a network ε̂(xt, t) trained to predict the added noise with a squared loss learns −√(1 − ᾱt) times the score (Song and Ermon 2019, section 2.1). Ho et al.’s “simple” loss is exactly this regression, with t drawn uniformly.

Sampling. DDPM steps from t to t − 1 using the mean of q(xt−1 | xt, x0) with x0 estimated from ε̂, plus fresh noise; it needs all T steps. DDIM reuses the same network but takes a deterministic path, so it can skip most steps and the same noise always gives the same sample (Song, Meng and Ermon 2020, section 4). The schedule matters too: a cosine schedule spends fewer steps on near-pure noise than the linear one (Nichol and Dhariwal 2021, section 3.2).

Checking it. If the data is a mixture of Gaussians, every pt is too (each component is only scaled and widened by the forward process of Ho et al.), so here the exact score is known and the learned one can be compared with it.

Why predicting the noise learns the scorestep 1 of 5
  1. pt(x)=∫p0(x0) q(x∣x0) dx0,q(x∣x0)=N(αˉt x0, (1−αˉt)I)p_t(x) = \int p_0(x_0)\, q(x \mid x_0)\, dx_0, \quad q(x \mid x_0) = \mathcal{N}(\sqrt{\bar\alpha_t}\, x_0,\, (1 - \bar\alpha_t) I)

Forward diffusion slider

How data becomes noise

Static view on small screens. Open this page on a laptop to interact.

schedule
xt = √ᾱt x0 + √(1 − ᾱt) ε, coloured by the blob each point came from. Dashed: 1 and 2 standard deviations of each noised blob; grey: 2 standard deviations of N(0, I)
00.5112505007501000step tlinear and cosine ᾱt
ᾱt, the share of signal variance left. It falls below ½ at step 260 (linear) and 497 (cosine)
ᾱt = 0.897: √ᾱ = 0.947 of the data, √(1 − ᾱ) = 0.321 noise signal-to-noise ratio 8.71 blob widths now 0.50 / 0.65 / 0.43 (at t = 0: 0.40 / 0.60 / 0.30)

Every point slides straight towards the origin, scaled by √ᾱt, while its own fixed noise ε is mixed in with weight √(1 − ᾱt), so the blobs shrink towards zero and widen until they merge into N(0, I). Because each blob stays Gaussian, pt is a Gaussian mixture at every step, with its score in closed form. The linear schedule leaves under 1% of the signal variance from step 674 on, so its last third is almost pure noise; the cosine schedule spreads the change more evenly and only gets there at step 936.

Two plots. The left one shows 400 points from a mixture of three Gaussian blobs after t noising steps, coloured by their blob, with dashed circles for the noised blobs and a grey circle for two standard deviations of N(0, I). A slider sets t from 1 to 1000 and buttons choose the linear or cosine schedule or draw new points. The right plot shows ᾱt against t for both schedules with a marker at the current step. The readout gives ᾱt, the signal and noise weights, the signal-to-noise ratio and the blob widths. Keyboard: Tab to the buttons and the slider.

Score-field viewer

Scores as denoising directions

Static view on small screens. Open this page on a laptop to interact.

sampler
shading: the density pt; arrows: its score ∇log pt, the direction a denoiser pushes. The network's target is −√(1 − ᾱt) times this
300 samples from N(0, I) noise, run backwards from step 1000 to 0 with the exact score; 20 paths drawn. Dashed: 1 and 2 standard deviations of the data's blobs
share nearest each blob: 47% / 31% / 22% (true weights 50% / 30% / 20%)

Slide t from 1000 down to 1: at large t, pt is one wide blob and the arrows all point to the middle; as t falls, the field splits and each arrow points to its nearest blob. Sampling follows these fields in that order, so a particle picks its blob while the blobs still overlap, which is why the shares come out right (compare plain Langevin in 6.4). DDIM is the deterministic version: the same noise always gives the same sample, and the paths are smooth. DDPM adds fresh noise each step; with few steps it blurs the blobs, while DDIM keeps them tight down to about 10 steps. A trained network only approximates these arrows: the notebook learns them from samples.

Two square plots. The left one shades the density of the noised mixture at step t, chosen with a slider, and draws its exact score as arrows. The right one shows 300 samples generated from pure noise by running DDPM or DDIM backwards with the exact score, with 20 of their paths and dashed circles for the data’s blobs; buttons choose the sampler and draw new noise and a slider sets the number of sampling steps. The readout gives the share of samples nearest each blob against the true weights. Keyboard: Tab to the buttons and sliders.

Watch the time direction. This module uses DDPM’s convention: x0 is data and t counts up toward pure noise. Module 6.4 and modules 6.6–6.8 use the flow convention instead, with noise at t = 0 and data at t = 1. Formulas carry over once you flip t. Symbols differ between sources too: here αt = 1 − βt and ᾱt is their running product, but UDL chapter 18 and the DDIM paper (eq. 12) write αt for what this page calls ᾱt, and 6.S184 uses βt for a noise standard deviation. Translate before you copy a formula.

Diffusion and score matching: edit exercises/stubs/m6_5_diffusion.py on your own machine. This task needs PyTorch, which the in-page runner does not have.

pip install torch   # CPU build is enough for the tests
mlc check 6.5
Hints · noised_mixture 0/3 revealed
Hints · ddpm_loss 0/3 revealed
Hints · ddpm_sample 0/3 revealed
Hints · ddim_sample 0/3 revealed

Then run the lab. Open notebooks/6.5-diffusion.ipynb (a CPU is enough) and paste in your 6.5 file. It trains your EpsNet on 20,000 points from the three-blob mixture, compares the learned score with the exact one at three noise levels, and samples with 1000 DDPM steps and 50 DDIM steps.

1. Why can x_t be sampled from x_0 in one step instead of t?
2. The network is trained to predict ε, which is pure noise and cannot be predicted from x_t exactly. What does it learn?
3. Why does a diffusion model get the blob weights right where plain Langevin from one start did not (6.4)?
4. You run DDIM twice from the same starting noise. What do you get?
  • not yet: All tests pass (0/?)
  • not yet: Checkpoint quiz ≥ 80%
Finish the items above first.
Learner log: time spent and points of confusion

Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.