Image generators learn one skill: spotting the noise in a noisy picture. Mix noise into a tiny 4-pixel image, then remove it step by step.
AI · beginner · 9 min · 23 steps · free
What you'll learn
Describe the forward process: adding noise step by step
Describe the reverse process: predicting and removing noise
Explain how starting from random noise gives new images
diffusion.py
# Diffusion models make images by learning to remove noise, a little at a time.
pixels = ["px 0", "px 1", "px 2", "px 3"]
image = [0.0, 1.0, 1.0, 0.0] # a tiny 4-pixel image: a bright bar in the middle
noise = [0.8, -0.6, 0.4, -0.9] # random noise (fixed here so the lesson repeats)
# Training pictures: noise mixed in, more at every step.
picture = image
for t in range(3):
picture = [round(p + 0.5 * n, 2) for p, n in zip(picture, noise)]
print("noisy:", picture)
# Generating: a trained network predicts the noise in the picture, and part of it is removed each step.
for t in range(3):
predicted = noise # a perfect network; real ones are close, not exact
picture = [round(p - 0.5 * n, 2) for p, n in zip(picture, predicted)]
print("cleaned:", picture)
Step by step
Names for the four pixels.
A tiny image: dark, bright, bright, dark. A bar of light in the middle.
Some random noise. Fixed here so every run is the same.
Start from the clean image.
t is created with the value 0.
Mix in half the noise. The bar is already harder to see.
t changes from 0 to 1.
More noise.
t changes from 1 to 2.
After three steps: [1.2, 0.1, 1.6, −1.35]. The bar is gone; it looks like noise.
Python moves to the next item of the loop: for t in range(3).
It prints noisy: [1.2, 0.1, 1.6, -1.35].
Now generate: run the process backwards.
A trained network looks at the noisy picture and predicts its noise. Here it is perfect; real ones are close.
Remove part of the predicted noise.
t changes from 0 to 1.
Line runs: predicted = noise # a perfect network; real ones are close, not exact.
picture now points to a different list: [0.4, 0.7, 1.2, -0.45].
t changes from 1 to 2.
Line runs: predicted = noise # a perfect network; real ones are close, not exact.
The last of the noise is removed.
Python moves to the next item of the loop: for t in range(3).
The bar is back: [0, 1, 1, 0]. A real generator starts from pure random noise, not a noised photo, so removing noise step by step produces a brand new image. A text prompt guides the noise predictor, which is how "a cat in a hat" becomes a picture.
Common mistakes
Thinking the model stores and copies training images: it learns to remove noise
Best practices
Remember that a text prompt steers which noise gets removed, which is how prompts shape the image
Key takeaways
Forward process: keep adding noise until the image is gone.
The network learns to predict the noise in a noisy image.
Generating: start from random noise and remove predicted noise step by step.
A text prompt steers the prediction, shaping what appears.
Check yourself
What does a diffusion model's network learn to predict?
Answer: The noise in a noisy image. Removing the predicted noise is how it cleans up a picture.
Why does generation start from random noise?
Answer: So each run produces a new image. Different starting noise leads to different images.