AI State of the Art
Fundamentals/Deep Learning: Fundamental Architectures/33 min
Section 27

Diffusion Models: Fundamental Principles

Listen as a podcast →

The simple and elegant idea

The fundamental principle of diffusion models can be summarized in two steps: take an image and progressively add noise until you get pure random Gaussian noise. Then learn to reverse this process: start from pure noise, remove noise step by step, and reconstruct a coherent image.

It's a deceptively simple idea. And yet, it triggered a revolution. In 2022, Stable Diffusion, DALL-E 2, and Midjourney showed that this approach produces better images than Generative Adversarial Networks (GANs) and sharper ones than Variational Autoencoders (VAEs). Where a GAN rests on a fragile balance between two competing networks, and a VAE produces blurry images, diffusion has just one network to train and one target to hit.

The founding paper goes by the name DDPM, for Denoising Diffusion Probabilistic Models. Most of the numbers quoted on this page come from it.

Denoising Diffusion Probabilistic Models
What the DDPM paper reports (2020)

Two figures from the paper's abstract, to anchor what follows. On CIFAR-10 (a set of 60,000 small 32x32-pixel images) in unconditional generation, the authors report an Inception Score of 9.46 and an FID of 3.17. The Inception Score (IS) reads upward: the higher, the better. The FID (Fréchet Inception Distance) reads downward: the lower, the better. Both are computed by running images through a reference classification network, and neither ever compares across image sets.

The abstract also notes that on a 256x256 set of scene photographs, sample quality was "similar to ProgressiveGAN," a GAN of that era. In other words: in 2020 diffusion matched GANs; overtaking them came later.

Theatre d Opera Spatial, the Midjourney image by Jason Allen: robed figures seen from behind face a glowing circular opening at the end of a monumental hall blending baroque and futuristic styles.
Théâtre d'opéra spatial, the Midjourney picture that won an art competition in Colorado in 2022 and set off a public row. This level of detail and light is exactly what diffusion made possible that year.Source : Wikipédia

Forward process: from signal to noise

Transformation of an image into noise via forward process
Read the strip of pictures from left to right: the dog picks up more grain at every frame until nothing is left but television snow. The top arrow describes that decay, which is easy to program; the bottom arrow runs the other way, and that is the direction the model has to learn.Source : SuperAnnotate

The step-by-step progression

The forward process is fixed and not learned: no network weights are involved, only constants decided in advance. It's simply the progressive addition of Gaussian noise. At each step tt (from 1 to TT; the DDPM paper uses T=1000T = 1000 in its experiments), you mix the image with a bit of noise:

xt=αt⋅xt−1+1−αt⋅ϵtx_t = \sqrt{\alpha_t} \cdot x_{t-1} + \sqrt{1 - \alpha_t} \cdot \epsilon_t

Symbol by symbol: tt is the step number; xtx_t is the image after tt noising steps, xt−1x_{t-1} the one from the previous step; αt\alpha_t (the Greek letter alpha) is a number between 0 and 1 fixed in advance for this step; ϵt\epsilon_t (epsilon) is the noise added at this step, drawn as ϵ∼N(0,I)\epsilon \sim \mathcal{N}(0, I), the normal distribution centred on zero with each coordinate of width 1;  \sqrt{\ } is the square root, whose purpose is explained just below.

The intuition is that of a mixer: αt\alpha_t controls the proportion between signal and noise at each step. When αt\alpha_t is close to 1, you mostly keep the existing signal and add very little noise. When αt\alpha_t is small, you drown the signal under the noise.

Why square roots? This is a variance-preserving process: the coefficients αt\sqrt{\alpha_t} and 1−αt\sqrt{1 - \alpha_t} guarantee that the total variance of xtx_t stays at 1 at each step. Indeed, if xt−1x_{t-1} has variance 1 and ϵt\epsilon_t does too, then Var(xt)=(αt)2+(1−αt)2=αt+(1−αt)=1\text{Var}(x_t) = (\sqrt{\alpha_t})^2 + (\sqrt{1 - \alpha_t})^2 = \alpha_t + (1 - \alpha_t) = 1. Without these square roots (if you directly used αt\alpha_t and (1−αt)(1 - \alpha_t) as coefficients) the variance of xtx_t would be αt2+(1−αt)2≠1\alpha_t^2 + (1-\alpha_t)^2 \neq 1. The signal would swell or collapse over steps, making the process unstable. The square roots are therefore the key to keeping each step properly normalized.

The noise schedule

The amount of noise added at each step is set by the noise schedule: the list, decided before training, of the 1000 values of βt=1−αt\beta_t = 1 - \alpha_t, where βt\beta_t (beta) is simply the share of noise added at step tt. Two common schedules:

Linear schedule, the one in the original DDPM paper: β\beta increases in a straight line from 0.00010.0001 to 0.020.02. So the noising is very light at first (preserving fine details), then accelerates. Simple and effective.

Cosine schedule: smoother progression, particularly useful for high-resolution images. The noising is more uniform throughout the process, which avoids destroying the image's large structures too quickly.

Jumping directly to step t

Crucial mathematical property: thanks to the properties of the Gaussian, you can compute directly xtx_t from x0x_0 without going through all intermediate steps.

Let's first define αˉt\bar{\alpha}_t: it's the cumulative product of all α\alpha values up to step tt:

αˉt=∏s=1tαs=α1⋅α2⋅…⋅αt\bar{\alpha}_t = \prod_{s=1}^{t} \alpha_s = \alpha_1 \cdot \alpha_2 \cdot \ldots \cdot \alpha_t

Intuitively, αˉt\bar{\alpha}_t measures how much of the original signal survives after tt noise steps. Since each αs<1\alpha_s < 1, this product decreases over steps: at the beginning αˉt\bar{\alpha}_t is close to 1 (signal dominates), at the end it tends toward 0 (noise completely dominates).

With this definition, the direct jump formula is:

xt=αˉt⋅x0+1−αˉt⋅ϵx_t = \sqrt{\bar{\alpha}_t} \cdot x_0 + \sqrt{1 - \bar{\alpha}_t} \cdot \epsilon

with ϵ∼N(0,I)\epsilon \sim \mathcal{N}(0, I).

Why can you jump directly from x0x_0 to xtx_t? Because the sum of independent Gaussians is itself a Gaussian. Each forward step adds Gaussian noise, and by "unrolling" the recurrence over tt steps, all these individual noises combine into a single Gaussian term. You can therefore compute the result of tt noising steps in a single operation.

Why is this crucial for training? Because during training, you need to noise an image to a randomly drawn step tt. Without this formula, getting x500x_{500} would mean chaining 500 steps one by one. With it, a single computation suffices, whatever tt is.

Progressive destruction in images

At step t=0t = 0: the original image, intact. At step t=100t = 100: the image is blurry, fine details have disappeared but the major structures are visible. At step t=500t = 500: you can vaguely make out shapes, dominant colors. At step t=T=1000t = T = 1000: pure noise, indistinguishable from N(0,I)\mathcal{N}(0, I). All information has been destroyed. But each individual step only added a small perturbation, which makes the process potentially reversible step by step. This is where the magic happens.

Reverse process: learned denoising

The objective

Reverse the forward process. Start from pure noise xTx_T and go back step by step to a clean image x0x_0.

Mathematically, this means learning p(xt−1∣xt)p(x_{t-1} | x_t), the vertical bar reading "given": the probability of the image at step t−1t-1 given the one at step tt. In other words: "given this noisy image at step tt, what did it look like at the previous step, with just a tiny bit less noise?" If you can answer this question for each tt, you can go back from xTx_T (noise) to x0x_0 (image) step by step.

The denoising network

In practice, you train a network (typically a U-Net, described below) to predict the noise that was added:

ϵθ(xt,t)≈ϵ\epsilon_\theta(x_t, t) \approx \epsilon

Symbol by symbol: ϵ\epsilon (epsilon) is the true noise, the one actually added and therefore known during training; ϵθ\epsilon_\theta is the noise predicted by the network; the subscript θ\theta (the Greek letter theta) stands for the whole set of the network's learned weights, a universal convention in machine learning, and the point not to miss here. xtx_t and tt are the network's two inputs; the sign ≈\approx means "should come close to".

What is learned and what is fixed

Three kinds of object coexist in these formulas, and mixing them up makes the page unreadable.

  • Learned: only θ\theta, the denoising network's weights. That is the sole part of the formula training modifies.
  • Fixed in advance: the noise schedule, i.e., all the αt\alpha_t and βt\beta_t. These are constants the engineer picks before starting; the model does not learn them.
  • Drawn at random or computed on the fly: the noise ϵ\epsilon, the step tt, and hence the noisy image xtx_t. They change with every training example.

The network receives as input the noisy image xtx_t along with an encoding of the timestep tt (as sinusoids, like the position encoding in transformers, see positional encoding). Its task: predict the Gaussian noise ϵ\epsilon that was added to obtain xtx_t.

Why predict noise rather than the image?

This is a parameterization choice, that is, "how to formulate the problem." You could train the network to predict x0x_0 directly, or xt−1x_{t-1}, and mathematically it would be equivalent: knowing ϵ\epsilon lets you recover x0x_0 by rearranging the direct-jump formula, and vice versa. But predicting the noise turns out to be more stable in practice, for a simple reason: the target is always noise of the same strength, whatever tt is. Predicting x0x_0, by contrast, is an almost trivial task when tt is small and an almost impossible one when tt is large, the network would have to learn two very different jobs through the same output.

DDPM training: remarkable simplicity

The training algorithm is almost strikingly simple:

  1. Sample an image x0x_0 from the dataset
  2. Draw a random timestep tt from {1,…,T}\{1, \ldots, T\}
  3. Draw noise ϵ∼N(0,I)\epsilon \sim \mathcal{N}(0, I)
  4. Compute xtx_t via the forward formula (a single computation, no need to iterate)
  5. Pass xtx_t and tt through the network, get the prediction ϵ^\hat{\epsilon}
  6. Minimize: L=∥ϵ−ϵ^∥2\mathcal{L} = \| \epsilon - \hat{\epsilon} \|^2

That's it. A simple mean squared error (MSE) between the true noise and the predicted noise: subtract, square, add up. No complex loss, no second adversarial network, no delicate balancing act.

Let's compare with the alternatives. A GAN demands a fragile balance between generator and discriminator, suffers from mode collapse (the generator produces only a small part of the possible images), and is notoriously difficult to train. A VAE forces a delicate tradeoff between its reconstruction loss and its Kullback-Leibler regularization, the KL, a measure of how far apart two probability distributions are, see its own lesson and often produces blurry images. Diffusion, meanwhile: a single network, a single loss, and training that behaves well in practice. One caution though: "behaves well" is not "converges for certain", no deep-network training offers that guarantee. It is this robustness that allowed scaling to resolutions and quality levels out of reach for GANs.

The beauty of simplicity

Each training step is essentially a classic supervised denoising task: "here's a noisy image, predict the noise." Nothing more. This simplicity makes diffusion extremely robust and easy to reproduce, unlike GANs which require complex training tricks.

U-Net architecture: the denoising network

U-Net architecture diagram for diffusion models
The blue hourglass shape in the middle is the U-Net: it squeezes the image down, then widens it back out, and the horizontal grey arrows are the shortcuts that carry the fine detail straight across. On the left, the encoder and decoder handle the trip to and from the visible image; on the right, the text prompt steers the denoising.Source : Towards Data Science

The standard denoising network is a U-Net: the name comes from its shape, a U you descend one branch of and climb back up the other. It originated in medical image segmentation and serves here because it returns an image the same size as the one you hand it, which is exactly what denoising needs.

  • Encoder (downward branch): convolutions that reduce spatial resolution while increasing the number of channels. The 256x256 image becomes 128x128, then 64x64, etc.
  • Bottleneck (lowest point): minimum resolution, maximum channels. This is where the network takes in the whole image at a glance and can grasp its overall structure.
  • Decoder (upward branch): progressive climb back to the original resolution.
  • Skip connections between encoder and decoder at each resolution: they pass information straight across from one side of the U to the other, bypassing the bottleneck. Without them, fine details (textures, edges) would be lost in the squeeze.
  • Attention blocks at intermediate resolutions: self-attention, the mechanism that lets every position in the image consult every other, to pick up relationships between distant regions.
  • Temporal conditioning: the step tt is encoded as sinusoids and injected into each layer. This is what lets the network know how much noise it should remove, the same noisy image calls for different treatment at step 50 than at step 900.

Inference: generating images from nothing

The generation process is the reverse of the forward:

  1. Sample pure noise: xT∼N(0,I)x_T \sim \mathcal{N}(0, I)
  2. For each step t=T,T−1,…,1t = T, T-1, \ldots, 1:
    • Predict the noise: ϵ^=ϵθ(xt,t)\hat{\epsilon} = \epsilon_\theta(x_t, t)
    • Compute xt−1x_{t-1} using ϵ^\hat{\epsilon} and the schedule parameters
    • Add a small stochastic noise (except at the last step t=1t = 1) to maintain diversity

The result: a realistic synthetic image, progressively built from pure noise.

The major tradeoff: slowness

Main disadvantage: with the original settings, generating one image means passing through the U-Net 1000 times in a row, each pass being a full computation. The arithmetic is direct: 1000 steps against a single pass for a GAN, so a factor of roughly 1000 in the number of operations. That gap (rather than a figure in seconds, which would depend on the hardware) is what explains the race for acceleration described below.

Acceleration techniques

Several approaches have been developed to drastically reduce this cost:

DDIM (Denoising Diffusion Implicit Models): a deterministic reformulation of denoising (that is, with no random draw at each step) which allows "skipping" intermediate steps. The paper shows respectable results at around 50 to 100 steps. The idea: instead of following the full trajectory with its random zigzags, you follow a smoother, more direct path.

DPM-Solver: treats denoising as solving an ordinary differential equation (ODE: an equation describing how a quantity evolves continuously, here the image along its denoising trajectory). Instead of a thousand naive little steps, you use finer numerical methods that cover several steps at once. The authors report comparable quality at 10 to 20 steps.

Consistency distillation: you train a "student" model to reproduce in one to four steps what the "teacher" model does in a thousand. The student learns to jump straight from noise to the final image, summarizing the entire denoising trajectory.

The link with score estimation

There's a deep mathematical link between diffusion and score matching. Predicting noise ϵ\epsilon amounts to estimating the score of the noised distribution, the score being the gradient of the log probability density, which put simply is the direction in which real data gets denser. Song et al. brought diffusion models, score matching, and stochastic differential equations (SDEs: differential equations with an added randomness term) under one framework. All of that is developed in the lesson on score-based models and flow matching.

Guidance: controlling what the model generates

Classifier guidance

First approach: use an already-trained classifier to "guide" denoising toward a target class. At each step, you add the direction the classifier points in to the denoising process, pushing the image under construction toward the desired class. The tradeoff: the more you guide, the more faithful the images are to the requested class and the less varied they become.

Classifier-free guidance (the current standard)

Often abbreviated CFG. A leaner approach that requires no external classifier. During training, the model learns to denoise with and without the instruction (text, class, etc.), all it takes is hiding the instruction on a fraction of the examples. At generation time, you combine the two predictions:

ϵguided=ϵuncond+w⋅(ϵcond−ϵuncond)\epsilon_{\text{guided}} = \epsilon_{\text{uncond}} + w \cdot (\epsilon_{\text{cond}} - \epsilon_{\text{uncond}})

Symbol by symbol: ϵuncond\epsilon_{\text{uncond}} is the noise predicted without the instruction (unconditional); ϵcond\epsilon_{\text{cond}} the noise predicted with it; ϵguided\epsilon_{\text{guided}} the noise actually used for the denoising step; ww is a hand-set dial called the guidance scale.

Why does the difference work? The term (ϵcond−ϵuncond)(\epsilon_{\text{cond}} - \epsilon_{\text{uncond}}) isolates the instruction's own contribution: the unconditional model predicts "generic" noise, the conditional model predicts noise steered by the instruction, so their difference is the instruction's net effect. Multiplying that difference by ww amplifies it. Concretely: w=0w = 0 amounts to ignoring the instruction entirely, w=1w = 1 recovers exactly the normal conditional behaviour (you subtract ϵuncond\epsilon_{\text{uncond}} only to add it straight back), and w>1w > 1 exaggerates the requested features, images that match the instruction better, but vary less. Values used in practice typically sit between 5 and 15 depending on the model, and are worth tuning rather than copying. This is the mechanism that lets Stable Diffusion and DALL-E turn a prompt, the natural-language instruction the user writes, into an image.

Latent diffusion: scaling up

Stable Diffusion doesn't work on pixels but in the latent space of a VAE trained in advance. The VAE's encoder compresses the image into a much smaller representation, diffusion operates entirely in that reduced space, and then the VAE's decoder climbs back to the final image.

The gain can be computed by hand. A 512x512 colour image is 512 × 512 × 3 = 786,432 numbers. Its latent version, in the most widespread configuration, is 64x64 with 4 channels, i.e., 64 × 64 × 4 = 16,384 numbers. The ratio is 786,432 ÷ 16,384 = 48. That factor of 48 is what makes diffusion practical at high resolution: without it, noising and denoising an image pixel by pixel a thousand times over would cost nearly fifty times as much.

Summary

Diffusion models rest on a remarkably simple idea: progressively noise data until pure noise, a fixed process, with nothing to learn, then learn to reverse it with a U-Net. Training is simple and robust: a single loss, the mean squared error between the noise added and the noise predicted, against the delicate balancing acts of a GAN. Generation was slow at first (a thousand steps), but DDIM, DPM-Solver, and distillation have brought that down to a few dozen, or even a handful. Classifier-free guidance allows conditioning generation on text. Since 2022, diffusion models dominate image generation; Stable Diffusion, DALL-E 2, and Midjourney all descend from them. For the rest of the story, unification with score estimation, then the flow matching that dominates today, see the next lesson. For how diffusion was adapted to text, where noise becomes masking, see diffusion language models.

Check your understanding0 / 5

1. In a diffusion model, what exactly does training modify?

2. Why train the network to predict the added noise rather than the original image, when the two formulations are mathematically equivalent?

3. During training, why is the direct-jump formula (computing x_t from x_0 in a single operation) crucial?

4. With classifier-free guidance (CFG), what happens when you push the guidance scale w above 1?

5. Why does Stable Diffusion run diffusion in the latent space of a VAE rather than directly on pixels?