AlgoPlusAlgoPlus
Learn/Machine Learning
Lesson

Diffusion Models

Learn to remove noise, then generate from pure static by denoising your way to a brand-new image.

9 min read Watch it move Build it

A diffusion model generates images by learning to do one humble thing: remove a little noise. The clever part is that if you can reliably un-noise an image, you can start from *pure static* and un-noise your way to a picture that never existed.

Forward process — destroy structure (no learning)

Take a real image and add a small amount of Gaussian noise over many steps, following a fixed schedule, until after T steps nothing remains but static. Nothing is *learned* here — the forward process just defines what 'noisy at step t' means, and conveniently produces training pairs: a noisy image and the noise that was added to it.

Reverse process — the learned denoiser

A neural network (the denoiser, typically a U-Net) is trained to look at a noisy image at step t and predict the noise inside it. Subtract that prediction and you've stepped one notch back toward a clean image. Training is just regression: minimize the error between predicted and actual noise — a simple MSE loss.

# training one step (DDPM)
t        = random_step()
noise    = gaussian()
noisy    = add_noise(real_image, noise, t)   # forward: known recipe
pred     = denoiser(noisy, t)                # learn to predict the noise
loss     = mse(pred, noise)                  # that's the whole objective
Why predict the noise, not the image?
Predicting the added noise is an easier, better-behaved target than predicting the clean image directly, and it's equivalent to learning the score — the direction in pixel space that points toward more likely, cleaner images. Follow that direction repeatedly and structure emerges from chaos.

Sampling — generate from nothing

  1. 1Start from pure Gaussian noise — fresh random static.
  2. 2Ask the denoiser to predict the noise, subtract a step's worth.
  3. 3Repeat for many steps, walking from static toward a coherent image.
  4. 4Different starting noise → a different image every time.
Text-to-image
Condition the denoiser on a text prompt and each denoising step is steered toward matching the words — this is Stable Diffusion and DALL·E. A trick called classifier-free guidance sharpens prompt-following by mixing the model's conditional and unconditional noise predictions.
OperationTimeSpace
Training · MSE on predicted noise1 denoiser call per step
Sampling · many steps — slower than a GANO(T) network passesO(image)
Check yourself
During training, what does a diffusion model's network learn to predict?