Machine learning · 15 min

Diffusion: generating by removing noise

Noise a shape away into a blob, then watch a trained denoiser turn fresh noise back into it, one small step at a time.

01Why it matters

Making something out of nothing

Image generators start every picture from pure static: random noise, different every time. Then, in a few dozen small steps, the static turns into a face or a landscape. Nothing in the noise says "face"; the knowledge is in a network that has learned one narrow skill: looking at something noisy and guessing which noise was added.

This lesson shows the whole idea on the smallest possible example: clouds of 2D points instead of pictures, so you can watch every point move.

02Key parts

The pieces you will see

forward process
Adding a little Gaussian noise at each step t=1,…,Tt = 1, \dots, T, until the data is indistinguishable from pure noise.
noise schedule
How much noise each step adds, βt\beta_t. Its running product αˉt=(1−β1)⋯(1−βt)\bar\alpha_t = (1-\beta_1)\cdots(1-\beta_t) is how much of the original signal survives to step tt.
denoiser
A neural network that takes a noisy point xtx_t and the step tt and predicts the noise ε\varepsilon in it.
reverse process
Generating: start from fresh noise and repeatedly remove a share of the predicted noise, from t=Tt = T down to 11.

03See it work

Destroy the data, then rebuild it

First the spiral is noised away into a round blob. Then fresh noise flows back onto the spiral, one small step at a time.

The forward process needs no learning at all. Because Gaussian noise adds up to Gaussian noise, you can jump straight to any step:

xt=αˉt x0+1−αˉt ε,ε∼N(0,I).x_t = \sqrt{\bar\alpha_t}\,x_0 + \sqrt{1-\bar\alpha_t}\,\varepsilon, \qquad \varepsilon \sim \mathcal{N}(0, I).

As tt grows, αˉt\sqrt{\bar\alpha_t} (the signal) falls towards 0 and 1−αˉt\sqrt{1-\bar\alpha_t} (the noise) rises towards 1, so every shape ends as the same blob: a standard normal around the origin.

Training is a guessing game built on that formula: take a data point, pick a random tt and a random ε\varepsilon, compute xtx_t, and ask the network for ε^\hat\varepsilon. The loss is ∥ε^−ε∥2\lVert \hat\varepsilon - \varepsilon \rVert^2. Our denoiser here is a small network with two hidden layers of 48 units, trained once when the site was built.

04Predict

Your turn: follow a point

Before each step, predict where the cloud or the followed point goes next. In the reverse process, use the dashed arrow.

Make 5 predictions: step forward and answer each question before the next step plays.

0 of 5 made

05Try it

Why so many steps?

When the noise is large, the guess x^0\hat x_0 is poor: a point that is mostly noise could have come from almost anywhere on the shape, so the best guess is a blurry average of all of them, near the middle. Jumping straight to it would land every point in the same place. Instead, each step moves only a little towards the guess and adds a little fresh noise back; at the next step the point is slightly less noisy and the guess slightly better.

Try it with the controls below:

  • Change the seed: the generated points are different every time, yet they always land on the shape. They are new samples, not copies of the training points.
  • Watch the arrow on the ring: early on it swings around; near the end it is short and steady.
  • Compare the shapes: the thin spiral is the hardest, and a few points end up between its arms.

06Check yourself

Check yourself

  1. Question 1 of 6

    What does the denoiser network predict?

    Your answer comes back in Review in a few days, so it sticks.

  2. Question 2 of 6

    Why can training jump straight to any step t instead of noising one step at a time?

  3. Question 3 of 6

    What does the cloud look like after the last forward step, whatever shape it started as?

  4. Question 4 of 6

    Why does generating take many small steps rather than one big jump to the predicted clean point?

  5. Question 5 of 6

    A diffusion model trained on a spiral generates 80 points. What are they?

  6. Question 6 of 6

    The denoiser has made its first guess of the clean point from almost pure noise. What does the sampler do next?

07Explain it back

Say it in your own words

How does a diffusion model turn pure noise into a new sample, and why does it need many steps?

08Recap

What to remember

  • The forward process adds noise until data becomes N(0,I)\mathcal{N}(0, I); the closed form jumps to any step.
  • A network is trained to predict the added noise ε\varepsilon from xtx_t and tt.
  • Generating runs backwards from fresh noise: remove a share of the predicted noise, add a little fresh noise, repeat.
  • Many small steps are needed because the denoiser's guess only sharpens as the noise fades.
  • Image models do exactly this to every pixel at once, with a far bigger network that also reads a text prompt.
Back to path