Image Generation › Diffusion Models: The Core Idea
Language AI HandbookAll chapters Image Generation › Diffusion Models: The Core IdeaOne section · Sections in this chapter3 / 14 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
The fundamental insight of diffusion models is this: if you have a process that gradually destroys structure, you can learn its reverse to build structure back.
Consider what happens when you gradually add Gaussian noise to an image. At first, the image is recognizable but slightly grainy. After more noise is added, the details blur and only global structure remains. Continue adding noise, and eventually you have something indistinguishable from pure random static, regardless of what the original image was. This forward process destroys information in a principled, mathematically tractable way.
Now flip the perspective. If you could learn to undo each small noise step, you could start with pure random noise and gradually recover a coherent image. You do not need to generate a complete image in one shot; you only need to denoise slightly at each step. This is dramatically more tractable because each denoising step is a small, local change to a partially formed image, rather than a jump from noise to image.
The Forward Process: Adding Noise
The forward process (also called the diffusion process) takes a real image and gradually adds Gaussian noise over timesteps:
where:
- : the noisy image at timestep
- : the image at the previous (less noisy) timestep
- : the noise schedule at timestep , controlling how much noise is added
- : a Gaussian distribution with mean and variance
Each step scales down the signal by (to preserve approximate unit variance) and adds Gaussian noise with variance . The scaling factor ensures that the overall variance of remains roughly constant over time: as signal energy decreases, noise energy increases by the same amount. With a carefully designed noise schedule, after steps (typically ), is approximately pure Gaussian noise, regardless of what was.
A mathematical property makes diffusion computationally tractable: you can compute the noisy image at any arbitrary timestep directly from , without simulating each intermediate step. This is possible because Gaussian distributions have the property that the composition of Gaussians is itself Gaussian. Define and (the cumulative product of all values up to step ). Then:
which means we can sample directly as:
where is pure Gaussian noise. This "reparameterization" has major practical implications. During training, you do not need to simulate the full forward chain from to for every training example. Instead, for each training image you pick a random timestep , sample noise , compute the corresponding using the formula above, and train the model on that single noisy image. This makes training embarrassingly parallel across timesteps.
The Noise Schedule
The noise schedule controls how quickly structure is destroyed as noise is added. Its design has a significant impact on the quality of the learned model.
A natural first choice is a linear schedule: increases linearly from a small value (like ) to a larger value (like ). This is easy to understand, but it has a practical flaw: it destroys too much structure too quickly at low noise levels, where most of the perceptual information lives, and too little at high noise levels. The model ends up spending many timesteps learning to denoise nearly-Gaussian noise, which carries little useful gradient signal for learning image structure.
The cosine schedule, proposed by Nichol and Dhariwal, addresses this by defining directly rather than working through :
where:
- : the total number of timesteps
- : a small offset (typically 0.008) that prevents from being too large near
- The cosine function ensures decreases smoothly from 1 to near 0 following a cosine curve
The cosine schedule keeps near 1 for a longer initial period before dropping more smoothly to near zero at the end. This allocates more timesteps to informative intermediate noise levels, where the model can learn meaningful structure. Empirically, cosine schedules produce better image quality than linear ones, especially for higher-resolution images.
More recent work has explored learned noise schedules (optimizing as parameters) and continuous-time diffusion (defining the noise process as a stochastic differential equation rather than discrete steps), but linear and cosine schedules remain widely used because of their simplicity and reliable performance.
The Reverse Process: Learning to Denoise
The reverse process aims to invert the forward process: starting from pure noise , progressively denoise to recover a clean image . The true reverse conditional distribution exists but is intractable to compute directly, because it requires integrating over all possible clean images consistent with the noisy image . However, if we condition on the clean image, the reverse conditional becomes tractable:
where is the posterior variance. This formula gives the optimal denoising direction when the clean image is known. In practice, we do not know during generation, so we learn a neural network to approximate the reverse conditional:
where is a neural network parameterized by that predicts the mean of the reverse distribution.
Rather than directly predicting the mean, most implementations train the network to predict the noise that was added. This is the -prediction formulation. Given the predicted noise , the reverse step mean becomes:
where:
- : un-scales the signal by reversing the scaling applied in the forward step
- : subtracts the predicted noise contribution, pushing the estimate toward the clean image
The -prediction formulation is used in practice because it produces stable training dynamics: the loss is well-scaled across all timesteps, and the noise prediction task is a well-defined, bounded regression problem. An alternative is -prediction (predicting the clean image directly), which is sometimes more stable at low noise levels, and -prediction (predicting a linear combination of noise and signal), which was introduced for improved training at high resolutions. Modern models often blend these formulations.
The Training Objective
The full diffusion training objective is derived from a variational lower bound on the log-likelihood of the data. Maximizing directly is intractable, but we can instead maximize a lower bound that decomposes into per-timestep KL divergences between the learned reverse process and the true posterior. Ho et al. (2020) showed that a simplified version of this objective works just as well in practice:
where:
- : a uniformly randomly sampled timestep
- : a real training image sampled from the training dataset
- : noise sampled from a standard Gaussian
- : the noisy image at timestep using the reparameterization trick
- : the neural network predicting the noise from the noisy image and timestep
In words: pick a random training image, pick a random timestep, add the corresponding amount of noise, and train the network to predict what noise was added. This is a denoising task framed differently at each timestep. At high , the image is mostly noise and the model must infer global structure. At low , the image is mostly clean and the model must predict the residual fine-grained noise.
The simplification from the full variational lower bound to drops per-timestep weighting that appears in the variational objective, trading theoretical optimality for empirical training stability. In practice, models trained with generate better images than those trained with the full variational bound, likely because the uniform weighting prevents some timesteps from dominating the gradient signal.
Diffusion models are closely related to score-based generative models. The "score" of a distribution is the gradient of the log probability with respect to the data: . If you know the score function everywhere, you can generate samples by starting from any point and following the gradient toward regions of higher probability, a procedure called Langevin dynamics. Song and Ermon (2019) showed how to estimate score functions at multiple noise levels through denoising score matching, and Song et al. (2021) unified this with diffusion models by showing that both processes are solutions to stochastic differential equations. Training a diffusion model to predict noise is mathematically equivalent to learning the score of a noisy data distribution at each noise level. This perspective has been productive for theoretical analysis and for developing improved samplers.
The U-Net Architecture for Noise Prediction
The neural network needs to accept a noisy image and a timestep, and output a same-sized noise prediction. The dominant architecture is a U-Net, originally developed by Ronneberger et al. for biomedical image segmentation, adapted for the diffusion setting.
The U-Net's key structural property is its encoder-decoder architecture with skip connections. The encoder path progressively downsamples the image using strided convolutions or pooling, building a hierarchy of features from fine-grained local patterns to coarse global structure. The decoder path progressively upsamples back to the original resolution using transposed convolutions or bilinear upsampling. Skip connections copy feature maps directly from each encoder stage to the corresponding decoder stage, allowing fine-grained spatial details to flow directly to the decoder. Without skip connections, the encoder bottleneck would force all spatial information through the narrow representation, causing loss of fine detail.
For diffusion, the U-Net is adapted with several modifications:
- Timestep embeddings: the scalar timestep is embedded via sinusoidal positional encodings (similar to those we discussed in Part XIV on positional encoding) and projected through small feed-forward networks. These embeddings are added to the feature maps at each resolution level, allowing the network to know at which noise level it is operating.
- Residual blocks: each U-Net block wraps its core convolutions in residual connections, improving gradient flow during training and allowing deeper networks.
- Self-attention layers: attention layers are added at lower resolutions where the spatial dimensions are small, making attention computationally feasible. These allow the network to model long-range dependencies between distant spatial locations. A global composition constraint, for example a sky being blue and grass being green, requires correlating distant pixels.
- Group normalization: normalization is applied within feature groups rather than across batch dimensions, which is more stable for the small batch sizes used in diffusion training.
The result is a flexible model that can process images at their full resolution while conditioning on the noise level. Because the same network handles all timesteps, it learns a range of denoising behaviors: at high noise (early in generation), it focuses on global structure and low-frequency signals; at low noise (late in generation), it refines fine details and textures. The model implicitly learns a curriculum from coarse to fine.