Search

Search papers and lessons

Diffusion Model

Level 4 · 17 min read

A diffusion model learns to generate data by reversing a fixed process that gradually turns data into noise, training a network to predict the noise at each step.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Linear algebra
  • Discrete math
  • Generative models
  • Inference
  • Sign in to see your progress

TL;DR

A diffusion model generates data by learning to undo a corruption process. Training starts from real data and adds a little Gaussian noise at each of many steps until the result is pure noise. A neural network is then trained to look at a noisy input and predict the noise that was added, so that the process can be run backwards, from noise to a clean sample.

The forward process is fixed and needs no learning. All the learning is in the one denoising network, trained with a simple mean squared error, which is why diffusion models are stable to train compared with adversarial models.

Where you'll see it: in image generators such as Stable Diffusion and in video and audio synthesis, where a model starts from random noise and refines it into a picture or clip over a number of denoising steps.

Intuition

Learning to clean up a photograph

Imagine a photograph that has been slowly covered in static, a little at a time, until nothing of the picture is left. Each step of the static is small and easy to describe. Now imagine a model that has seen many such photographs at every stage of the damage. Given a slightly noisy image, it can say which part is noise and which part is picture. Subtract the noise it predicts, and the image becomes a little clearer.

Repeat that many times from pure static and an image appears. That is the whole idea. The model never has to draw a face in one go. It only has to make one small, reliable decision: how much of what it sees is noise.

Why does this work when a single jump from noise to image does not? Because the small steps stay close to a Gaussian shape, so the model's job at each step is simple and well posed. A sudden jump would require the model to invent a whole image from nothing, which is a far harder and more ambiguous question.

flowchart LR
    X0[clean image x0] -->|add noise step 1| X1[x1]
    X1 -->|add noise| X2[x2]
    X2 -->|...| XT[pure noise xT]
    XT -->|denoise with network| Y2[y2]
    Y2 -->|denoise| Y1[y1]
    Y1 -->|denoise| Y0[new sample]

The top row is fixed and known. The bottom row is what the trained network runs, from noise back to a sample.

00.20.40.60.8100.20.40.60.81fraction of the schedule (t / T)coefficient
  • signal, sqrt of alpha-bar
  • noise, sqrt of 1 - alpha-bar
With a linear noise schedule over T = 1000 steps, the signal shrinks and the noise grows as the process runs. The two cross roughly a quarter of the way along, so the noise has taken over well before the end.

The math

Setup

Let x0x_0 be a data point, such as an image flattened into a vector. The forward process adds noise over TT steps. A schedule β1,…,βT\beta_1, \dots, \beta_T with each βt∈(0,1)\beta_t \in (0, 1) says how much noise each step adds. Define αt=1−βt\alpha_t = 1 - \beta_t and the running product αˉt=∏s=1tαs\bar\alpha_t = \prod_{s=1}^{t} \alpha_s.

Each step is a Gaussian draw from N(μ,σ2I)\mathcal{N}(\mu, \sigma^2 I), where II is the identity matrix:

q(xt∣xt−1)=N ⁣(xt; 1−βt xt−1, βtI).q(x_t \mid x_{t-1}) = \mathcal{N}\!\left(x_t;\ \sqrt{1-\beta_t}\, x_{t-1},\ \beta_t I\right).

The scaling 1−βt\sqrt{1-\beta_t} shrinks the old signal slightly, so the total variance stays bounded and the process settles to a standard Gaussian.

The closed form for any step

Each step is linear and Gaussian, so composing tt of them gives a single Gaussian. The result is that we can jump straight to any step tt:

q(xt∣x0)=N ⁣(xt; αˉt x0, (1−αˉt)I).q(x_t \mid x_0) = \mathcal{N}\!\left(x_t;\ \sqrt{\bar\alpha_t}\, x_0,\ (1-\bar\alpha_t) I\right).

Equivalently, with noise ε∼N(0,I)\varepsilon \sim \mathcal{N}(0, I),

xt=αˉt x0+1−αˉt ε.x_t = \sqrt{\bar\alpha_t}\, x_0 + \sqrt{1-\bar\alpha_t}\, \varepsilon.

This is the key to training. We never run the chain step by step during learning. We pick a random tt, draw ε\varepsilon, and form xtx_t in one line. The reparameterised form above is the same trick described in Reparameterization Trick.

The reverse process

The reverse chain is also modelled as Gaussian:

pθ(xt−1∣xt)=N ⁣(xt−1; μθ(xt,t), σt2I).p_\theta(x_{t-1} \mid x_t) = \mathcal{N}\!\left(x_{t-1};\ \mu_\theta(x_t, t),\ \sigma_t^2 I\right).

The true reverse step is hard to write down, but conditioning on x0x_0 it has a closed form. Its mean is a function of xtx_t and the noise ε\varepsilon that produced it. This suggests parameterising the network to predict ε\varepsilon, so the mean becomes

μθ(xt,t)=1αt(xt−βt1−αˉt εθ(xt,t)).\mu_\theta(x_t, t) = \frac{1}{\sqrt{\alpha_t}}\left(x_t - \frac{\beta_t}{\sqrt{1-\bar\alpha_t}}\, \varepsilon_\theta(x_t, t)\right).

Here εθ\varepsilon_\theta is the denoising network, and σt\sigma_t is a fixed variance chosen from the schedule.

The training objective

Training comes from the evidence lower bound on the data log likelihood. Once the terms are rewritten using the closed forms above, the bound becomes a sum over steps of squared errors between the true noise and the predicted noise. Ho and colleagues found that dropping the per-step weights gives a simpler objective that trains better:

Lsimple=Et, x0, ε[ ∥ε−εθ ⁣(αˉt x0+1−αˉt ε, t)∥2].L_{\text{simple}} = \mathbb{E}_{t,\, x_0,\, \varepsilon}\left[\, \left\| \varepsilon - \varepsilon_\theta\!\left(\sqrt{\bar\alpha_t}\, x_0 + \sqrt{1-\bar\alpha_t}\, \varepsilon,\ t\right) \right\|^2 \right].

This is a Mean Squared Error loss. Each training step samples a data point, a time tt, and a noise draw, then takes a gradient step on the squared error.

Connection to score matching

The predicted noise is closely tied to the gradient of the log density of the noisy data, the score. For Gaussian noise, εθ(xt,t)≈−1−αˉt ∇xtlog⁡q(xt)\varepsilon_\theta(x_t, t) \approx -\sqrt{1-\bar\alpha_t}\, \nabla_{x_t} \log q(x_t). A diffusion model is therefore a noise-level-indexed family of score models, which is why the same picture appears in the score-based formulation of Song and colleagues.

Worked examples

Example
One forward step, by hand

Take a single number as the data, x0=2x_0 = 2, with schedule β1=0.1\beta_1 = 0.1. Draw noise ε=0.5\varepsilon = 0.5.

  1. Then α1=0.9\alpha_1 = 0.9 and αˉ1=0.9\bar\alpha_1 = 0.9.
  2. Signal coefficient: 0.9=0.9487\sqrt{0.9} = 0.9487, so the signal is 0.9487×2=1.89740.9487 \times 2 = 1.8974.
  3. Noise coefficient: 1−0.9=0.3162\sqrt{1 - 0.9} = 0.3162, so the noise is 0.3162×0.5=0.15810.3162 \times 0.5 = 0.1581.
  4. Sum: x1=1.8974+0.1581=2.0555x_1 = 1.8974 + 0.1581 = 2.0555.
  5. Interpretation: after one step the value moved only slightly toward the noise. Most of the signal survives, which is why the earliest steps are easy to undo.
Example
One training term

Continue the same toy schedule to a second step with β2=0.2\beta_2 = 0.2, so α2=0.8\alpha_2 = 0.8 and αˉ2=0.9×0.8=0.72\bar\alpha_2 = 0.9 \times 0.8 = 0.72. Take x0=2x_0 = 2 and noise ε=−0.3\varepsilon = -0.3. The forward draw is

  1. 0.72×2=0.8485×2=1.6971\sqrt{0.72} \times 2 = 0.8485 \times 2 = 1.6971.
  2. 0.28×(−0.3)=0.5292×(−0.3)=−0.1587\sqrt{0.28} \times (-0.3) = 0.5292 \times (-0.3) = -0.1587.
  3. So x2=1.6971−0.1587=1.5384x_2 = 1.6971 - 0.1587 = 1.5384.
  4. Suppose the network, given x2x_2 and t=2t = 2, predicts εθ=−0.2\varepsilon_\theta = -0.2. The loss term is (ε−εθ)2=(−0.3+0.2)2=0.01(\varepsilon - \varepsilon_\theta)^2 = (-0.3 + 0.2)^2 = 0.01.
  5. Interpretation: the gradient step nudges the network to predict more negative noise for inputs like this one. Averaged over many tt, x0x_0 and ε\varepsilon, these small corrections shape the whole denoising behaviour.

Tradeoffs and failure modes

Diffusion models give high sample quality and stable training. Their costs are in sampling speed and in the schedule and parameterisation choices.

SituationProblemBetter choice
Interactive generationHundreds of network calls per sampleFewer-step samplers, or distillation into a faster model
Very high resolution imagesCompute grows fast with pixel countWork in a compressed space, as in [[latent-diffusion-model
Need exact likelihoodsDiffusion gives only a bound on the likelihoodNormalising flows, or accept the bound
Schedule too aggressiveSignal disappears too early and the model learns little at the endTune the schedule, for example a cosine schedule
Need a fast single-pass generatorIterative sampling is the bottleneckA GAN or a one-step student model

Two failure modes are common. Sampling with too few steps or a poor variance choice gives blurry or washed-out outputs. Training on a small dataset can lead the network to memorise examples, which shows up as near-copies of the training set.

History

Sohl-Dickstein and colleagues proposed the idea in 2015, drawing on nonequilibrium thermodynamics and the way systems diffuse toward equilibrium. Their model was not competitive with the GANs of the time. In 2020 Ho, Jain and Abbeel showed that a simple reweighting of the bound, together with a U-Net denoiser, produced image quality that rivalled the best generative models, and the objective LsimpleL_{\text{simple}} above became the standard recipe. Song and colleagues later unified the approach with score-based models and stochastic differential equations, which opened faster samplers. Latent diffusion moved the process into a compressed space and made large image generators practical on modest hardware.

Check yourself

QuestionRecall: what does the network εθ\varepsilon_\theta predict?Show answer

The noise ε\varepsilon that was added to the clean data to produce the noisy input xtx_t at step tt. The network receives xtx_t and the time tt, and its output is compared with the true ε\varepsilon in the mean squared error loss.

QuestionCompute: with x0=2x_0 = 2, αˉt=0.5\bar\alpha_t = 0.5 and ε=1\varepsilon = 1, what is xtx_t?Show answer

xt=0.5×2+0.5×1=0.7071×2+0.7071=1.4142+0.7071=2.1213x_t = \sqrt{0.5} \times 2 + \sqrt{0.5} \times 1 = 0.7071 \times 2 + 0.7071 = 1.4142 + 0.7071 = 2.1213. Signal and noise have equal weight at this step, because αˉt=0.5\bar\alpha_t = 0.5 gives equal coefficients on the two terms.

QuestionWhy: why can training pick a random tt instead of running the whole chain?Show answer

The closed form xt=αˉt x0+1−αˉt εx_t = \sqrt{\bar\alpha_t}\, x_0 + \sqrt{1-\bar\alpha_t}\, \varepsilon lets us sample any noise level directly from clean data. Each training example then costs one forward pass of the network, and random tt gives an unbiased estimate of the full objective summed over all steps.

QuestionApplied: you want the model to denoise faster at sampling time while keeping the same network. What can you change, and what does it cost?Show answer

Use a sampler that takes fewer, larger steps, such as a strided or ODE-based sampler, instead of all TT steps of the ancestral sampler. The network stays the same, and the cost is some loss of sample quality, because the larger steps approximate the reverse chain less accurately. Sampling quality should be checked against the full-step result on held-out prompts or inputs.

Sources