Maps to: MIT 6.S978 Deep Generative Models (Fall 2024), Lecture · Thu 10/17/2024 · Energy-based Models, Score Matching, Diffusion Models (slides:
lec5_diffusion.pdf, Kaiming He). Problem Set 4 (released 10/22):
- DDPM on MNIST: schedule, forward process, sampling;
- ablations of the number of timesteps and of the number of sampling steps;
- continuous time: marginals and analytic scores of a two-Gaussian mixture under the VP-SDE.
Basis: The order and content follow the slides — diffusion models (forward process, reverse process, training objective, noise-conditional network), then energy-based models and score matching. Their derivations are written out in full: the closed-form forward process, the Gaussian posterior, KL → L2 on the noise, the variational bound, denoising score matching, Langevin dynamics. All of Problem Set 4 is done: the DDPM on glyphs (in a latent space, §5.3), its ablations, and the VP-SDE derivations as worked solutions (§8). Experiments were designed for these notes and run on a CPU. Several outcomes refined or contradicted my expectations and are reported as they came out:
- training with the ELBO’s weights failed through gradient variance, and worked with importance sampling;
- T = 500 beat T = 1000 with the problem set’s schedule;
- a pixel-space MLP denoiser was too weak, which is why the glyph DDPM runs in Lecture 8’s latent space.
Code:
code/lecture12/— 7 scripts + the shared modulediffusion.py. They run in 1–125 s. Reading time: 5 hours including running the code.
0. How to read this lecture
0.1 Where we are
The slides open with a timeline: diffusion models were proposed in 2015 (Sohl-Dickstein et al.), largely ignored for five years, and then — after DDPM (Ho et al. 2020) and score-based models (Song & Ermon 2019) — boomed. Today they and their relatives (flow matching, Lectures 16–17) power most image, video and audio generators.
We now have every ingredient to see why:
- Lecture 3: the VAE’s ELBO — and diffusion’s objective is an ELBO;
- Lecture 5: “divide and conquer” by a chain of simple conditionals — and diffusion chains many Gaussian conditionals;
- Lecture 8: a one-page preview of noising and denoising;
- Lecture 11: samples moving along a score difference.
This lecture builds diffusion models properly. It then shows that they are a form of score matching, an old idea for training energy-based models.
0.2 Learning goals
- Derive the forward process in closed form, explain the role of the schedule, and check that its end is “all noise” (§2).
- Explain why the reverse conditional q(x_{t−1} | x_t) is unknown but q(x_{t−1} | x_t, x_0) is a known Gaussian; derive its mean and variance; turn the KL into an L2 loss on the noise (§3).
- Derive the training objective from the variational bound, and explain “set the weights as 1 (critical)” (§4).
- Describe the noise-conditional network and the training and sampling algorithms; implement Problem Set 4’s DDPM and its ablations (§5).
- Define energy-based models, score functions, score matching (explicit, sliced, denoising) and Langevin dynamics, and connect them to diffusion (§6–7).
- Solve Problem Set 4’s continuous-time problem: VP-SDE marginals, scores, the reverse SDE and the probability-flow ODE (§8).
0.3 Map
| Section | Content | Code |
|---|---|---|
| §1 | Diffusion in a nutshell; “what is noise?” | 01_forward_process.py (part 1) |
| §2 | The forward process and the schedule | 01_forward_process.py |
| §3 | The reverse process | 02_reverse_conditionals.py |
| §4 | The training objective | 03_ddpm_2d.py, 04_ddpm_glyphs.py (part 3) |
| §5 | The noise-conditional network; Problem Set 4 | 04_ddpm_glyphs.py, 05_ddpm_timesteps.py |
| §6–7 | Energy-based models, score matching, Langevin dynamics | 06_score_matching.py |
| §8 | Continuous time: the VP-SDE (Problem Set 4, problem 3) | 07_vp_sde.py |
| §9 | Many perspectives on diffusion | — |
1. Diffusion in a nutshell
The slides compress the idea into two pictures:
- Forward process: add noise. Data → slightly noisy → … → pure noise, by many small, fixed steps. Nothing is learned.
- Reverse process: denoise. Pure noise → … → data. Each small step back is learned.
What is noise? The slides answer with an equivalence: adding Gaussian noise to a sample ⇔ sampling from the convolution of its density with a Gaussian. If x ~ p, then x + σε ~ p * N(0, σ²). Noise blurs a distribution: modes widen, merge, and eventually the distribution becomes a single broad Gaussian — easy to sample.
Code 01 (part 1) checks this on a 1-D mixture 0.3·N(−2, 0.3²) + 0.7·N(1.5, 0.5²). The histograms of x + σε match p * N(0, σ²) to histogram precision. The two modes, 3.5 apart, merge between σ = 1 and σ = 2. But the variance keeps growing (2.8 → 18.8 at σ = 4). DDPM therefore also shrinks the data at every step, so that the end point is a fixed N(0, I).
Divide and conquer. Generating data from noise in one step is hard: one map from N(0, I) to the data distribution. Generating one slightly less noisy version of a noisy sample is easy, as §3 shows. Diffusion splits the hard problem into T easy ones — the chain rule of Lecture 5, applied along a noise axis instead of across pixels.
2. The forward process
2.1 The step and its coefficients
- Variance preserving. If , then : the coefficients are chosen so the scale never explodes.
- The noise is i.i.d. It is independent across dimensions (the identity covariance), with the same dimension as the data.
- The schedule — “key to diffusion models’ success”, in the slides’ words — fixes how fast the data is destroyed.
Problem Set 4, 1(a) uses the linear schedule with β from 1e-4 to 0.02 and T = 1000, and (ddpm_schedules).
2.2 Sampling without simulation
Claim. .
Derivation (Problem Set 4, 1(b)). Unroll two steps:
The two noise terms are independent Gaussians, so their sum is Gaussian with variance . Hence . By induction,
Training never simulates the chain: draw t, draw ε, compute in one line (q_sample).
Code 01 (part 2) iterates the chain for one glyph with 4000 noise draws. After t steps every pixel has mean and variance to Monte-Carlo precision:
| t | ᾱ_t | max |mean − √ᾱ x0| | max |var − (1 − ᾱ)| |
|---|---|---|---|
| 10 | 0.99811 | 0.0019 | 0.0001 |
| 100 | 0.89702 | 0.0142 | 0.0054 |
| 500 | 0.07859 | 0.0531 | 0.0604 |
| 1000 | 0.00004 | 0.0478 | 0.0662 |
(The standard error of a mean over 4000 chains is ≈ 0.015; the table shows the worst of 256 pixels.)
2.3 The schedule, and whether x_T is pure noise
How much signal survives is best read through the log signal-to-noise ratio, :
| t | linear β_t | linear ᾱ_t | linear log-SNR | cosine ᾱ_t | cosine log-SNR |
|---|---|---|---|---|---|
| 1 | 0.00010 | 0.99990 | +9.21 | 0.99996 | +10.09 |
| 250 | 0.00506 | 0.52409 | +0.10 | 0.84701 | +1.71 |
| 500 | 0.01004 | 0.07859 | −2.46 | 0.49384 | −0.02 |
| 750 | 0.01502 | 0.00335 | −5.70 | 0.14427 | −1.78 |
| 1000 | 0.02000 | 0.00004 | −10.12 | 0.00000 | −19.84 |
The linear schedule reaches “as much noise as signal” by t ≈ 250 and spends its last quarter below log-SNR −5.7, at almost pure noise. The cosine schedule (Nichol & Dhariwal 2021) keeps more signal in the middle.
Is the end “all noise”? The sampler starts from N(0, I), so we want . For glyph pixels in [−1, 1]:
| schedule | ᾱ_T | E KL(q(x_T | x_0) ‖ N(0, I)), nats / image |
|---|---|---|
| linear, T = 500 | 6.35e-3 | 0.677 |
| linear, T = 1000 (Problem Set 4) | 4.04e-5 | 0.004 |
| linear, T = 2000 | 1.63e-9 | 0.000 |
| cosine, T = 1000 | 2.43e-9 | 0.000 |
With the problem set’s β range, T = 500 leaves √ᾱ_T ≈ 8% of the image in x_T. §5.4 measures whether that matters (it barely did).

The slides’ summary of the forward process: pre-defined conditional distributions; Gaussian with controllable mean and std; divide and conquer.
3. The reverse process
3.1 The target is unknown
We want to run the chain backward: . By Bayes, it is , and is the data distribution blurred to level t-1. The slides’ picture: several different could have produced the same , and how likely each one is depends on the data.
So the reverse conditional is our target, but unknown. We represent it by a network, , a Gaussian whose mean (and possibly variance) the network outputs.
3.2 Conditioned on x_0, it is a known Gaussian
is not our target but known. Both factors in are Gaussians in , so their product is too.
- Precision (inverse variance):
- Variance and mean:
The mean is a linear combination of and , and the variance depends only on the schedule: “the outcome of the dependency graph, some linear combinations”. Code 02 (part 1) confirms all three numbers by simulation. It fixes x_0 = 1.3, draws 400,000 pairs and regresses one on the other:
| t | slope on x_t: fitted / formula | intercept / c0·x_0 | residual var / β̃_t |
|---|---|---|---|
| 2 | 0.45423 / 0.45484 | 0.70949 / 0.70896 | 5.440e-5 / 5.455e-5 |
| 100 | 0.98069 / 0.98090 | 0.02501 / 0.02480 | 2.042e-3 / 2.035e-3 |
| 1000 | 0.98985 / 0.98995 | −0.00012 / 0.00017 | 2.002e-2 / 2.000e-2 |
Written with the noise. Substitute and simplify (the coefficients of combine to ):
The “noise” ε is the only unknown in the mean.
3.3 Why many small steps
How wrong is it to model — the unknown one — as a Gaussian? Code 02 (part 2) computes it exactly for the 1-D mixture (Bayes on a fine grid). It measures its KL to the best Gaussian fit, averaged over 100 values of x_t:
| step t → s | jump | mean KL to the best Gaussian |
|---|---|---|
| 10 → 9 | 1 | 1.1e-13 |
| 300 → 299 | 1 | 4.1e-8 |
| 300 → 250 | 50 | 5.0e-3 |
| 300 → 100 | 200 | 0.22 |
| 900 → 500 | 400 | 3.6e-4 |
| 300 → 0 | 300 | 0.35 |
| 900 → 0 | 900 | 0.75 |

- One small step back is Gaussian to 10⁻⁷ nats or better, so a Gaussian loses essentially nothing and only its mean must be learned. That is the justification for many small steps.
- Big jumps that land near the data are bimodal. A noisy is compatible with both modes, and a Gaussian step must average them (Lecture 1’s ghost).
- A big jump between two noise-dominated levels (900 → 500) is still nearly Gaussian. The difficulty lives at the low-noise end, where the data’s structure appears.
3.4 From KL to an L2 loss on the noise
Represent with a preset variance ( or ; DDPM found both work). The KL between two Gaussians with the same variance is an L2 distance between the means — Problem Set 1’s result, which the slides recall:
Give the same form as , with a network in place of the unknown noise: . Then
“Learn → preset → ‘noise’ → estimate noise”: a network to predict noise; input: noisy image. Code 02 (part 3) checks the identity to six digits and prints the weights: with , λ_t ranges from 0.005 to 0.6 (a factor of 120), largest at the smallest t.
4. The training objective
4.1 A variational bound
Treat as latent variables of a hierarchical VAE whose “encoder” is the fixed forward process and whose “decoder” is the reverse chain . Exactly as for the ELBO of Lecture 3 (it is the ELBO if T = 1):
The slides read the three terms one by one:
- has no parameters, unlike the VAE’s : it compares a fixed Gaussian with the constant prior. §2.3 measured it: 0.004 nats per image for Problem Set 4’s schedule.
- is the KL of §3.4, an L2 loss on the noise, weighted by λ_t.
- is a reconstruction loss, as in the VAE.
4.2 “Set as 1 (critical)”
Averaging over t uniformly, the bound is . Ho et al. (2020) dropped the weights:
The slides call this the critical step: a weighted sum of ELBO terms becomes one plain regression, which trains stably and gives the best samples. Salimans & Ho (2022) study the choice of weighting in depth.
4.3 Experiment: three objectives in 2-D
Code 03 trains (an MLP) on 8 Gaussians of std 0.1 on a ring with three objectives (two seeds). It reports sample quality and the bound itself in nats per point; the true is 0.323.
| objective | quality (seeds 0 / 1) | spread (data 0.125) | −ELBO total |
|---|---|---|---|
| L_simple (weights 1) | 0.919 / 0.935 | 0.156 / 0.153 | 1.471 / 1.432 |
| ELBO weights, t uniform | 0.637 / 0.412 | 0.299 / 0.408 | 2.135 / 2.570 |
| ELBO weights, t ∝ λ_t (importance sampling) | 0.963 / 0.938 | 0.139 / 0.148 | 1.346 / 1.338 |

I expected the ELBO weights to give the best bound and worse samples. Instead, with uniform t they gave the worst bound and the worst samples. λ_t varies by 120×, so a few small-t terms dominate every minibatch gradient. Drawing t with probability ∝ λ_t optimizes the same objective with low variance, and it gave the best bound — with samples at least as good as L_simple’s. In 2-D, the weights matter through the variance of the gradient.
4.4 Experiment: the weights on glyphs
On glyphs (code 04, part 3; DDPM on Lecture 8’s latents, §5.3), the importance-sampled ELBO objective again gives the better bound, but now clearly worse samples:
| objective | valid | FID | −ELBO of test latents (nats / glyph) |
|---|---|---|---|
| L_simple | 0.897 | 33.5 | 94.5 |
| ELBO weights (t ∝ λ_t) | 0.790 | 89.3 | 74.3 |
This is Ho et al.’s finding in miniature. The ELBO’s weights concentrate on the smallest noise levels, where the network learns to remove the last traces of noise: fine detail that matters for likelihood but barely for how the glyph looks. Weights of 1 spend relatively more effort at the noisy levels, which decide each glyph’s overall shape. Likelihood and sample quality ask for different weightings — the trade-off behind “set as 1 (critical)”.
5. The noise-conditional network, the algorithms, and Problem Set 4
5.1 One network for all levels
Diffusion decomposes one hard distribution into T simpler conditionals, which would need T networks. Instead, one network conditioned on the noise level fits all of them: the Noise Conditional Score Network (NCSN) of Song & Ermon (2019), “things made work”.
- Inputs: the noisy image and the level t, as an embedding added inside the network.
- Output: the predicted noise , the same size as the input (a U-Net or a ViT). Predicting the noise from a noisy image, the network “behaves like a denoising autoencoder”.
5.2 The algorithms, annotated
Training (repeat until converged) Sampling
x_0 ~ data x_T ~ N(0, I)
t ~ Uniform{1, …, T} for t = T, …, 1:
ε ~ N(0, I) z ~ N(0, I) if t > 1 else 0
gradient step on ‖ε − ε_θ(√ᾱ_t x_0 + √(1−ᾱ_t) ε, t)‖² x_{t−1} = (x_t − (1−α_t)/√(1−ᾱ_t) · ε_θ(x_t, t)) / √α_t + σ_t z
return x_0
The sampling line is “estimated μ, then sample from the estimated distribution” — Problem Set 4, 1(c). ddpm_sample implements it, and also uses it with fewer steps: jumps between levels of an evenly spaced subsequence use the same posterior formula with α = ᾱ_t/ᾱ_s. In the slides’ words: “turns out to be extremely simple”, and being simple and effective moves the needle.
The slides’ examples (Ho et al. 2020):
- unconditional CIFAR-10 samples emerging from noise;
- “shared intermediate latents”: start several reverse chains from the same intermediate . From a very noisy the results differ entirely; from a mildly noisy one they share coarse structure. The reverse process decides coarse content first and detail last.
5.3 Problem Set 4, problem 1 on glyphs: pixels vs latents
Problem Set 4 trains a pixel-space DDPM on MNIST with a U-Net on a GPU. On our CPU a convolutional U-Net would need minutes per 1000-step sampling run (3×3 convolutions at 16×16 cost ~9 ms per layer here). Code 04 therefore uses an MLP denoiser, which is fast, and compares two spaces:
| DDPM (T = 1000, linear β, ε-prediction, 1000 sampling steps) | valid | FID | train | sample 2000 |
|---|---|---|---|---|
| on the 256 pixels (MLP, 4000 steps) | 0.444 | 183.0 | 20 s | 15 s |
| on the 64 numbers of Lecture 8’s continuous tokenizer (MLP, 6000 steps) | 0.897 | 33.5 | 25 s | 11 s |
| the tokenizer’s reconstructions (ceiling) | 0.989 | 1.9 | — | — |

On pixels, the MLP leaves blotchy, half-formed strokes. On the tokenizer’s 64 smooth numbers the same algorithm produces clean glyphs. The pixel space is 4× larger and an MLP has no notion of locality — the reason pixel diffusion uses U-Nets, and the reason latent diffusion (Lecture 13) moved the whole process into an autoencoder’s latent space. The rest of the glyph experiments use the latent DDPM.
5.4 Problem Set 4, problem 2: steps and timesteps
An inference schedule with fewer steps (2.2; code 04, part 2). The network is trained once on all 1000 levels; only the sampler changes:
| sampling steps | 1000 | 100 | 50 | 20 |
|---|---|---|---|---|
| valid | 0.897 | 0.893 | 0.888 | 0.836 |
| FID | 33.5 | 36.7 | 55.7 | 72.7 |
| time (2000 glyphs) | 11.7 s | 1.3 s | 0.7 s | 0.4 s |
100 steps lose almost nothing, which is a 10× speed-up for free; 50 lose some; 20 clearly degrade.
In 2-D (code 03, part 2), going from 1000 to 50 steps made samples slightly tighter (spread 0.156 → 0.137). Each big jump injects the posterior variance of a single step, too little — the effect Lecture 8 §2.4 saw. Below ~20 steps they spread out and leave the modes, as §3.3 predicts for big Gaussian jumps.
The number of training timesteps (2.1; code 05). With the problem set’s schedule (β from 1e-4 to 0.02 whatever T is):
| T | ᾱ_T | KL(q(x_T) ‖ N(0, I)) per latent | valid | FID | sampling time |
|---|---|---|---|---|---|
| 500 | 6.4e-3 | 0.204 | 0.933 | 27.9 | 5.3 s |
| 1000 (baseline) | 4.0e-5 | 0.001 | 0.897 | 33.5 | 10.6 s |
| 2000 | 1.6e-9 | 0.000 | 0.853 | 34.6 | 21.9 s |
| 500 with β_max = 0.04 | 3.9e-5 | 0.001 | 0.896 | 33.5 | 5.2 s |
I expected T = 500 to be the worst, since its chain does not reach pure noise. It was the best. Starting from N(0, I) instead of the true costs only 0.2 nats. And with β_max = 0.04, the same T = 500 ends in pure noise and performs exactly like T = 1000.
So the number of steps was not what mattered; the range of noise levels was. The T = 500 / 0.02 schedule never goes below log-SNR ≈ −5, while the T = 1000 schedule spends its last quarter at almost pure noise (§2.3). T = 2000 spreads the same 6000 training steps over twice as many levels and costs twice the sampling time. Lecture 13 returns to the lesson: choose the noise schedule and its endpoint carefully — more than the step count.
6. Energy-based models
The slides’ second part: diffusion models are closely related to score matching, and score matching is one solution to training energy-based models (EBMs). Many concepts co-evolved: annealed importance sampling (Neal 1998), denoising score matching (Vincent 2011), the noise-conditional score network (Song & Ermon 2019).
Energy. Define a scalar function E(x). At inference time, find the x that minimizes the energy. In this general sense EBMs can be probabilistic or not, generative or discriminative (LeCun et al.’s tutorial, 2006: inference is energy minimization).
A probability from an energy.
Any energy network defines a density, but Z is an intractable integral in high dimensions. Maximum likelihood needs , which requires sampling from the model itself: the classic difficulty of EBMs.
The score function sidesteps Z:
The score needs no normalizing constant. EBMs are therefore “non-normalized probabilistic models”, and a model of the score is enough both to train (§7) and to sample (Langevin, §7.3).
7. Score matching and Langevin dynamics
7.1 Score matching
Instead of parameterizing p, parameterize the score, , and minimize the Fisher divergence:
The data score is unknown. Hyvärinen (2005) expands the square and integrates the cross term by parts (assuming p vanishes at infinity):
So the objective equals, up to a constant,
computable from samples alone. The trace costs one backward pass per dimension. Sliced score matching (Song et al. 2019) projects onto random directions v, , at the cost of one pass.
7.2 Denoising score matching
Vincent (2011) noises the data, , and proves
Proof sketch. Both sides share . For the cross term,
The remaining terms do not depend on θ.
The conditional score is known: . So the network learns to predict the (negative, scaled) noise, and its optimum is the score of the noised data. For the diffusion process, , so
DDPM’s noise predictor is a scaled score model.
7.3 Langevin dynamics
Given a score, sample p by iterating
This needs the score, not p. As η → 0 with many steps, the stationary distribution is p. In the slides’ reading: a “learning rate” times a gradient plus a perturbation — gradient descent in the energy landscape, with noise.
The diffusion sampling step has the same shape. Writing as a score,
a step along the score plus noise, at a noise level that decreases over time: an annealed, Langevin-like sampler.
7.4 Experiment: score matching and Langevin on a lopsided mixture
Code 06 uses two 2-D Gaussians (std 0.5) at (−2, 0) and (2, 0), with weights 0.8 and 0.2.
Learning the score (2000 steps; relative error of the learned score):
| method | on data | between the modes | far away |
|---|---|---|---|
| explicit (Hyvärinen) | 0.078 | 0.265 | 0.159 |
| sliced | 0.123 | 0.351 | 0.186 |
| denoising, σ = 0.1 (vs the blurred score) | 0.149 | 0.518 | 0.233 |
All three are accurate where the data are and 2–4× worse where training samples are rare: Song & Ermon’s low-density regions problem.
Sampling with the exact score, from a uniform start (left-mode fraction; truth 0.80):
| sampler | left-mode fraction | within 3 std |
|---|---|---|
| Langevin, 100 steps | 0.528 | 0.989 |
| Langevin, 1000 steps | 0.536 | 0.988 |
| Langevin, 10,000 steps | 0.591 | 0.985 |
| annealed Langevin (10 levels, σ = 3 → 0.01) | 0.793 | 0.987 |
| NCSN (learned, 10 levels) + annealed Langevin | 0.793 | 0.963 |

- Plain Langevin gets the weights wrong, even with the perfect score. Each chain falls into the mode nearest its start and crosses the low-density gap very rarely. The score near a mode does not “know” how heavy the other mode is.
- Annealing fixes it. At σ = 3 the blurred density is one broad bump with the right weights, so the chains are distributed correctly before the gap opens.
- A learned noise-conditional network does the same. One network learns all ten blurred scores by denoising — a diffusion model in all but name (§5.1).
8. Continuous time: the VP-SDE (Problem Set 4, problem 3)
As T → ∞ with β_t scaled down accordingly, DDPM’s chain becomes the variance-preserving SDE of Song et al. (2021):
with β_min = 0.1 and β_max = 20.
(a) Marginals. The SDE is linear, so is Gaussian with mean and variance , where
For , integrating against the data:
(b) The score. With and responsibilities ,
(c) Generation. Anderson’s (1982) reverse-time SDE
run from t = 1 down to 0 has the same marginals in reverse. So does the deterministic probability-flow ODE, .
Code 07 uses π = (0.6, 0.4), μ = (−2, 1.5), σ = (0.4, 0.3):
-
Forward. The simulated forward SDE matches (a) at every t (L1 of 0.03–0.05, histogram noise). At t = 1, m = 0.0066.
-
Score. The closed-form score matches a finite-difference derivative of log p_t to three decimals.
-
Generation, starting from N(0, 1) with the exact score:
sampler steps L1 to p_data left fraction (0.60) reverse SDE 1000 0.035 0.599 probability-flow ODE 1000 0.036 0.600 reverse SDE 30 0.082 0.601 probability-flow ODE 30 0.051 0.596 reverse SDE 10 0.329 0.597 probability-flow ODE 10 0.142 0.590

- The score is all the generator needs. Both reverse processes recover the 60 / 40 data exactly with enough steps. The ODE maps each starting noise to one sample deterministically, yet has the same marginals.
- With few steps the ODE degrades more gracefully. The SDE’s injected noise must be removed again by the score term, which first-order steps do poorly. This is why fast samplers (DDIM, DPM-Solver, EDM’s Heun sampler, Lecture 13) integrate the ODE. MIT 6.S184’s notes cover the SDE / ODE view in depth.
9. Many perspectives on diffusion
The slides close with a list, after Sander Dieleman’s blog post “Perspectives on diffusion”. Diffusion models can be read as:
| perspective | what it explains |
|---|---|
| a hierarchical VAE with a fixed encoder | the training objective (§4) |
| energy-based models / score matching | why noise prediction learns a score; Langevin sampling (§6–7) |
| an autoregressive model (over noise levels; Dieleman: “spectral autoregression” — coarse frequencies first) | why coarse structure is decided first (§5.2’s shared latents) |
| SDEs / ODEs | continuous time, fast deterministic samplers (§8) |
| a normalizing flow | the probability-flow ODE is an invertible map with exact likelihoods (Lecture 4) |
| a recurrent network | the same network applied repeatedly, with the noise level as input |
10. Summary
- Forward process. , in one jump; variance preserving; the schedule decides how fast signal turns to noise. With β from 1e-4 to 0.02, x_T is pure noise for T ≥ 1000.
- Reverse process. is unknown, but is a Gaussian with mean . One small step back is Gaussian to 10⁻⁷ nats; big jumps toward the data are bimodal.
- Objective. The variational bound is a sum of noise-L2 terms weighted by λ_t. Setting the weights to 1 favours sample quality: on glyphs FID 33 vs 89, at a worse bound (94 vs 74 nats). Naive ELBO training also suffers from gradient variance, which importance sampling of t removes.
- Problem Set 4.
- A latent-space MLP DDPM gives valid 0.90 on glyphs, where the pixel-space MLP gives 0.44.
- 100 sampling steps are nearly as good as 1000.
- T = 500 beat T = 1000 with the pset’s schedule, because the noise range mattered, not the step count.
- The VP-SDE’s marginals and scores are exact, and both the reverse SDE and the ODE regenerate the data.
- EBMs and scores. ; the score needs no Z. Score matching (explicit, sliced, denoising) learns it from samples, and denoising score matching is noise prediction. Langevin dynamics samples with it but mixes poorly between modes; annealing over noise levels fixes that — which is what diffusion does.
11. Common pitfalls
- “The forward process must be simulated step by step.” It has a closed form for every t (§2.2).
- “The network learns q(x_{t−1} | x_t, x_0).” That one is known; the network learns the unknown q(x_{t−1} | x_t), using the known one as a regression target (§3).
- “More diffusion steps are always better.” The range of noise levels matters more than the count (§5.4); and samplers can skip steps (§5.4, Lecture 13).
- “L_simple is just a crude ELBO.” It is a deliberate reweighting toward sample quality; the ELBO weighting wins on likelihood and loses on samples (§4.4).
- “Langevin with the true score samples the distribution.” In the limit, yes; in practice it cannot move mass between separated modes (§7.4).
- “The ODE and the SDE give different distributions.” With the exact score they have the same marginals; they differ in stochasticity and discretization error (§8).
12. Exercises
Exercise 1 (Problem Set 4, 1(a)(b)). Prove . What is for the linear schedule from 1e-4 to 0.02 with T = 1000, approximately, using ?
Answer
The proof is in §2.2.
, so . The exact value is 4.04e-5; the next term of the expansion, , accounts for the difference.
For T = 500: , so (exact: 6.35e-3).
Exercise 2 (the posterior). Derive and of §3.2 by completing the square, and show the ε form of the mean.
Answer
The log of the product, as a function of , is
- Quadratic coefficient: , giving the precision of §3.2 and hence .
- Linear coefficient: . Multiplying by gives .
- The ε form: substitute . The coefficients add to . The ε coefficient is .
Exercise 3 (the ELBO decomposition). Derive the decomposition of §4.1 from , using (the Markov property makes conditioning on free).
Answer
The last sum telescopes to , which cancels the and leaves
Take to get .
Exercise 4 (the weights). With , show . Why is it largest at small t?
Answer
. Substituting gives .
At small t, is tiny — almost no noise has been added yet — so λ_t is large. For t = 2: , as code 02 printed. Predicting the noise at nearly clean levels is weighted heavily by the bound, though it changes the image imperceptibly.
Exercise 5 (denoising score matching). Complete the proof in §7.2, including what the “const” is. What is the minimizer of the DSM objective?
Answer
Left side: .
Right side: .
The first terms are equal (same marginal of ), and the cross terms are equal by the computation in §7.2. So the two sides differ by the constant .
The minimizer over all functions is , the score of the noised data. That is also “the best noise prediction is ” (Tweedie’s formula).
Exercise 6 (Langevin’s stationary distribution). Show that p is stationary for the continuous-time Langevin diffusion , using the Fokker–Planck equation .
Answer
At ρ = p: .
Stationarity says nothing about how fast ρ approaches p. With two modes separated by a low-density gap, the convergence time grows exponentially with the barrier height — code 06’s 0.53 → 0.59 in 10,000 steps.
Exercise 7 (Problem Set 4, 3(a)). Derive m(t) for the VP-SDE from and the variance equation . Show .
Answer
Mean. , with .
Variance. , so , i.e. .
For exactly, v(0) = 0, giving the conditional variance . For a component with variance , , as in §8.
Exercise 8 (hands-on).
(a) In 04_ddpm_glyphs.py, predict instead of ε (the network outputs ; use in the sampler). Compare samples.
(b) In 05_ddpm_timesteps.py, try the cosine schedule with T = 1000.
(c) In 06_score_matching.py, change the mode weights to 0.95 / 0.05. How many annealing levels does annealed Langevin need to get them right?
Answer (sketch)
(a) x₀-prediction with an unweighted L2 loss emphasizes the noisy levels (where x₀ is hard to predict) differently from ε-prediction; samples are often blurrier. Lecture 13 discusses the v-parameterization as a compromise.
(b) The cosine schedule spends fewer levels at almost-pure noise. Given script 05’s finding, expect it to do at least as well as the linear T = 1000.
(c) The first levels must be broad enough that the blurred density is unimodal with the right weights. With more extreme weights the sampler is more sensitive to the largest σ and to the number of steps per level; expect to need σ₁ ≳ the mode separation and a few more levels.
13. Further reading
From the slides
- J. Sohl-Dickstein et al., Deep Unsupervised Learning using Nonequilibrium Thermodynamics, ICML 2015.
- J. Ho, A. Jain, P. Abbeel, Denoising Diffusion Probabilistic Models, NeurIPS 2020.
- A. Hyvärinen, Estimation of Non-Normalized Statistical Models by Score Matching, JMLR 2005.
- P. Vincent, A Connection Between Score Matching and Denoising Autoencoders, Neural Computation 2011.
- Y. Song, S. Ermon, Generative Modeling by Estimating Gradients of the Data Distribution, NeurIPS 2019.
- Y. Song, D. P. Kingma, How to Train Your Energy-Based Models, arXiv 2021.
- Y. LeCun et al., A Tutorial on Energy-Based Learning, 2006.
- S. Dieleman, Perspectives on diffusion (blog), 2023.
Beyond the slides
- Y. Song et al., Score-Based Generative Modeling through Stochastic Differential Equations, ICLR 2021 (Problem Set 4, problem 3).
- A. Nichol, P. Dhariwal, Improved Denoising Diffusion Probabilistic Models, ICML 2021 (cosine schedule, learned variances, importance sampling of the ELBO).
- T. Salimans, J. Ho, Should EBMs model the energy or the score?, 2021; and Progressive Distillation, ICLR 2022 (Lecture 13).
- B. D. O. Anderson, Reverse-time diffusion equation models, Stochastic Processes and their Applications, 1982.
- MIT6.S184 (in Chinese): flow matching and diffusion through SDEs and ODEs.
Related notes in this course
- Lecture 3 (the ELBO); Lecture 8 §1 (the one-page preview), §2.4 (few-step tightening); Lecture 11 §5 (GANs and score differences).
14. Next lecture
Lecture 13: Diffusion in practice — guidance, distillation and high resolution (reading session · 10/22).
This lecture’s DDPM is unconditional, needs hundreds of steps, and ran on 16×16 glyphs. Lecture 13’s papers address all three:
- classifier-free guidance makes diffusion models follow a condition (and trades diversity for fidelity, like truncation in Lecture 10);
- progressive distillation halves the number of sampling steps again and again;
- simple diffusion shows how to train directly on high-resolution pixels by shifting the noise schedule — the schedule lesson of §5.4, at scale.