Denoising Diffusion Probabilistic Models
DDPMs generate images by taking pure television static and iteratively removing the noise step-by-step until a clear image emerges.
Why Does This Exist?
For a long time, the generative AI field was dominated by Generative Adversarial Networks (GANs). GANs could generate incredibly realistic images, but they suffered from mode collapse—meaning they would just memorize a few good examples and produce the same things repeatedly. Furthermore, GANs were notoriously unstable during training.
Denoising Diffusion Probabilistic Models (DDPMs) emerged as a rigorously mathematical alternative that traded the adversarial game for a more stable, thermodynamic-inspired process. Instead of training two neural networks to fight each other, DDPMs rely on a single neural network that learns to perform a very specific task: removing a tiny amount of noise from an image. By chaining this simple denoising operation hundreds of times, a DDPM can start with pure random static and slowly carve it into a highly detailed, coherent image. This process ensures stable training, mathematically predictable likelihoods, and excellent diversity in generated samples, which is why DDPMs quickly became the backbone of modern text-to-image systems.
Think of It Like This
The Sandcastle Sculptor
Imagine building a sandcastle, and then letting the wind slowly blow grains of sand away over several hours until it is nothing but a flat pile of sand. This is the Forward Process: it destroys structure predictably over time.
Now imagine you have a magical assistant who watched this happen, but they only watched in one-second intervals. If you give them a pile of sand, they can estimate where a few grains should go to reverse the last second of wind.
If you ask the assistant to reverse the wind one second at a time, thousands of times in a row, they will eventually rebuild the sandcastle from a flat pile of sand. This is the Reverse Process. The DDPM is the magical assistant learning exactly how to reverse a tiny fraction of the destruction at each step.
How It Actually Works
DDPMs are formulated around two opposing Markov chains: the forward process and the reverse process.
1. The Forward Process (Adding Noise)
The forward process, denoted as , takes a real, clean image and slowly adds Gaussian noise over steps (where is typically 1,000). At each step , a small amount of normally distributed noise is injected according to a variance schedule .
Because the sum of Gaussian distributions is also Gaussian, we don't have to simulate this step-by-step to see what the image looks like at step . We can use a mathematical shortcut to jump directly from to using the cumulative variance . By the time we reach step , the image is indistinguishable from pure, isotropic Gaussian noise. The forward process has no learned parameters; it is fixed and deterministic.
2. The Reverse Process (Removing Noise)
The true magic of DDPMs happens in the reverse process, . This process aims to undo the forward process. Because removing noise perfectly is computationally intractable, we use a neural network (typically a U-Net) with parameters to approximate this reversal.
Instead of predicting the clean image directly, the most mathematically robust way to train this network is to predict the noise that was added to the image at step . The network takes in the noisy image and the current timestep , and outputs a tensor representing the noise it believes is present.
3. Training the Network
During training, we sample a random image from our dataset, sample a random timestep , and generate the noisy version using our forward process shortcut. We then ask the neural network to predict the noise . We calculate the Mean Squared Error (MSE) between the actual noise we added and the noise the network predicted. We then backpropagate to update the network weights. This simple objective function naturally weights the loss across different noise levels, leading to stable, highly effective learning.
4. Generation (Inference)
To generate a new image, we start by sampling pure noise from a standard normal distribution to get . We then feed this noise into our trained network, asking it to predict the noise . We subtract a small fraction of this predicted noise to step backward from to . We repeat this process 1,000 times until we reach . Because the network removes only a tiny fraction of noise at each step, any small prediction errors are corrected in subsequent steps, leading to an incredibly coherent final image.
Show Me the Code
This code demonstrates the simplified training objective of a DDPM, where we sample noise, corrupt the image, and train the model to predict the noise.
import torchimport torch.nn as nn
def ddpm_training_step(model: nn.Module, x_0: torch.Tensor, t: torch.Tensor, alpha_bar: torch.Tensor) -> torch.Tensor: """ Computes the DDPM loss for a single training step. Args: model: The U-Net predicting the noise. x_0: Clean batch of images (B, C, H, W). t: Randomly sampled timesteps (B,). alpha_bar: Cumulative product of (1 - beta) schedule (T,). """ # 1. Sample pure Gaussian noise noise = torch.randn_like(x_0) # 2. Extract the cumulative variance for the current timesteps a_bar_t = alpha_bar[t].view(-1, 1, 1, 1) # 3. Forward process shortcut: compute x_t directly # x_t = sqrt(a_bar) * x_0 + sqrt(1 - a_bar) * noise x_t = torch.sqrt(a_bar_t) * x_0 + torch.sqrt(1 - a_bar_t) * noise # 4. Neural network predicts the noise that was added predicted_noise = model(x_t, t) # 5. Compute Mean Squared Error loss between true and predicted noise loss = nn.functional.mse_loss(predicted_noise, noise) return loss
# Example usage (mock tensors)# B=4, C=3, H=64, W=64, T=1000# -> loss (e.g., tensor(0.124))Watch Out For
Extremely Slow Inference
Because the original DDPM formulation requires passing the image through the neural network 1,000 times sequentially for a single generation, inference is remarkably slow. Unlike a GAN which generates an image in a single forward pass, a DDPM might take seconds or minutes. Modern systems solve this by swapping out the DDPM sampling strategy for faster algorithms (like DDIM) at inference time.
Predicting Noise vs. Image
It might seem intuitive to train the network to predict the clean image directly from the noisy image . While mathematically possible, the original DDPM paper found that predicting the noise resulted in vastly superior sample quality because it implicitly scales the learning objective to focus on fine details at lower noise levels.
The Quick Version
- DDPMs consist of a forward process (adding noise) and a reverse process (removing noise).
- The forward process requires no learning and destroys an image into pure Gaussian noise over steps.
- The reverse process trains a neural network (typically a U-Net) to predict the exact noise added at a specific timestep.
- By starting with pure noise and iteratively subtracting the network's predicted noise over hundreds of steps, DDPMs generate diverse, high-quality data.
- While training is stable, the step-by-step generative process makes them much slower to sample from than previous architectures like GANs.
What to Read Next
- Read Diffusion Samplers to see how researchers bypassed the 1,000-step DDPM bottleneck to generate images much faster.
- Read U-Net vs. Diffusion Transformers to understand the architectural backbone that performs the actual denoising.