Lesson 8.9advanced4 h⚡ live demo

Generative models II: Diffusion and flow matching

Destroy data with noise, learn to reverse it — the math behind image and video models

Diffusion models power essentially every image, video and audio generator of the 2020s, and flow matching is their streamlined successor. The core is surprisingly small: a Gaussian forward process you can write in closed form, a denoising network trained with a mean-squared error, and a handful of sampling rules. The ELBO from 8.8 Generative models I: VAEs and GANs and the Gaussian algebra from 1.6 Probability I: random variables and distributions are all you need; the U-Net and transformer backbones come from 5.6 Segmentation and 7.4 Inside the block: residuals, LayerNorm, FFN and embeddings.

You'll be able to

  • Derive the closed-form forward process q(x_t | x_0) and explain noise schedules
  • Explain how the ELBO reduces to the simple ε-prediction loss and implement a training step
  • Write the DDPM and DDIM sampling updates and explain the score-matching view
  • Apply classifier-free guidance and explain latent diffusion and transformer backbones
  • State the flow-matching objective and why it became the default for new models

Key ideas

  1. Forward process: x_t = √ᾱ_t x_0 + √(1−ᾱ_t) ε in closed form, with ᾱ_t = Π(1−β_s) and x_T ≈ N(0, I).
  2. The ELBO reduces to a weighted MSE between posterior and predicted means; dropping weights gives L_simple = E‖ε − ε_θ(x_t, t)‖².
  3. DDPM sampling runs the stochastic reverse chain for ~1000 steps; DDIM's deterministic rule predicts x̂_0 and jumps, allowing 20–50 steps and reproducible samples.
Show 4 more key ideas
  1. ε_θ is a scaled score estimate; the SDE/probability-flow-ODE view unifies samplers and connects to flow matching.
  2. Classifier-free guidance extrapolates between unconditional and conditional predictions with weight w, trading diversity for prompt adherence.
  3. Latent diffusion runs in a VAE's compressed space; U-Nets gave way to Diffusion Transformers (DiT) as the backbone.
  4. Flow matching learns a velocity field along straight data–noise paths, v_θ ≈ ε − x_0, and is the default training objective for new models; distillation reduces sampling to 1–4 steps.

Suggested path through this lesson