Syllabus Lesson 185 of 246 · Module 17: Noise in, pictures out (diffusion)
Module 17: Noise in, pictures out (diffusion)

From noise to a spiral (DDPM)

Why it matters This is the moment noise becomes data. Start from pure noise and take 100 small steps back, each using the network once. The loop you write here is, in shape, the one that draws pictures in today's image generators.

One step back from x_t to x_(t-1), the DDPM way:

  1. Predict the noise, and from it the clean estimate x0 (last lesson).
  2. Clip x0 to the range real data can have (here -3 to 3).
  3. Move to the exact average of where x_(t-1) would be, given x_t and x0:
    mean = (sqrt(abar_(t-1)) beta_t / (1 - abar_t)) x0 + (sqrt(1 - beta_t) (1 - abar_(t-1)) / (1 - abar_t)) x_t
  4. Add fresh noise of spread sqrt(beta_t (1 - abar_(t-1)) / (1 - abar_t)), except at the last step. (abar_(-1) counts as 1.)

Why the clip? The course found out the hard way. At the cosine schedule's last few steps beta reaches 0.999, so a plain step multiplies any error in the predicted noise about 30 times. Without the clip, the course's digit model drew pictures closer to scrambled pixels than to digits (closeness 4.68). With it, 1.09.

Words and symbols in this lesson
noise schedule (beta)
how much fresh noise is mixed in at each step
abar
the share of the original signal left after a number of steps (its square root multiplies x0)
x0, x_t
the clean data, and the same data after t steps of noise
eps
the bell-curve noise that was added; the network learns to predict it
DDPM
sampling by stepping back through every step, with fresh noise each time
on shape, coverage
the 2D judge: share of samples near the real shape, and share of the shape with a sample near it
Your turn

Write step_back(x, eps_pred, t, beta, abar, rng) exactly as the four steps above (clip x0 to -3 to 3, the mean, then the noise except at t = 0) and the sampling loop generate(model, n, rng) that starts from N(0, 1) noise at step 99 and steps back to 0. Pass with samples at least 0.8 on the spiral and 0.95 coverage.

Spotted a problem in this lesson? Report it · Ask about this lesson

This exercise opens soon

Finish the lesson before it and this one unlocks. The whole course is free, no account needed, and you can read this lesson right now.