Maps to: UC Berkeley CS294-158 Deep Unsupervised Learning (Spring 2024), Lecture 6 · Thu 2/22/2024 · Diffusion Models, first part (slides 1–40 and 68–95): motivation (DALL·E 2, Sora, UniSim), the state of the field in 2020, denoising diffusion probabilistic models (forward and reverse process, the variational bound, the simplified loss), the DDPM architecture, the key inspirations (nonequilibrium thermodynamics; score matching and annealed Langevin dynamics), “diffusion models beat GANs” (learned variance, architecture improvements, classifier guidance), classifier-free guidance (training, sampling, the FID / IS trade-off), the DDIM sampler, prediction targets (, , ), the problem with common noise schedules (zero terminal SNR), and the U-Net and its conditioning. Homework 4, Questions 1–2. Basis: The slides give the algorithms and results. These notes derive the forward marginals, the posterior of a reverse step, the variational bound and how it becomes “predict the noise”, Tweedie’s formula, DDIM, the probability-flow ODE and the guidance rules. Then they check everything against exact optimal denoisers: for a 2-D mixture of 402 Gaussians and for ShapeWorld itself, can be computed in closed form — so every trained network, every sampler and every guidance rule is compared with the best possible one. Code:
code/lecture11/(4 scripts +diffusion.py; PyTorch (CPU); scripts 01–02 take 1–2 minutes, 03–04 ~45–60 minutes the first time (U-Nets cached)) Reading time: 4 hours including running the code
0. How to read this lecture
0.1 Learning goals
After this lecture you should be able to:
- Write the forward process of a diffusion model, its closed-form marginals , and the signal-to-noise ratio of a schedule.
- Derive the posterior , the variational bound, and why it reduces to a (re-weighted) noise-prediction loss.
- Explain why the reverse step can be modelled as a Gaussian only when steps are small — and measure it.
- State Tweedie’s formula: the optimal noise predictor is the scaled score , and the optimal denoiser the posterior mean.
- Implement DDPM and DDIM sampling and the probability-flow ODE, and say what each trades.
- Compare the -, - and -parameterizations and explain zero terminal SNR.
- Derive classifier guidance and classifier-free guidance, and explain their fidelity / diversity trade-off.
- Distinguish a diffusion model that generalizes from one that memorizes its training set.
0.2 Prerequisites and scope
| Assumed | Introduced here | Developed later |
|---|---|---|
| VAEs and the ELBO (Lectures 7–8) | forward / reverse processes, DDPM, the simple loss | latent diffusion, Diffusion Transformers, text-to-image (Lecture 12) |
| continuous flows and the change of variables (Lecture 6) | DDIM, the probability-flow ODE | fast sampling: distillation, adversarial diffusion (Lecture 12) |
| GANs, FID and precision / recall (Lectures 9–10) | score functions, Tweedie’s formula, guidance | video diffusion (Lecture 17) |
0.3 Map
§1 why diffusion? the state of the field in 2020
§2 the forward process and noise schedules ← code 01, Part 1
§3 the reverse process: Gaussian steps, the ELBO, "predict the noise" ← code 01, Part 2
§4 scores and the optimal denoiser (Tweedie); the inspirations
§5 sampling: DDPM, DDIM, the probability-flow ODE ← code 01, Parts 3-4
§6 training a denoiser: homework 4 Q1 ← code 02, Parts 1-3
§7 what to predict: ε, x_0 or v -- and zero terminal SNR ← code 02, Part 4
§8 the U-Net, and pixel diffusion on ShapeWorld: generalizing vs memorizing ← code 03
§9 guidance: classifier guidance, classifier-free guidance ← code 04
§10 summary §11 pitfalls §12 exercises §13 further reading §14 next lecture
0.4 Notation
| Symbol | Meaning |
|---|---|
| ; | data; the noised version at time |
| , | signal and noise scales, ; homework 4: , , |
| , | DDPM’s discrete schedule: , so , |
| signal-to-noise ratio; | |
| , , | a network’s predictions; |
| , | the optimal predictions , |
| sampler noise: 1 = DDPM, 0 = DDIM | |
| guidance scale |
0.5 How the experiments are run
- 2-D data with an exact denoiser. Lecture 5’s smiley drawn as a mixture of 402 small Gaussians (standard deviation 0.1). Every noised version is again a Gaussian mixture, so , and even the exact reverse step are closed-form (
diffusion.GMM2D). - ShapeWorld with an exact denoiser. Every panel is one of 960 known images and the two panels are coupled only through the shape kinds, so under the true distribution is a sum over 960 × 8 terms per panel (
diffusion.ShapeWorldDenoiser); under the training set it is a softmax over 10,000 images (EmpiricalDenoiser). - Homework 4’s continuous-time setup: cosine schedule, , the DDPM sampler from to , Adam with 100 warmup steps and cosine decay. Images live in ; samples are thresholded at 0 and judged with Lecture 9’s exact yardsticks and scores.
- Everything is seeded and single-threaded; reruns print identical numbers.
1. Why diffusion?
The slides take stock of the course in 2020. Autoregressive models and flows gave exact likelihoods; latent variable models gave approximate ones and representations; GANs gave the most realistic images “but have lots of bells and whistles and struggle with truly covering what’s in the data distribution” — Lectures 9–10 measured both halves of that sentence: the ring’s dropped modes, the oscillating games, the sharp but never-valid ShapeWorld samples.
Denoising diffusion probabilistic models (Ho, Jain & Abbeel, 2020) combined the strengths: trained by a likelihood bound (stable, no game, mode-covering), with sample quality that soon passed GANs (Dhariwal & Nichol, 2021). By 2024 they drive text-to-image (DALL·E 2/3, Imagen, Stable Diffusion), video (Sora: a cat waking its owner for breakfast, from a paragraph of text) and even world models for robots (UniSim: imagining the results of actions).
The idea in one sentence: destroy data gradually with noise, and learn to undo one small step of destruction at a time. Each small undo is easy (§3 shows how easy, exactly); chaining a thousand of them turns noise into data.
2. The forward process
2.1 Adding noise in many small steps
DDPM defines a Markov chain that adds a little Gaussian noise at each of steps:
with a variance schedule (linear). The shrinks the signal so that the total variance stays bounded (“variance preserving”). Because Gaussians compose, any step can be reached in one shot (Exercise 1):
i.e. with , . This is what makes training cheap: no need to simulate the chain — pick a time, noise the data directly.
2.2 Schedules in continuous time
Homework 4 uses continuous time and specifies directly, here the cosine schedule , (). What matters about a schedule is how the signal-to-noise ratio falls with — how much of the network’s capacity is spent on each noise level — and where it ends.
Experiment (script 01, Part 1). The smiley under the cosine schedule, with the exact KL from to :
| log SNR (cosine) | log SNR (DDPM linear, ) | KL() | ||
|---|---|---|---|---|
| 0.1 | 0.988 | +3.7 | +2.2 | 0.695 |
| 0.2 | 0.951 | +2.2 | +0.7 | 0.185 |
| 0.4 | 0.809 | +0.6 | −1.4 | 0.033 |
| 0.6 | 0.588 | −0.6 | −3.6 | 0.006 |
| 0.8 | 0.309 | −2.2 | −6.5 | ≈ 0 |
| 1.0 | 0 | −∞ | −10.1 | ≈ 0 |
The smiley is unrecognizable (KL < 0.04 nats) by . DDPM’s linear schedule is noisier at every intermediate — and does not end at pure noise: , i.e. of the signal survives (log SNR −10.1). §7.2 explains why that matters.
3. The reverse process
3.1 The posterior of one step is Gaussian
Going backwards, is intractable (it depends on the whole data distribution), but conditioned on it is a Gaussian (Exercise 2):
DDPM models the reverse step as a Gaussian too, , with a fixed variance ( or ). The slides call the full reverse model “conceptually helpful; but we never explicitly represent ”: what is learned is a denoiser.
3.2 Why may the reverse step be Gaussian?
The true is a mixture of the Gaussians above, weighted by how likely each is given . If the step is small, is pinned close to whatever is, and the mixture is nearly Gaussian; for big steps it can have many modes. (Feller’s classic result: the reverse of a diffusion with small enough steps has the same functional form.)
Experiment (script 01, Part 2). For the smiley the exact reverse step is computable. KL in nats between it and the best single Gaussian (moment-matched), averaged over :
| step | 0.01 | 0.05 | 0.1 | 0.2 | jump to the data | |
|---|---|---|---|---|---|---|
| 0.3 | 0.0000 | 0.0000 | 0.0040 | 0.0408 | 0.4507 | 1.143 |
| 0.6 | 0.0000 | 0.0000 | 0.0001 | 0.0005 | 0.0063 | 1.377 |
| 0.9 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0002 | 1.428 |
With DDPM’s step size () the Gaussian assumption is exact to four decimals. Near the data (small ) larger steps quickly become non-Gaussian; at high noise even is fine. This is why sampling needs many steps — and why later methods replace large Gaussian steps by GANs or distilled models (Lecture 12).
3.3 The variational bound, and “predict the noise”
A diffusion model is a latent variable model (Lecture 7) whose latents are and whose “encoder” is the fixed forward process. Its ELBO splits into one term per step (Exercise 3):
Each KL between Gaussians with equal variance is a squared distance between means: . Ho et al. parameterize the mean through a noise prediction: since ,
Dropping the weights gives the simple loss the slides call the variance-reduction version:
Training: pick an image, a time and a noise; noise the image; regress the noise. No game, no encoder, no posterior collapse. The re-weighting de-emphasizes the low-noise steps, which dominate the bound but matter little for sample quality; it makes the likelihood worse and the samples better (Ho et al. report both).
4. Scores, the optimal denoiser, and the inspirations
What does a perfectly trained network output? The loss is a squared error, so the minimizer is the conditional expectation:
Tweedie’s formula connects it with the score of the noised distribution (Exercise 4):
A noise predictor is a score estimator. That links DDPM to its two inspirations on the slides:
- Nonequilibrium thermodynamics (Sohl-Dickstein et al., 2015) introduced diffusion models: a forward diffusion that slowly destroys structure and a learned reverse diffusion, trained with the same variational bound.
- Score matching and annealed Langevin dynamics (Song & Ermon, 2019; NCSN): learn for a ladder of noise levels by denoising score matching (which is exactly the loss above), then sample with Langevin dynamics, , from high noise down to low. “Very promising sample generation” — and the score view leads to the SDE / ODE formulation used in §5.3.
For ShapeWorld and for the 402-Gaussian smiley, is a finite sum, so these scripts can sample with the optimal denoiser and compare it with every trained one.
5. Sampling
5.1 DDPM: ancestral sampling
Start from and repeatedly draw : predict the noise, form the mean, add fresh Gaussian noise. One network evaluation per step, 1,000 steps for the original DDPM.
5.2 DDIM: the same marginals, fewer and deterministic steps
Song, Meng & Ermon (2021) noticed that the training loss only involves the marginals , and many processes share them. Writing the predicted clean image , a whole family of updates is consistent with the same marginals (Exercise 5):
gives DDPM’s ancestral step (this is homework 4’s formula); gives DDIM: deterministic — “move to the next noise level and add back the predicted noise”. Two consequences from the slides: the step size at inference can differ from training (most models give good samples with ~50 steps), and since noise maps deterministically to an image, images can be inverted to noise (used for editing in Lecture 12).
5.3 The probability-flow ODE: a diffusion model is a flow
As the number of DDIM steps grows, the updates become an ordinary differential equation that carries to (Song et al., 2021). With the angle of the cosine schedule () it is remarkably simple: . This is a continuous normalizing flow (Lecture 6): its log-likelihood is the prior’s plus the integrated divergence of the velocity field, computed exactly in 2-D with automatic differentiation.
5.4 Experiments (script 01, Parts 3–4): sampling with the optimal denoiser
No learning at all — the exact of the smiley — and different numbers of steps. “(sample)” is the true negative log-density of the generated points (for perfect samples it equals the entropy, 1.580); “off” counts samples in regions where real data are rare (1% for real samples):
| steps | sampler | off the drawing | sliced | |
|---|---|---|---|---|
| 2 | DDPM / DDIM | 9.76 / 10.20 | 58% / 60% | 0.43 / 0.43 |
| 5 | DDPM / DDIM | 3.19 / 3.29 | 20% / 20% | 0.23 / 0.18 |
| 10 | DDPM / DDIM | 1.43 / 1.39 | 4.1% / 3.1% | 0.13 / 0.10 |
| 20 | DDPM / DDIM | 1.18 / 1.36 | 0.5% / 0.9% | 0.097 / 0.065 |
| 50 | DDPM / DDIM | 1.29 / 1.49 | 0.1% / 0.9% | 0.046 / 0.047 |
| 200 | DDPM / DDIM | 1.49 / 1.55 | 0.5% / 0.7% | 0.054 / 0.042 |
| — | real data | 1.580 | 1.0% | 0.049 |

- Even a perfect denoiser needs steps. With 2–5 steps samples are smeared between the strokes (the figure); from 20–50 steps on they match the data (sliced at the real-vs-real floor). This error is pure discretization — the large-step non-Gaussianity of §3.2.
- DDIM is better at few steps (sliced 0.065 vs 0.097 at 20 steps): it adds no fresh noise that later steps must remove.
- DDPM with few steps is under-dispersed: its samples have lower than real data (1.18 at 20 steps vs 1.58) — they crowd the centers of the strokes. The fixed variance of each step ignores the uncertainty about ; Improved DDPM (Nichol & Dhariwal, 2021) learns the variance for this reason (“learned variance” on the slides). With 200 steps the effect fades.
The ODE likelihood of the exact model, 200 test points: 1.6631 nats with 200 RK4 steps vs the exact 1.6631 (largest per-point difference 0.0008; 0.013 with 50 steps, 2.3 with 10). A diffusion model has an exact likelihood — through its ODE.
6. Training a denoiser (homework 4 Q1)
Setup (script 02). An MLP with 4 hidden layers of 64 units on , trained on 100,000 points of the smiley with batch 1,024 for 6,000 steps (homework 4’s recipe). Because the exact is known, the loss can be split into the irreducible part (the optimal denoiser’s error) and the network’s excess error:
| optimal | network | excess | |
|---|---|---|---|
| 0.02 | 1.888 | 1.995 | +0.107 |
| 0.05 | 1.528 | 1.949 | +0.421 |
| 0.10 | 1.149 | 1.863 | +0.715 |
| 0.20 | 1.386 | 1.709 | +0.323 |
| 0.40 | 1.297 | 1.307 | +0.009 |
| 0.60 | 0.751 | 0.755 | +0.004 |
| 0.95 | 0.014 | 0.015 | +0.001 |
Most of the loss is irreducible: even the optimal denoiser cannot tell which nearby point of the drawing produced a noisy observation. The network is near-optimal at high noise and far from it at low noise ( = 0.05–0.2), where the strokes (0.1 wide) must be resolved — invisible in the training curve, which averages over .
Samples (Part 2): with the same samplers, the network is much worse than the optimal denoiser — about half of its samples fall off the drawing even with 200 DDPM steps, and DDIM with few steps explodes ( in the thousands). §7 finds the cause.
Likelihood through the ODE (Part 3, 300 test points; exact: 1.509 nats): the -network 2.773 (+1.26), the -network 1.671 (+0.16), the -network 607 — useless (§7).

7. What to predict, and where the schedule ends
7.1 , or
Any one of , or determines the others given (with : , ). They differ in conditioning. The slides motivate geometrically: is a point on the unit circle, rotates from to as the angle grows, and is the velocity of that rotation, . Progressive distillation (Salimans & Ho, 2022) introduced it because the others break at one end:
- -prediction at high noise: divides by ;
- -prediction at low noise: divides by ;
- -prediction is a rotation of — bounded at both ends.
Experiment (script 02, Part 4). The same MLP, trained on each target; errors against the optimal denoiser after converting to and :
| prediction | : err / err | ||||
|---|---|---|---|---|---|
| 0.028 / 7e-8 | 0.42 / 0.0026 | 0.0055 / 0.0055 | 0.0012 / 0.19 | 0.0013 / 51,000 | |
| 390 / 0.0010 | 0.51 / 0.0032 | 0.0006 / 0.0006 | 5e-6 / 0.0008 | 7e-11 / 0.0027 | |
| 0.29 / 7e-7 | 0.027 / 0.0002 | 0.0010 / 0.0010 | 2e-6 / 0.0004 | 2e-11 / 0.0009 |
| prediction | DDIM 50 steps: / off / sliced | DDPM 200 steps |
|---|---|---|
| 607 / 92% / 1.84 | 7.14 / 51% / 0.097 | |
| 7.18 / 51% / 0.099 | 6.47 / 44% / 0.109 | |
| 2.15 / 8.6% / 0.033 | 1.84 / 4.6% / 0.047 |
The -network’s error near is tiny (0.0013), but the sampler’s first step from turns it into an error of 51,000 (). The -network’s explodes near (which ruins its ODE likelihood). The -network is well-behaved at both ends and — same architecture, same budget — gives samples at the real-data noise floor. For images, homework 4’s remedy for -prediction is to clip to the data range at every step, which removes the explosion (script 03 does so).
7.2 Zero terminal SNR
Lin et al. (2024): most schedules do not reach pure noise at — Stable Diffusion’s ends at ; DDPM’s linear one at (§2). Two problems follow. During training the “noisiest” inputs still leak global information about (e.g. its mean brightness), and the network learns to use it; at sampling time we start from pure , where there is no such signal, and the model reads signal into noise — Stable Diffusion could not make very dark or very bright images. The fix: rescale the schedule so that exactly. But at the -target is trivial (, the network learns the identity) and is undefined — the row of the table above taken to its limit. -prediction to the rescue: at , , a meaningful target (“predict the mean image”). The slides’ recipe: rescale the schedule to zero terminal SNR and fine-tune with the -loss. Homework 4’s cosine schedule already ends at .
8. The U-Net, and pixel diffusion on ShapeWorld
8.1 Architecture
DDPM’s backbone is PixelCNN++‘s U-Net (a Wide ResNet): residual blocks at several resolutions (32×32 down to 4×4 for CIFAR), downsampling and upsampling paths connected by skip connections, group normalization, self-attention at 16×16, and the time injected into every residual block through a sinusoidal embedding (as for Transformer positions) and a small MLP. Two ways to inject a conditioning vector (time, class, later text): add it to the feature map (homework 4’s U-Net), or use it to produce a per-channel scale and shift (adaptive group norm / FiLM), which works better (Dhariwal & Nichol). The CIFAR-10 model had 35.7 million parameters; the LSUN / CelebA-HQ models 114–256 million. Dhariwal & Nichol’s architecture improvements: more attention heads and resolutions, BigGAN-style residual up/downsampling, adaptive group norm.
8.2 Experiment: between the truth and the training set (script 03)
Setup. Homework 4 Q2’s U-Net, small: channels (16, 32) at 12×24 and 6×12, one residual block per resolution, time embedding added in every block — 160,000 parameters; -prediction, batch 64, 10,000 steps (~45 minutes on one CPU thread). Sampling: DDPM, 100 steps, clipped to . The two exact denoisers are used with the same sampler.
Part 1 — samples (2,000 each):
| denoiser | valid | novel valid | flips | copy rate | IS | FID | precision | recall |
|---|---|---|---|---|---|---|---|---|
| exact, for the true | 100% | 87.5% | 0.00 | 0.57 | 7.88 | 406 | 0.95 | 0.95 |
| exact, for the training set | 100% | 0.0% | 0.00 | 0.57 | 7.89 | 364 | 0.95 | 0.93 |
| U-Net (160k parameters) | 30.4% | 28.6% | 6.2 | 0.23 | 7.57 | 1,863 | 0.90 | 0.94 |
(real data: 100% valid, 87% novel, copy rate 0.5625, FID between two real sets 323–437.)
![]()
- The optimal denoiser for the true distribution generates the true distribution: every sample valid, 87.5% new (real data: 87%), the copy rule right (0.57).
- The optimal denoiser for the training set reproduces the training set: every sample valid and none new — and the better FID of the two (364): FID cannot tell (Lecture 9). Minimizing the training loss perfectly is memorization.
- The U-Net sits in between, on the right side: 30% of its samples are valid ShapeWorld images, nearly all of them new; 6 flips on average. By FID (1,863) and precision / recall (0.90 / 0.94: no mode-seeking) it beats every GAN (Lectures 9–10: FID ≥ 14,893, recall ≤ 0.37) and the VAE, and it beats the 18-token VQ-VAE + Transformer of Lecture 8 in validity (26%). Like Lecture 3’s convolutional models it misses the long-range copy rule (0.23 vs 0.56).
Part 2 — between the two optima. For 300 noised test images, the squared distance (per image, pixels in ) between the U-Net’s and each optimal denoiser:
| U-Net vs truth’s optimum | U-Net vs training set’s optimum | training set’s vs truth’s optimum | |
|---|---|---|---|
| 0.1 | 0.03 | 62.2 | 62.1 |
| 0.3 | 0.24 | 61.5 | 61.6 |
| 0.5 | 3.0 | 59.8 | 61.4 |
| 0.7 | 28.0 | 51.9 | 45.7 |
| 0.9 | 11.2 | 11.8 | 0.6 |
On test images the memorizing optimum snaps to the nearest training image (distance ~62 from the truth at low noise), while the U-Net agrees with the true optimum almost exactly at low noise (0.03 at ): it has learned to denoise shapes, not to recall images — it generalizes. Its error is concentrated at mid noise ( ≈ 0.7), the level where the image’s identity (which shapes, where) is decided; mistakes made there are what the low-noise steps later render as crisp but invalid shapes.
Part 3 — sampling steps (500 samples):
| steps | 10 | 20 | 50 | 100 | 200 |
|---|---|---|---|---|---|
| DDPM: valid / flips | 16.4% / 7.3 | 26.2% / 6.4 | 29.6% / 6.2 | 31.8% / 6.1 | 32.0% / 6.5 |
| DDIM: valid / flips | 6.4% / 11.1 | 7.6% / 10.8 | 9.0% / 10.5 | 9.0% / 10.0 | 12.2% / 8.9 |
With the exact denoiser DDIM was the better sampler at few steps (§5.4); with an imperfect network it is much worse here (9% vs 30% valid at 50–100 steps). The fresh noise of each DDPM step lets later steps correct an earlier mistake; DDIM’s deterministic path commits to it. (Karras et al., 2022, make the same observation and add controlled “churn” noise to deterministic samplers.)
Plan vs reality. The first version of this experiment trained the U-Net for 4,000 steps, and it gave 1.8% valid samples (FID 3,577; DDIM 0.6%). Training 2.5× longer — the same model, recipe and seed — raised validity to 30%. The loss had looked converged (0.0245 at both 3,200 and 4,000 steps, but the cosine learning-rate schedule was already near 0); the samples were not. Undertrained diffusion models look almost right; a pixel-exact criterion makes the difference visible.
9. Guidance
9.1 Classifier guidance
Dhariwal & Nichol (“Diffusion models beat GANs on image synthesis”, 2021) added a term to the sampler that steers each step towards a class . By Bayes’ rule, , so with a classifier trained on noisy images :
samples ; samples — a sharper, “lower temperature” distribution that favours images the classifier is sure about. With –10, guided ImageNet models matched BigGAN’s image quality with better diversity (higher recall).
9.2 Classifier-free guidance
Ho & Salimans (2022): can we get the sharpening without training a separate noisy classifier? Bayes again: , and both terms are diffusion models. Train one network on both: replace the label by a “null” token 10–20% of the time (“a one-line change of code during training”). At sampling time mix the two predictions:
(: unconditional, : conditional, : guided; the slides write the same thing as ). The slides’ plot of FID and IS against : IS keeps improving (each sample a more “realistic member of its class”), FID improves briefly and then gets worse (diversity is lost). GLIDE, DALL·E 2, Imagen and Stable Diffusion all use it (Lecture 12).
9.3 Experiment (script 04)
Class = the kind of the left shape. 100 samples per class (800), DDPM 50 steps. “Requested shape” and “right = requested” are read exactly from valid samples; in the true conditional distribution they are 100% and 0.5625.
Part 1 — classifier guidance with the exact model. The exact unconditional denoiser plus the exact noisy classifier (both from ShapeWorld’s tables): .
| scale | valid | requested shape | right = requested | novel valid | distinct | FID | precision | recall |
|---|---|---|---|---|---|---|---|---|
| 0 (unconditional) | 100% | 13.6% | 0.13 | 89.8% | 796 | 973 | 0.94 | 0.94 |
| 1 | 100% | 100% | 0.56 | 88.2% | 795 | 665 | 0.95 | 0.93 |
| 2 | 100% | 100% | 0.70 | 80.1% | 780 | 2,508 | 0.96 | 0.93 |
| 4 | 100% | 100% | 0.80 | 66.1% | 728 | 9,026 | 0.97 | 0.88 |
At , Bayes’ rule on the score is exact: the samples are — the requested shape every time, the right panel copying it at exactly the data’s rate, novelty unchanged. Above 1, every sample is still valid and of the right class, yet the distribution is distorted: guidance favours images that are “most clearly class ”, and in ShapeWorld the clearest evidence for the left shape is the right panel showing it too (copy rate 0.56 → 0.80). Novelty falls and FID rises tenfold. Here the trade-off is not “quality vs diversity” — quality is perfect throughout — it is typicality vs faithfulness to the distribution.
Part 2 — classifier-free guidance with a U-Net. Lecture 11’s U-Net with a class embedding added to the time embedding, trained 10,000 steps with the class dropped 10% of the time; :
| scale | valid | requested shape | right = requested | novel valid | FID | precision | recall |
|---|---|---|---|---|---|---|---|
| 0 (unconditional) | 23.9% | 11.5% | 0.15 | 22.8% | 3,453 | 0.90 | 0.92 |
| 1 (conditional) | 25.9% | 88.9% | 0.60 | 23.2% | 1,796 | 0.92 | 0.91 |
| 2 | 27.0% | 98.1% | 0.90 | 22.2% | 2,000 | 0.95 | 0.85 |
| 3 | 26.9% | 99.1% | 0.94 | 21.4% | 4,454 | 0.93 | 0.82 |
| 5 | 21.9% | 100% | 0.98 | 14.6% | 9,150 | 0.92 | 0.73 |
| 7 | 20.0% | 100% | 0.97 | 12.5% | 12,396 | 0.91 | 0.69 |

- The slides’ FID curve, measured: from to 1, FID improves (3,453 → 1,796 — the conditional model is simply the better model) and the requested shape goes from chance to 89%. Beyond, class adherence saturates at 100% while FID worsens (12,396 at ) and recall falls (0.91 → 0.69): diversity is traded for class-typicality.
- The conditional model learned the copy rule (0.60 at , vs 0.15 unconditionally — and vs 0.23 for Lecture 11’s unconditional U-Net): telling the network the left shape makes the long-range dependency easy. Guidance then overshoots it (0.90 at , 0.98 at ), more than exact guidance does (0.70, 0.80): the learned conditional over-weights the class for the right panel too.
- Validity barely depends on guidance (20–27%): guidance makes samples more class-typical, not more correct. Pixel-level errors are the network’s, and no sampling trick removes them.
10. Summary
- A diffusion model destroys data with noise in closed form () and learns to undo one small step at a time; training is regression of the noise, a re-weighted variational bound.
- The true reverse step is Gaussian for small steps and multimodal for large ones (KL to the best Gaussian: 0.0000 at , 0.45 at near the data) — why sampling needs many steps.
- The optimal noise prediction is the scaled score, (Tweedie); the optimal denoiser is the posterior mean — computable exactly for a Gaussian mixture and for ShapeWorld.
- Even the optimal denoiser needs ~20+ steps; DDIM beats DDPM at few steps with the exact model; DDPM with few steps under-disperses (learned variances, Improved DDPM). The probability-flow ODE is a flow whose likelihood matched the exact density to 0.0008 nats.
- A trained network is near-optimal at high noise and far off at low noise in 2-D; its error is invisible in the average loss.
- -prediction explodes near ( divides by : error 51,000), -prediction near ; -prediction is well-conditioned at both ends and gave samples at the noise floor. Schedules should reach zero terminal SNR.
- On ShapeWorld the exact denoiser for the truth generates the truth (100% valid, 87.5% new); the exact denoiser for the training set reproduces it (100% valid, 0% new, better FID). A 160k-parameter U-Net (10,000 steps) sits in between on the side of the truth: 30% valid, nearly all new, FID 1,863, precision / recall 0.90 / 0.94 — better than every GAN and the VAE — and agrees with the true optimum on test images at low noise. Its errors live at mid noise. A 4,000-step version gave only 1.8% valid samples.
- For an imperfect network DDPM beats DDIM (32% vs 9% valid): fresh noise lets later steps correct mistakes.
- Guidance: exact classifier guidance at scale 1 samples exactly; larger scales keep samples valid but distort the distribution (copy rate 0.56 → 0.80, novelty 88% → 66%). Classifier-free guidance improves FID from = 0 to 1 (3,453 → 1,796), then trades diversity for class-typicality (recall 0.91 → 0.69 at ) without making samples more valid.
11. Common pitfalls
- Reading the training loss as sample quality. Most of a diffusion loss is irreducible (the optimal denoiser’s error is 1.9 at small in script 02), and it averages over : a network can be near-optimal at high noise and far off at low noise without the curve showing it.
- Starting the sampler at exactly with the cosine schedule: and is undefined. Start at — and know that an -network’s tiny error is then multiplied by (clip , or predict ).
- Few DDPM steps. Ancestral sampling with a fixed variance under-disperses (and eventually smears) when steps are large; use DDIM or many steps — or distillation (Lecture 12).
- Forgetting that guidance changes the distribution. samples , not : more typical, less diverse, and — script 04 — joint statistics such as “the right shape copies the left one” drift.
- Using the same noise for training and evaluation of the loss across noise levels, or comparing losses of -, - and -models directly: they weight noise levels differently; convert predictions to the same quantity first (script 02, Part 4).
- Confusing “valid” with “novel”. The training set’s own optimal denoiser gives 100% valid samples — all copies. Check samples against the training set.
- A schedule that does not reach pure noise (DDPM’s linear one ends at ): the model learns to rely on leaked signal it will not get at sampling time.
- Forgetting
x̂_0clipping for images with -prediction (homework 4’s tip): without it the first steps can push samples far outside .
12. Exercises
Exercise 1 (one-shot noising — derivation). From , show .
Answer
Induction. If , then . The bracket is Gaussian with variance .
Exercise 2 (the posterior of a step — derivation). Derive (mean , variance of §3.1).
Answer
Bayes: , both Gaussian in . The precisions add: , so . The mean is times the precision-weighted sum , which gives the two coefficients of §3.1.
Exercise 3 (the variational bound — derivation). Starting from , show that the bound splits into the prior term, one KL per step between and , and a reconstruction term.
Answer
Write (Bayes on each step of the Markov chain, conditioned on ) and . The log-ratio becomes ; taking expectations turns the first two kinds of terms into minus KL divergences.
Exercise 4 (Tweedie’s formula — derivation). For , show and hence .
Answer
. Differentiate under the integral: . Divide by : . Substituting and solving for gives the second form.
Exercise 5 (DDIM). Show that the update maps to when and are exact, for any .
Answer
With exact predictions, , where and are independent standard normals. The noise part has variance , so : the right marginal for every . Training never sees how and are jointly distributed, only the marginals — which is why one network supports the whole family. as in homework 4 reproduces DDPM’s posterior variance; is deterministic.
Exercise 6 (-loss vs -loss vs -loss). Show and (with ). Which noise levels does each loss emphasize?
Answer
: squared, . For : and , so and : squared, . So -loss weights -errors by SNR (huge at low noise, zero at pure noise); -loss by (never zero: high-noise levels still count); -loss weights all equally.
Exercise 7 (classifier-free guidance as a distribution — derivation). Show that corresponds to the score of .
Answer
, so . With Bayes, , and the product is . (Strictly, the noised versions of this tilted distribution are not the tilted noised distributions, so guided sampling is a heuristic for — one reason guidance distorts statistics.)
Exercise 8 (memorization). Show that the optimal denoiser for a training set is , and argue that sampling with it (many small steps) returns a training image.
Answer
The data distribution is uniform over the training images; given , the posterior over which image was noised is proportional to ; the posterior mean is the weighted average. As the softmax sharpens onto the nearest training image, and the reverse dynamics are attracted to it: the sampler’s endpoint is one of the . A network that generalizes must therefore fail to minimize the training loss perfectly — script 03 measures where it sits between the two optima.
Exercise 9 (coding). In script 02, train the -network twice as long (12,000 steps) and check whether its low-noise excess error and its samples improve. Then sample it with clipped to .
Answer
(Run it.) Expect the excess error at = 0.05–0.2 to shrink but not vanish (capacity), the DDIM explosion to remain without clipping (it is a conditioning problem near , not a training problem), and clipping to remove the explosion while leaving the low-noise blur.
13. Further reading
From the slides
- J. Ho, A. Jain, P. Abbeel. Denoising Diffusion Probabilistic Models, NeurIPS 2020.
- J. Sohl-Dickstein et al. Deep Unsupervised Learning using Nonequilibrium Thermodynamics, ICML 2015; Y. Song, S. Ermon. Generative Modeling by Estimating Gradients of the Data Distribution (NCSN), NeurIPS 2019.
- P. Dhariwal, A. Nichol. Diffusion Models Beat GANs on Image Synthesis, NeurIPS 2021; A. Nichol, P. Dhariwal. Improved Denoising Diffusion Probabilistic Models, ICML 2021.
- J. Ho, T. Salimans. Classifier-Free Diffusion Guidance, 2022.
- J. Song, C. Meng, S. Ermon. Denoising Diffusion Implicit Models, ICLR 2021.
- T. Salimans, J. Ho. Progressive Distillation for Fast Sampling of Diffusion Models (-parameterization), ICLR 2022.
- S. Lin et al. Common Diffusion Noise Schedules and Sample Steps are Flawed, WACV 2024.
- O. Ronneberger, P. Fischer, T. Brox. U-Net, MICCAI 2015.
- Tutorials linked from the slides: L. Weng’s blog post on diffusion models; the CVPR 2022 diffusion tutorial; C. Luo, Understanding Diffusion Models: A Unified Perspective, 2022; Y. Song’s blog on score-based models; and, for what the lecture skipped, Y. Song et al., Score-Based Generative Modeling through SDEs (ICLR 2021) and T. Karras et al., Elucidating the Design Space of Diffusion-Based Generative Models (NeurIPS 2022).
Added by these notes
- B. Efron. Tweedie’s Formula and Selection Bias, JASA 2011 — the posterior-mean / score identity of §4.
- Z. Kadkhodaie, F. Guth, E. Simoncelli, S. Mallat. Generalization in Diffusion Models Arises from Geometry-Adaptive Harmonic Representations, ICLR 2024 — when diffusion models memorize vs generalize (§8).
- N. Carlini et al. Extracting Training Data from Diffusion Models, USENIX Security 2023.
- D. Kingma, T. Salimans, B. Poole, J. Ho. Variational Diffusion Models, NeurIPS 2021 — the ELBO in terms of the SNR, and how loss weightings relate.
14. Next lecture
Lecture 11 built diffusion models from first principles on pixels. Lecture 12 follows them into the systems on the slides: text-to-image (GLIDE, DALL·E 2, Imagen, DALL·E 3), latent diffusion and the Diffusion Transformer (homework 4 Q3, on a ShapeWorld latent), cascades and video models, editing a pretrained model (SDEdit, inpainting — judged with the exact denoiser of this lecture), ControlNet, score distillation through a renderer, fast sampling by distillation (can a 1-step student beat the optimal denoiser’s 1-step sampler?), diffusion models as classifiers — and a side-by-side comparison of every model family of the course.