liveBook · Manning

Diffusion model

As explained in Grokking AI Algorithms, Second Edition

A diffusion model is a generative model that learns to create structured data by reversing a gradual noise-addition process.

A diffusion model creates an image by starting with random noise and refining it step by step. Think of a screen full of TV static gradually becoming a recognizable picture. Rather than drawing each object onto a blank canvas, the model learns which noise to remove. section 12.1 section 12.2

From static to a tree

At first, the pixels look like a meaningless blur. After several denoising steps, a rough silhouette appears, though it could still be mistaken for something else. Later, a trunk and canopy become clear. Further steps sharpen the branches, bark, and leaves. This progression shows why an early result may have the right general shape but lack detail. section 12.2

The model learns this reversal by practicing on real images. During forward diffusion, a clean training image is combined with random noise at a chosen timestep. The resulting noisy image and the noise used to make it provide a training example. A noise schedule controls how much of the original image remains at each timestep; examples for different timesteps are calculated from the original image, not by repeatedly modifying the previous noisy example. section 12.9

Table 12.1 A subset of values for the noise schedule

Timestep (t)

Beta

Alpha calculation (1 - beta)

Alpha

Alpha-Bar calculation

Alpha-Bar

1

0.00010

1 - 0.00010

0.99990

0.99990

0.99990

2

0.00012

1 - 0.00012

0.99988

0.99990 × 0.99988

0.99978

3

0.00014

1 - 0.00014

0.99986

0.99990 × 0.99988 × 0.99986

0.99964

…

1000

0.02000

1 - 0.02000

0.98000

0.00004

The noise-schedule table shows beta, the per-step noise setting; alpha, the signal retained at a step; and alpha-bar, the cumulative signal retained by a timestep. section 12.9

A simple training and generation workflow is:

  • Prepare images and captions, convert them to numerical representations, and create noisy training examples. section 12.3
  • Train a U-Net to predict the noise in a noisy image, using its caption as a guide. Compare predicted noise with the actual noise, then update the model’s weights through backpropagation. section 12.3 section 12.23
  • To generate an image, start with fresh random noise and use the trained model to denoise it repeatedly, guided by a text prompt. section 12.23 section 12.3

Training longer is not always better. Stopping too early can leave generated images noisy or dull; training too long can cause the model to memorize its training images. Validation loss checks noise prediction on unseen examples, while Fréchet inception distance (FID) compares generated and real images. Checkpoints preserve promising model weights as these measures improve. section 12.30

How Grokking AI Algorithms, Second Edition covers it

How deep it goes
Traces forward noising, U-Net noise prediction, training updates, and iterative generation; also outlines schedules and training-quality checks.
Examples
Follows a noisy image becoming a detailed tree and gives a timestep table for a noise schedule.
What it assumes
Introduces denoising through a visual example before detailing the training workflow.
What you can do after
Build it from scratch as a small image-generation workflow, then assess training with validation loss, FID, and checkpoints.
How it connects
Connects diffusion to U-Nets, forward diffusion, noise schedules, backpropagation, text embeddings, and overfitting.

Also explained in

Compare how 10 books explain diffusion model →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking AI Algorithms, Second Edition. The section links point to the parts of the book it draws on.