The final boss. We've assembled the prerequisites (Part 1), the closed-form noising process
(Part 2 — the route), and the denoising posterior (Part 3 — the car). What's left is the
driver (司机): how do we train the thing? — i.e. where DDPM's loss function comes from.
We'll do it two ways: the quick "cheat path," then the principled max-likelihood path. Both land
on the same place.
Path A — the cheat path (功法逆练)
From Part 3 we already have the reverse posterior:
q(xt−1∣xt,x0)=N(xt−1;μ~t(xt,x0),β~t2I).
The variance β~t2 is a constant (it depends on t but not on data), so to sample
xt−1 we only need the mean μ~t — and μ~t needs xt (the current, known
step) and x0 (unknown at inference). So we let a neural network with parameters θ predict
x0. Since x0 and the noise ε are interchangeable
(xt=αˉtx0+1−αˉtε), predicting x0 and predicting
ε are the same task — the authors just found predicting ε trains a bit
better.
Following that logic, the answer drops out immediately: minimize the gap between the model's
predicted noise εθ and the true (ground-truth) noise ε — that is, an
MSE loss:
L=ε−εθ(xt,t)22.
That's the shortcut — quick, but it skirts the author's actual reasoning. Now let's walk the
principled path and confirm it gives the same thing.
Path B — the principled path (功法正练)
The objective: maximum likelihood
We want generated images to have high probability of being what we asked for. (Want to generate
basketball-playing Kunkun? Then we want the generated image to have a high probability of being
that.) So we maximize the log-likelihood:
θmaxlogpθ(x0).
From Part 1, the variational bound (the thing we actually optimize) is:
L=Eq[−logq(x1:T∣x0)pθ(x0:T)].
If the step from maxlogpθ(x0) to this bound is fuzzy, go back to Part 1 — that's
where it was cleared.
Set the Eq aside for now and just play with the log part.
Step 1: expand the ratio with log rules
Substitute the route (Part 2) and reverse (Part 1) product forms:
Collect the xT pieces, logp(xT)−logq(xT)=logq(xT)p(xT), and recall that
every q here secretly carries the x0 condition (Part 3's whole point). Adding x0 back:
That last term is the running joke: "Given that Kunkun practiced for two and a half years, what's
the probability that Kunkun practiced for two and a half years?" It's 1, and log1=0, so it
vanishes.
Step 4: why the paper's L0 split is unnecessary
The paper (Eq. 5) writes the bound as three labeled pieces:
The author splits the t=1 term out as a special L0. But that's showmanship — it's
unnecessary. Pull t=1 out of our unified sum:
logq(x0∣x1,x0)pθ(x0∣x1)=logpθ(x0∣x1),
because q(x0∣x1,x0)=1 (the Kunkun joke again — you already knowx0). So the special
L0 term is just the t=1 case of the general sum. Keep it folded into ∑t=1T — it's
cleaner, and it's what most real implementations do anyway.
Step 5: recognize the KL divergences
Restore the negative sign (we minimize −log) and the expectation Eq. A term of the
form Eq[logpq]=∫qlogpqis a KL divergence
(Part 1). So:
Term (i) has both ends known: q(xT∣x0) from the forward process, and p(xT) defined
as fixed Gaussian noise. No neural network involved → it's a constant → drop it.
Term (ii) is a KL between two Gaussians — and that's the heart of the loss.
(A small honesty note: the outer Eq doesn't fully cancel into the KL, because the
q(x1:T∣x0) expectation has only "used up" the xt−1,xt marginals — the other
latents x1,…,xT−1 are still being integrated. So an expectation lingers. It doesn't
affect the final result.)
Computing the KL between two Gaussians
Let p=N(μ1,σ12) (this is q(xt−1∣xt,x0)) and
q=N(μ2,σ22) (this is pθ(xt−1∣xt)).
DKL(P∥Q)=∫p(x)logq(x)p(x)dx.
Trick: keep the leading factor as p(x) — don't expand it. Only expand the log of the ratio:
Term 1 — expectation of a constant is itself:
∫p(x)logσ1σ2dx=logσ1σ2.
Term 2 — ∫p(x)(x−μ1)2dx=Varp(X)=σ12, so:
−2σ121⋅σ12=−21.
Term 3 — expand (x−μ2)2=x2−2μ2x+μ22 and use:
∫p(x)xdx=μ1,∫p(x)x2dx=E[X2]=μ12+σ12.
The second identity comes from the variance definition (a quick college-level reminder):
$$
\sigma_1^2 = \mathbb{E}[(X-\mathbb{E}X)^2] = \mathbb{E}[X^2] - 2\mu_1^2 + \mu_1^2 = \mathbb{E}[X^2] - \mu_1^2
\ \Rightarrow\ \mathbb{E}[X^2] = \sigma_1^2 + \mu_1^2.
$$
Therefore:
∫p(x)(x−μ2)2dx=(μ12+σ12)−2μ2μ1+μ22=σ12+(μ1−μ2)2,
and Term 3 =2σ22σ12+(μ1−μ2)2.
Here's the payoff: almost everything is dead weight. In DDPM we set the model's variance equal
to the posterior variance — i.e. σ1=σ2 (both equal the constant β~t).
Then:
logσ1σ2=log1=0,
2σ22σ12=21, which cancels the −21,
leaving only 2σ22(μ1−μ2)2.
Since σ22 is a constant, the loss is proportional to:
L∝(μ1−μ2)2=(μ~t(xt,x0)−μθ(xt,t))2.
Both means are functions of (xt,x0), and pθ has the same functional form as q —
the only difference is that q uses the true x0 while pθ uses the model's predicted
x^0. Plugging the Part 3 expression for μ~t into both and converting x0→ε via x^0=(xt−1−αˉtε^)/αˉt,
the whole thing simplifies to a noise-prediction MSE:
L∝ε−εθ(xt,t)22.
The principled path lands exactly where the cheat path did. Forward and reverse "cultivation"
agree — which is why DDPM training is, in the end, just MSE on predicted noise.
Takeaways
Two routes, one answer. The shortcut (predict ε, match with MSE) and the
principled max-likelihood derivation give the same loss.
Drop the constants. Of the variational bound's terms, LT is network-free (constant), the
variances cancel, and only the mean-difference term survives — which becomes MSE on noise.
Don't over-engineer the reading. The paper's separate L0 term is unnecessary — it folds
into the general sum. Most implementations just use the plain noise-MSE. When learning diffusion,
think simple, not complicated — the more you try to over-formalize every term, the scarier
(and less clear) it gets.
That's the driver — and with it, the four-level run through the DDPM main paper is complete. 🎉
Congratulations on clearing all four gates: prerequisites → noising → denoising → loss. Anything
left unclear can go in a bonus "DLC" episode.
Notes taken from a video tutorial on DDPM (Part 4 of the series).
References
Ho, J., Jain, A., & Abbeel, P. (2020). Denoising Diffusion Probabilistic Models. NeurIPS 2020. arXiv:2006.11239