IntermediateEnglish19 lectures · 2h 16m

DDPM and DDIM from Scratch

Derive and implement DDPM and fast DDIM sampling in PyTorch, from the paper to working code on MNIST

Watch 3 lectures free

01

What changes after this course

  • ●Instead of stopping at “it adds noise and removes it”, you read a diffusion paper and map its equations to code
  • ●Instead of treating 1000 sampling steps as a fixed cost, you cut them to 50 with DDIM on the same model
  • ●Instead of starting latent diffusion, DiT and SANA cold, you start from a DDPM you derived and trained

02

Why it matters now

Diffusion models power Stable Diffusion, DALL-E and Midjourney, and the sequel follows the same line to latent diffusion, DiT and SANA. Most tutorials stop at “it adds noise and removes it”, which is not enough to modify or train one. This course derives every equation on screen and turns it into code that trains on MNIST in minutes.

03

What you leave with

  • ✓Two notebooks: DDPM and DDIM
  • ✓A DDPM you trained that generates digits from pure noise
  • ✓A deterministic 50-step DDIM sampler

04

Skills you'll build

  • →Derive the closed-form noising formula and the ELBO, and simplify it to the noise-prediction loss
  • →Implement a noise scheduler, a time-conditioned U-Net and the training and sampling loops
  • →Cut sampling from 1000 steps to 50 with DDIM, using the same trained model

Inside the course

Screens from the lectures. The derivations, plots and samples are in the two notebooks you download, run in Jupyter.

Notebook derivation merging two Gaussian noise terms into the closed form x_t = sqrt(alpha-bar) x_0 + sqrt(1 - alpha-bar) epsilon

notebooks/01_DDPM.ipynb

The closed form, derived line by line

Two noising steps written out, the Gaussian terms merged, the variance simplified, then induction to any t. This is Equation (4) from the DDPM paper, and it is why training can jump straight to any noise level.

Lecture 1.4 · Forward process

Four plots comparing the linear and cosine noise schedules over 1000 timesteps

notebooks/01_DDPM.ipynb

Linear and cosine schedules, side by side

Both schedules implemented and plotted over 1000 timesteps: noise per step, signal kept per step, cumulative signal and total noise variance. Under the linear schedule the cumulative signal is close to zero well before the last step; the cosine one spends its steps more evenly.

Lecture 1.9 · Noise scheduler

An MNIST 5 at t = 0, 50, 100, 250, 500, 750 and 999, fading into noise, with the printed alpha-bar values

notebooks/01_DDPM.ipynb

A digit dissolving into noise

The forward process applied to a real MNIST digit with the closed form. The signal kept falls from 1.000 at t = 0 to about 0.49 at t = 500 and 0.000 at t = 999, where the image is indistinguishable from Gaussian noise.

Lecture 1.10 · Diffusion class

Eight MNIST-style digits generated by the trained DDPM: 5, 7, 2, 4 and four 1s

notebooks/01_DDPM.ipynb

Digits from the model you trained

After training the U-Net, the sampling loop turns noise into digits. Eight samples: a 5, a 7, a 2, a 4 and four 1s. Not every sample is clean, and you see that as it is.

Lecture 1.12 · Training and sampling

Table comparing DDPM and DDIM sampling: timestep movement, alpha values, noise variance, x0 prediction, determinism

notebooks/02_DDIM.ipynb

What DDIM changes in the sampler

DDPM steps from t to t-1; DDIM jumps from t to any earlier s with the same precomputed alphas. The noise variance becomes a dial, eta, and with eta = 0 sampling is deterministic.

Lecture 2.2 · DDIM sampling

Notebook output: DDIM with 51 steps printed as 19.6x, 0.010 s per image; with 21 steps printed as 47.6x, 0.003 s per image

notebooks/02_DDIM.ipynb

Fewer steps, same trained model

The DDPM model, sampled with DDIM in 51 and 21 steps instead of 1000. The notebook prints the step ratios as 19.6x and 47.6x; the measured time is 0.010 s and 0.003 s per image.

Lecture 2.3 · DDIM implementation

What you'll learn

  • ✓Explain the forward and reverse diffusion processes and derive the closed-form noising formula
  • ✓Derive the ELBO for diffusion models and simplify it to the DDPM noise-prediction loss
  • ✓Implement a noise scheduler, a time-conditioned U-Net, training and sampling loops in PyTorch
  • ✓Train a DDPM on MNIST and generate digits from pure noise
  • ✓Understand why DDIM sampling is non-Markovian and implement deterministic 50-step sampling
  • ✓Read a diffusion paper and map its equations to code

About this course

Diffusion models power Stable Diffusion, DALL-E and Midjourney — but most tutorials stop at "it adds noise and removes it". This course goes all the way: we read the DDPM paper (Ho et al., 2020) and the DDIM paper (Song et al., 2020) line by line, derive every equation on screen in plain language, and turn each one into PyTorch code that trains on MNIST in minutes.

You will build the forward diffusion process and its closed form, derive the reverse process and the evidence lower bound (ELBO) through Jensen's inequality, simplify it to the noise-prediction loss, implement the noise scheduler, a time-conditioned U-Net, the training loop and the sampling loop. Then you will add DDIM: the non-Markovian view of diffusion that turns 1000 sampling steps into 50 with the same trained model.

Every lecture shows the notebook on screen while the narration explains what each cell does and why. Failure modes, trade-offs and "what to try if this breaks" are part of the lesson. By the end you can read a diffusion paper, explain it to a colleague, and implement it.

This is the first of two courses. The sequel, *Latent Diffusion to SANA from Scratch*, continues with Stable Diffusion, Diffusion Transformers, PixArt-α and SANA.

Curriculum3 sections · 19 lectures · 2h 16m

Section 0. Welcome

Free preview

Section 1. DDPM

Free preview
  • ▶section 1 intro0:18
  • ▶DDPM chapter intro2:00
  • 🔒Environment setup2:42
  • 🔒Notation6:24
  • 🔒Forward process7:18
  • 🔒Reverse process8:42
  • 🔒Jensen inequality6:00
  • 🔒ELBO4:00
  • 🔒From ELBO to the loss10:24
  • 🔒Noise scheduler10:42
  • 🔒Diffusion class14:18
  • 🔒UNet17:48
  • 🔒Training and sampling12:24
  • 🔒DDPM wrap up3:06

Section 2. DDIM

  • 🔒section 2 intro0:18
  • 🔒DDIM intro and differences6:18
  • 🔒DDIM sampling9:42
  • 🔒DDIM implementation11:48

Requirements

  • · A computer with Python 3.10+ (a free Google Colab account is enough for most sessions)
  • · Comfort reading Python code; you do not need to be an expert
  • · Basic PyTorch (tensors, autograd, nn.Module, a training loop)
  • · Basic probability: Gaussian distributions, expectation, Bayes' rule; a little calculus

Who this is for

  • · ML engineers and data scientists who use diffusion models and want to understand them
  • · Graduate students and researchers preparing to read or write diffusion papers
  • · Developers who can run Stable Diffusion but cannot modify or train it
  • · Anyone who prefers derivations and code over hand-waving

Read alongside the course

The voice-over in this course is synthesized with a text-to-speech model from scripts written and reviewed by the instructor, and the on-screen material (notebooks, code, slides) is the instructor's own work.

DDPM and DDIM from Scratch | SOTAAZ Blog