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).q(x_{t-1} \mid x_t, x_0) = \mathcal{N}\big(x_{t-1};\ \tilde\mu_t(x_t, x_0),\ \tilde\beta_t^2 \mathbf{I}\big).

The variance β~t2\tilde\beta_t^2 is a constant (it depends on tt but not on data), so to sample xt−1x_{t-1} we only need the mean μ~t\tilde\mu_t — and μ~t\tilde\mu_t needs xtx_t (the current, known step) and x0x_0 (unknown at inference). So we let a neural network with parameters θ\theta predict x0x_0. Since x0x_0 and the noise ε\varepsilon are interchangeable (xt=αˉtx0+1−αˉtεx_t = \sqrt{\bar\alpha_t}x_0 + \sqrt{1-\bar\alpha_t}\varepsilon), predicting x0x_0 and predicting ε\varepsilon are the same task — the authors just found predicting ε\varepsilon trains a bit better.

Following that logic, the answer drops out immediately: minimize the gap between the model's predicted noise εθ\varepsilon_\theta and the true (ground-truth) noise ε\varepsilon — that is, an MSE loss:

L=∥ ε−εθ(xt,t) ∥22.\mathcal{L} = \big\|\,\varepsilon - \varepsilon_\theta(x_t, t)\,\big\|_2^2.

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:

max⁡θ log⁡pθ(x0).\max_\theta\ \log p_\theta(x_0).

From Part 1, the variational bound (the thing we actually optimize) is:

L=Eq ⁣[−log⁡pθ(x0:T)q(x1:T∣x0)].L = \mathbb{E}_q\!\left[-\log \frac{p_\theta(x_{0:T})}{q(x_{1:T}\mid x_0)}\right].

If the step from max⁡log⁡pθ(x0)\max \log p_\theta(x_0) to this bound is fuzzy, go back to Part 1 — that's where it was cleared.

Set the Eq\mathbb{E}_q 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:

pθ(x0:T)=p(xT)∏t=1Tpθ(xt−1∣xt),q(x1:T∣x0)=∏t=1Tq(xt∣xt−1).p_\theta(x_{0:T}) = p(x_T)\prod_{t=1}^{T} p_\theta(x_{t-1}\mid x_t), \qquad q(x_{1:T}\mid x_0) = \prod_{t=1}^{T} q(x_t\mid x_{t-1}).

So:

log⁡pθ(x0:T)q(x1:T∣x0)=log⁡p(xT)∏t=1Tpθ(xt−1∣xt)∏t=1Tq(xt∣xt−1).\log \frac{p_\theta(x_{0:T})}{q(x_{1:T}\mid x_0)} = \log \frac{p(x_T)\prod_{t=1}^{T} p_\theta(x_{t-1}\mid x_t)}{\prod_{t=1}^{T} q(x_t\mid x_{t-1})}.

Using log⁡(ab)=log⁡a+log⁡b\log(ab) = \log a + \log b, every product becomes a sum:

=log⁡p(xT)+∑t=1Tlog⁡pθ(xt−1∣xt)−∑t=1Tlog⁡q(xt∣xt−1).= \log p(x_T) + \sum_{t=1}^{T}\log p_\theta(x_{t-1}\mid x_t) - \sum_{t=1}^{T}\log q(x_t\mid x_{t-1}).

Step 2: Bayes on the qq term (telescoping)

Transform q(xt∣xt−1)q(x_t\mid x_{t-1}) with Bayes' rule:

q(xt∣xt−1)=q(xt−1∣xt) q(xt)q(xt−1).q(x_t\mid x_{t-1}) = \frac{q(x_{t-1}\mid x_t)\,q(x_t)}{q(x_{t-1})}.

(This is exactly why we sweated through Part 3 — you carry your loot into the next level.) The third sum becomes:

∑t=1Tlog⁡q(xt∣xt−1)=∑t=1Tlog⁡q(xt−1∣xt)+∑t=1Tlog⁡q(xt)q(xt−1).\sum_{t=1}^{T}\log q(x_t\mid x_{t-1}) = \sum_{t=1}^{T}\log q(x_{t-1}\mid x_t) + \sum_{t=1}^{T}\log\frac{q(x_t)}{q(x_{t-1})}.

That second piece telescopes. Take the 3-term example (t=3,2,1t = 3, 2, 1):

q(x3)q(x2)⋅q(x2)q(x1)⋅q(x1)q(x0)=q(x3)q(x0),\frac{q(x_3)}{q(x_2)}\cdot\frac{q(x_2)}{q(x_1)}\cdot\frac{q(x_1)}{q(x_0)} = \frac{q(x_3)}{q(x_0)},

so in general ∑t=1Tlog⁡q(xt)q(xt−1)=log⁡q(xT)q(x0)\sum_{t=1}^{T}\log\frac{q(x_t)}{q(x_{t-1})} = \log\frac{q(x_T)}{q(x_0)}. Putting it back:

log⁡pθ(x0:T)q(x1:T∣x0)=log⁡p(xT)+∑t=1Tlog⁡pθ(xt−1∣xt)q(xt−1∣xt)−log⁡q(xT)q(x0).\log \frac{p_\theta(x_{0:T})}{q(x_{1:T}\mid x_0)} = \log p(x_T) + \sum_{t=1}^{T}\log\frac{p_\theta(x_{t-1}\mid x_t)}{q(x_{t-1}\mid x_t)} - \log\frac{q(x_T)}{q(x_0)}.

Step 3: regroup and restore the x0x_0 conditions

Collect the xTx_T pieces, log⁡p(xT)−log⁡q(xT)=log⁡p(xT)q(xT)\log p(x_T) - \log q(x_T) = \log\frac{p(x_T)}{q(x_T)}, and recall that every qq here secretly carries the x0x_0 condition (Part 3's whole point). Adding x0x_0 back:

=log⁡p(xT)q(xT∣x0)+∑t=1Tlog⁡pθ(xt−1∣xt)q(xt−1∣xt,x0)+log⁡q(x0∣x0)⏟= log⁡1 = 0.= \log\frac{p(x_T)}{q(x_T\mid x_0)} + \sum_{t=1}^{T}\log\frac{p_\theta(x_{t-1}\mid x_t)}{q(x_{t-1}\mid x_t, x_0)} + \underbrace{\log q(x_0 \mid x_0)}_{=\,\log 1\,=\,0}.

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 log⁡1=0\log 1 = 0, so it vanishes.

Step 4: why the paper's L0L_0 split is unnecessary

The paper (Eq. 5) writes the bound as three labeled pieces:

L=Eq[DKL(q(xT∣x0) ∥ p(xT))⏟LT+∑t>1DKL(q(xt−1∣xt,x0) ∥ pθ(xt−1∣xt))⏟Lt−1− log⁡pθ(x0∣x1)⏟L0].L = \mathbb{E}_q\Big[\underbrace{D_{\mathrm{KL}}(q(x_T\mid x_0)\,\|\,p(x_T))}_{L_T} + \sum_{t>1}\underbrace{D_{\mathrm{KL}}(q(x_{t-1}\mid x_t, x_0)\,\|\,p_\theta(x_{t-1}\mid x_t))}_{L_{t-1}} \underbrace{-\,\log p_\theta(x_0\mid x_1)}_{L_0}\Big].

The author splits the t=1t=1 term out as a special L0L_0. But that's showmanship — it's unnecessary. Pull t=1t=1 out of our unified sum:

log⁡pθ(x0∣x1)q(x0∣x1,x0)=log⁡pθ(x0∣x1),\log\frac{p_\theta(x_0\mid x_1)}{q(x_0\mid x_1, x_0)} = \log p_\theta(x_0\mid x_1),

because q(x0∣x1,x0)=1q(x_0\mid x_1, x_0) = 1 (the Kunkun joke again — you already know x0x_0). So the special L0L_0 term is just the t=1t=1 case of the general sum. Keep it folded into ∑t=1T\sum_{t=1}^{T} — 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⁡-\log) and the expectation Eq\mathbb{E}_q. A term of the form Eq[log⁡qp]=∫qlog⁡qp\mathbb{E}_{q}\big[\log\frac{q}{p}\big] = \int q\log\frac{q}{p} is a KL divergence (Part 1). So:

L=Eq[DKL(q(xT∣x0) ∥ p(xT))⏟(i)+∑t≥1DKL(q(xt−1∣xt,x0) ∥ pθ(xt−1∣xt))⏟(ii)].L = \mathbb{E}_q\Big[\underbrace{D_{\mathrm{KL}}\big(q(x_T\mid x_0)\,\|\,p(x_T)\big)}_{\text{(i)}} + \sum_{t\ge1}\underbrace{D_{\mathrm{KL}}\big(q(x_{t-1}\mid x_t, x_0)\,\|\,p_\theta(x_{t-1}\mid x_t)\big)}_{\text{(ii)}}\Big].
  • Term (i) has both ends known: q(xT∣x0)q(x_T\mid x_0) from the forward process, and p(xT)p(x_T) 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\mathbb{E}_q doesn't fully cancel into the KL, because the q(x1:T∣x0)q(x_{1:T}\mid x_0) expectation has only "used up" the xt−1,xtx_{t-1}, x_t marginals — the other latents x1,…,xT−1x_1,\dots,x_{T-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)p = \mathcal{N}(\mu_1, \sigma_1^2) (this is q(xt−1∣xt,x0)q(x_{t-1}\mid x_t,x_0)) and q=N(μ2,σ22)q = \mathcal{N}(\mu_2, \sigma_2^2) (this is pθ(xt−1∣xt)p_\theta(x_{t-1}\mid x_t)).

DKL(P∥Q)=∫p(x)log⁡p(x)q(x) dx.D_{\mathrm{KL}}(P\|Q) = \int p(x)\log\frac{p(x)}{q(x)}\,dx.

Trick: keep the leading factor as p(x)p(x) — don't expand it. Only expand the log of the ratio:

log⁡p(x)q(x)=log⁡1σ12πe−(x−μ1)22σ121σ22πe−(x−μ2)22σ22=log⁡σ2σ1−(x−μ1)22σ12+(x−μ2)22σ22.\log\frac{p(x)}{q(x)} = \log\frac{\frac{1}{\sigma_1\sqrt{2\pi}}e^{-\frac{(x-\mu_1)^2}{2\sigma_1^2}}}{\frac{1}{\sigma_2\sqrt{2\pi}}e^{-\frac{(x-\mu_2)^2}{2\sigma_2^2}}} = \log\frac{\sigma_2}{\sigma_1} - \frac{(x-\mu_1)^2}{2\sigma_1^2} + \frac{(x-\mu_2)^2}{2\sigma_2^2}.

So:

DKL=∫p(x)[log⁡σ2σ1−(x−μ1)22σ12+(x−μ2)22σ22]dx.D_{\mathrm{KL}} = \int p(x)\left[\log\frac{\sigma_2}{\sigma_1} - \frac{(x-\mu_1)^2}{2\sigma_1^2} + \frac{(x-\mu_2)^2}{2\sigma_2^2}\right]dx.

Evaluate term by term as expectations under pp:

Term 1 — expectation of a constant is itself: ∫p(x)log⁡σ2σ1 dx=log⁡σ2σ1.\int p(x)\log\tfrac{\sigma_2}{\sigma_1}\,dx = \log\frac{\sigma_2}{\sigma_1}.

Term 2 — ∫p(x)(x−μ1)2 dx=Var⁡p(X)=σ12\int p(x)(x-\mu_1)^2\,dx = \operatorname{Var}_p(X) = \sigma_1^2, so: −12σ12⋅σ12=−12.-\frac{1}{2\sigma_1^2}\cdot\sigma_1^2 = -\frac12.

Term 3 — expand (x−μ2)2=x2−2μ2x+μ22(x-\mu_2)^2 = x^2 - 2\mu_2 x + \mu_2^2 and use: ∫p(x) x dx=μ1,∫p(x) x2 dx=E[X2]=μ12+σ12.\int p(x)\,x\,dx = \mu_1, \qquad \int p(x)\,x^2\,dx = \mathbb{E}[X^2] = \mu_1^2 + \sigma_1^2. 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)2 dx=(μ12+σ12)−2μ2μ1+μ22=σ12+(μ1−μ2)2,\int p(x)(x-\mu_2)^2\,dx = (\mu_1^2+\sigma_1^2) - 2\mu_2\mu_1 + \mu_2^2 = \sigma_1^2 + (\mu_1-\mu_2)^2, and Term 3 =σ12+(μ1−μ2)22σ22= \dfrac{\sigma_1^2 + (\mu_1-\mu_2)^2}{2\sigma_2^2}.

Putting it together — the Gaussian KL formula:

 DKL(N(μ1,σ12) ∥ N(μ2,σ22))=log⁡σ2σ1+σ12+(μ1−μ2)22σ22−12. \boxed{\,D_{\mathrm{KL}}\big(\mathcal{N}(\mu_1,\sigma_1^2)\,\|\,\mathcal{N}(\mu_2,\sigma_2^2)\big) = \log\frac{\sigma_2}{\sigma_1} + \frac{\sigma_1^2 + (\mu_1-\mu_2)^2}{2\sigma_2^2} - \frac12.\,}

Collapsing to MSE

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\sigma_1 = \sigma_2 (both equal the constant β~t\tilde\beta_t). Then:

  • log⁡σ2σ1=log⁡1=0\log\dfrac{\sigma_2}{\sigma_1} = \log 1 = 0,
  • σ122σ22=12\dfrac{\sigma_1^2}{2\sigma_2^2} = \dfrac12, which cancels the −12-\dfrac12,
  • leaving only (μ1−μ2)22σ22\dfrac{(\mu_1-\mu_2)^2}{2\sigma_2^2}.

Since σ22\sigma_2^2 is a constant, the loss is proportional to:

L ∝ (μ1−μ2)2=(μ~t(xt,x0)−μθ(xt,t))2.L \ \propto\ (\mu_1 - \mu_2)^2 = \big(\tilde\mu_t(x_t, x_0) - \mu_\theta(x_t, t)\big)^2.

Both means are functions of (xt,x0)(x_t, x_0), and pθp_\theta has the same functional form as qq — the only difference is that qq uses the true x0x_0 while pθp_\theta uses the model's predicted x^0\hat{x}_0. Plugging the Part 3 expression for μ~t\tilde\mu_t into both and converting x0→εx_0 \to \varepsilon via x^0=(xt−1−αˉt ε^)/αˉt\hat{x}_0 = (x_t - \sqrt{1-\bar\alpha_t}\,\hat\varepsilon)/\sqrt{\bar\alpha_t}, the whole thing simplifies to a noise-prediction MSE:

 L ∝ ∥ ε−εθ(xt,t) ∥22. \boxed{\,L \ \propto\ \big\|\,\varepsilon - \varepsilon_\theta(x_t, t)\,\big\|_2^2.\,}

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 ε\varepsilon, match with MSE) and the principled max-likelihood derivation give the same loss.
  • Drop the constants. Of the variational bound's terms, LTL_T 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 L0L_0 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