Diffusion models
The forward pass (image to noise)
The forward process can be described with a sequence of gaussian distributions:
q(xt∣xt−1)=N(xt;1−βtxt−1,βtI)
Assume βt is the weight for gaussian noise (variance) at step t, and αt=1−βt, αˉt=∏i=1tαi.
The forward step gradually adds a sequence of gaussian noise {ϵt}∼N(0,I) to the real image x0, so the corrupted image at step t is:
xt=αtxt−1+1−αtϵ1=αt(αt−1xt−2+1−αt−1ϵ2)+1−αtϵ1=αtαt−1xt−2+αt(1−αt−1)ϵ2+1−αtϵ1=αtαt−1xt−2+1−αtαt−1ϵ2=⋯=αˉtx0+1−αˉtϵt
Note that two independent gaussian noise ϵ1,ϵ2 can be merged into one:
N(0,αt(1−αt−1))+N(0,1−αt)→N(0,1−αtαt−1)
This means we can express any xt with x0, {β1,β2,⋯,βt}, and a random noise ϵt.
The backward pass (noise to image)
Given xt, we want to get xt−1, and eventually restore the real image x0.
It's proven that if βt is small enough, the backward process is also a sequence of gaussian.
q(xt−1∣xt)=N(xt−1;μ~t(xt,t),β~tI)
which gives the denoising step with another sequence of gaussian noise {zt}∼N(0,I):
xt−1=μ~t(xt,t)+β~tzt
Through some math we could get:
β~t=1−αˉt1−αˉt−1βtμ~t(xt,t)=αt1(xt−1−αˉt1−αtϵt)
Note that here ϵt is the exact noise added in the forward step!
And all we need is a neural network that predicts this noise ϵt given xt(x0,t)=αˉtx0+1−αˉtϵt.
Assume the predicted noise is ϵθ, we need to minimize the following loss:
L=Et,x0,ϵt∣∣ϵt−ϵθ(x0,t)∣∣2
The neural network is usually an U-Net with attention since we need same-size output as input.
DDPM (Denoising Diffusion Probabilistic Model)
DDPM exactly describes the above idea.
Note that DDPM use β~t=βt as an approximation.

DDIM (Denoising Diffusion Implicit Model)
DDIM use a different formula for the backward step:
xt−1=αˉt−1x0+1−αˉt−1ϵt−1=αˉt−1x0+1−αˉt−1−σt2ϵt+σtzt=αˉt−1x0+1−αˉt−1−σt21−αtxt−αtx0+σtzt
According to xt−1=μ~t(xt,t)+β~tzt, we have
μ~t(xt,t)=αˉt−1x0+1−αˉt−1−σt21−αtxt−αtx0β~t=σt2=1−αˉt1−αˉt−1βt
Another hyper-parameter η≥0 is introduced as ηβ~t=σt2.
DDPM usually requires T=1000 steps for both training and inference (S=T=1000), which makes generation (inference) too slow.
To speed up inference, DDIM use strided sampling where S<T, and shows when η=0, S=50∼100 steps are enough to generate good images.
LDM (Latent Diffusion Model)
Instead of diffusing in the pixel space, LDM first use a VQVAE to compress the image space (e.g., 512x512x3) into a latent space (e.g., 64x64x4).
This greatly reduces the computation cost for both training and testing.
Conditioned Generation
Assume we want to condition the generation to an input y which may be class labels or text.
Classifier-Guided Diffusion
A pretrained classifier is required, not flexible.
Classifier-Free Guidance
Train both conditioned denoiser ϵθ(x0,t) and unconditioned denoiser ϵθ(x0,t,y) in the same neural network together.
The final denoiser is simply a weighted combination of these two terms:
ϵˉθ(x0,t,y)=ϵθ(x0,t,y)+w(ϵθ(x0,t,y)−ϵθ(x0,t))=(w+1)ϵθ(x0,t,y)−wϵθ(x0,t)
where a larger w leads to stronger class guidance.