Diffusion Models
The Core Idea
Diffusion models work by:- Forward process: Gradually add noise to data until it becomes pure noise
- Reverse process: Learn to denoise step by step, recovering the original data
A senior engineer would frame it this way: “Diffusion models decompose one impossibly hard problem — generate a realistic image from nothing — into a thousand easy problems: remove a tiny bit of noise. Each sub-problem is a simple regression task. The genius is in the decomposition, not the network architecture.”
Mathematical Foundation
Forward Diffusion (Adding Noise)
At each step , we add a small amount of Gaussian noise: Where is the noise schedule — a small positive number (typically between 0.0001 and 0.02) that controls how much noise is added at step . The factor slightly shrinks the signal while controls the noise variance. Over many steps, the signal is completely destroyed. The key mathematical trick: we do not need to run all steps sequentially. We can jump directly to any timestep in closed form: Where and (the cumulative product of all alphas up to step ). Intuition: decays from nearly 1 (almost clean image) to nearly 0 (almost pure noise). So is just a weighted blend of the original image and random noise. Early timesteps are mostly signal; late timesteps are mostly noise.Reverse Process (Learning to Denoise)
We train a neural network to predict the noise that was added at step : Read this carefully — the loss is beautifully simple. We take a clean image , pick a random timestep , add known noise to get , then ask the network “what noise was added?” The loss is just MSE between the actual noise and the predicted noise. No adversarial training, no complex objectives — just noise prediction. Why predict noise instead of the clean image? Empirically, noise prediction gives more stable gradients. Intuitively, predicting noise is a “residual” task — the network only needs to learn what was added, not reconstruct the entire image from scratch. This is the same insight that makes ResNets work.Training Loop
The training algorithm is refreshingly simple compared to GANs — no adversarial balancing, no mode collapse to worry about. Each iteration samples a random timestep, corrupts a clean image to that noise level, and asks the network to predict what noise was added.Sampling (Generation)
Sampling is the reverse of the forward process: start from pure Gaussian noise and iteratively denoise. Think of it like developing a Polaroid photo — the image gradually emerges from chaos, with coarse structure appearing first (large-scale shapes and colors) and fine details (textures, edges) filling in during the last steps.Classifier-Free Guidance
Classifier-free guidance (CFG) is the mechanism that lets you steer generation with a text prompt (or class label). The intuition is surprisingly elegant: during training, the model randomly drops the conditioning signal some fraction of the time (say 10%), so it learns both conditional and unconditional generation. At inference, you run the model twice — once with your prompt and once without — and amplify the difference. Think of it like asking for directions. The unconditional prediction says “go vaguely north.” The conditional prediction (with your prompt) says “go northeast toward the bakery.” Guidance amplifies the difference: “go VERY northeast toward the bakery.” Higher guidance scale means stronger adherence to the prompt, at the cost of reduced diversity. Where is the guidance scale (typically 7.5 for Stable Diffusion). At you get the raw conditional model; at you get unconditional generation. Values above 7-8 tend to produce over-saturated, artifact-heavy images — a common beginner mistake is cranking guidance to 20+ and wondering why the outputs look “deep-fried.”Connection to Stable Diffusion
Running diffusion directly on 512x512 pixel images is absurdly expensive — the U-Net would need to process 786,432 values per image at every timestep. Stable Diffusion’s key insight is to run the entire diffusion process in a compressed latent space instead. This is like editing a blueprint instead of rebuilding the house for every revision. Stable Diffusion operates in latent space for efficiency:- VAE Encoder: Compress 512x512 image to 64x64 latent (64x spatial compression)
- U-Net: Denoise in latent space (operating on 4,096 values instead of 786,432 — roughly 192x cheaper)
- VAE Decoder: Expand latent back to full-resolution image
- CLIP Text Encoder: Convert text prompts into conditioning vectors that guide the denoising
Pitfall — VAE quality ceiling: Because the final image must pass through the VAE decoder, the VAE’s reconstruction quality puts a hard ceiling on output fidelity. Fine details that the VAE cannot reconstruct will never appear in generated images, no matter how good your diffusion model is. This is why newer versions of Stable Diffusion ship with improved VAE decoders.
Exercises
Exercise 1: MNIST Diffusion
Exercise 1: MNIST Diffusion
Train a diffusion model on MNIST. Generate digit samples and visualize the denoising process.
Exercise 2: Noise Schedules
Exercise 2: Noise Schedules
Implement and compare linear, cosine, and quadratic noise schedules.
Exercise 3: Conditional Diffusion
Exercise 3: Conditional Diffusion
Add class conditioning to generate specific digits.
What’s Next
Module 15: Residual & Skip Connections
Learn how to train very deep networks with identity mappings.