Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

DDPM: Forward and Reverse Processes

Introduction

In our previous lessons on Generative Adversarial Networks (GANs), we concluded that their training instability often stems from the limitations of the Jensen-Shannon Divergence, especially when the real and generated data distributions are disjoint. This can lead to vanishing gradients and mode collapse, making the adversarial game notoriously difficult to win.

Today, we shift our focus to an entirely different and remarkably successful generative paradigm: Denoising Diffusion Probabilistic Models (DDPMs). Instead of the adversarial dynamics of GANs, diffusion models are inspired by principles from non-equilibrium thermodynamics. They approach the problem of generation not as a single, complex transformation from latent space to data, but as a gradual, multi-step process.

This lesson will introduce the two fundamental components of DDPMs. We will first describe the forward process, a fixed procedure that systematically destroys information in an image by adding Gaussian noise. Then, we will explore the reverse process, which involves training a neural network to methodically undo this noising process, learning to generate data by denoising a pure noise signal. By the end, you will understand the core mechanism that allows these models to generate stunningly high-quality and diverse images.

The Core Idea: Diffusion and Denoising

At its heart, a diffusion model operates on a beautifully simple principle. Imagine you have a clear image, .

  1. Forward (Diffusion) Process: You begin a process of adding a small amount of Gaussian noise to the image. You repeat this over and over again, for a large number of timesteps, . After each step, the image becomes slightly noisier. By the final timestep , the original image is completely overwhelmed by the cumulative noise, and what remains is indistinguishable from a sample drawn from a standard Gaussian distribution. This process is fixed and requires no learning; it's a pre-defined recipe for data destruction.

  2. Reverse (Denoising) Process: The generative magic happens here. We now train a neural network to reverse this process. The network's task is, given a noisy image at some timestep , to predict a slightly less noisy image . If this model can learn to perform this single-step denoising operation accurately, we can start with pure noise () and iteratively apply the model times, gradually removing noise at each step, until a clean, coherent image emerges.

This two-stage mechanism is visualized in the following diagram, which shows an image of a sunflower being progressively noised in the forward process and an architecture for reversing this.

A schematic of a Denoising Diffusion Probabilistic Model. The top row shows the forward process, where an initial image `x_0` is gradually noised over `T` timesteps until it becomes pure noise `x_T`. The bottom row illustrates the reverse process, where a model learns to denoise the image step-by-step to recover an image from noise.

To begin, let's watch a short, high-level overview of this concept.

Diffusion Models | Paper Explanation | Math Explained

The video "Diffusion Models | Paper Explanation" by Outlier provides a concise and clear introduction to the main idea.

Please watch the section from the initial explanation. Focus on the conceptual distinction between the fixed forward process (adding noise) and the learned reverse process (removing noise).

The Forward Process: A Fixed Markov Chain

Let's formalize the diffusion process. We define it as a Markov chain, which means that the state at any given timestep , denoted as , depends only on the state at the immediately preceding timestep, .

At each step, we sample from a Gaussian distribution whose mean is a scaled version of and whose variance is fixed. The transition is defined as:

Let's break down this equation:

  • denotes a Gaussian distribution with mean and variance .
  • is a small, positive constant that defines the variance schedule. It controls how much noise is added at step . These are hyperparameters, typically set to increase from a small value (e.g., ) to a larger one (e.g., ) over steps (e.g., ).
  • is the mean of the distribution. We slightly shrink the previous image.
  • is the variance. We add Gaussian noise with variance .

We can re-write this process using the reparameterization trick, which makes it clear how a sample is constructed:

The "Shortcut" Property

A key property of this formulation is that we don't need to iteratively apply the noising step times to find . We can derive a closed-form expression to sample directly from the original image .

Let and (the cumulative product of all ). Through recursive substitution, it can be shown that:

Or, using the reparameterization trick again:

This equation is fundamental to the entire DDPM framework. It tells us that any noisy image can be expressed as a simple linear combination of the original image and a single Gaussian noise sample . This allows us to directly generate training pairs by simply picking a random timestep and a random noise vector .

As , the value of approaches zero. Consequently, the term vanishes, and is effectively just a sample from , achieving our goal of turning data into pure noise.

To dive deeper into the mathematics of the forward process, the following resources are excellent.

Denoising diffusion probabilistic models from first principles

This blog post by Lior Sinai provides a clear, code-oriented derivation of the DDPM framework. It's particularly useful for seeing how the math translates into implementation, which should appeal to your engineering background.

Please read the sections Forward process and Shortcut. Pay close attention to the derivation of the q_sample function, which directly implements the shortcut equation we just discussed.

How Diffusion Models Work (DDPM Explained)

For a complementary video explanation, "How Diffusion Models Work" by DeepBean offers a more theoretical perspective.

Watch the segment from the forward process. This video elaborates on why the Gaussian distribution is a good choice (maximum entropy) and the variance-preserving property of the chosen scaling factors, which are important design details.

The Reverse Process: Learning to Denoise

Now for the hard part: reversing the diffusion. Our goal is to learn the distribution , which allows us to take a noisy image and sample a slightly cleaner version .

Socratic Question: Knowing the forward transition , what mathematical tool would you first reach for to try and define the reverse transition ?

...

Your first instinct should be Bayes' theorem:

However, we immediately hit a wall. Evaluating requires integrating over the entire (unknown) data distribution, making it intractable.

The Trick: Condition on

The key insight of diffusion models is that the reverse posterior is tractable if we also condition on the starting image : . Applying Bayes' theorem here gives:

Because the forward process is Markovian, simplifies to . All three terms on the right are known Gaussian distributions we defined in the forward process. A remarkable property of Gaussians is that this combination results in another Gaussian distribution. After some non-trivial algebra (which you can find in the resources), we find that , where the mean is a function of and , and the variance is a function of the schedule.

Parameterizing the Reverse Step

This gives us a target. We know what the reverse step should look like if only we knew the original image . Since we don't know during generation (that's what we are trying to create!), we train a neural network to approximate this posterior.

Given that the true posterior is Gaussian, we parameterize our learned reverse transition as a Gaussian as well:

The neural network's job is to take a noisy image and the timestep as input and predict the parameters (mean and variance ) of the distribution for the previous, cleaner image .

The original DDPM paper simplifies this further by setting the variance to a fixed constant () and only training the network to predict the mean .

The Final Leap: Predicting Noise

The final, crucial design choice is how to parameterize the mean . We could train the network to predict it directly. However, we can use our shortcut equation to re-express the mean in terms of and . This re-parameterization reveals that predicting the mean is mathematically equivalent to predicting the noise that was added to create .

Empirically, training a network to predict the noise component was found to be much more stable and effective. This transforms the complex task of distribution modeling into a straightforward regression problem: given a noisy image , predict the noise that it contains.

This diagram shows the forward process mapping from the image space to the noise space. The reverse process learns a function to map back from the noise space to the image space. The term `Score function`, \(\nabla_{x_t} \log p_t(x_t)\), is deeply related to the noise prediction task and is a key concept in score-based generative models, a close cousin of DDPMs.

The following video segments provide an excellent walkthrough of these ideas.

How Diffusion Models Work (DDPM Explained)

Let's continue with the "How Diffusion Models Work" video.

First, watch the part explaining the reverse process and its challenges. It details why direct computation is intractable and motivates the Gaussian approximation. Then, watch the segment on the DDPM practical design choices, which explains the simplification of fixing the variance and, crucially, parameterizing the model to predict noise (\epsilon-prediction).

Conclusion

In this lesson, we have dissected the core mechanics of Denoising Diffusion Probabilistic Models, establishing the conceptual and mathematical foundation for this powerful class of generative models.

Key Takeaways:

  • Two-Process System: DDPMs consist of a fixed forward (diffusion) process that gradually adds Gaussian noise to data, and a learned reverse (denoising) process that iteratively removes it.
  • Forward Process is a Fixed Markov Chain: The forward process is defined by a pre-set variance schedule . A key property is the "shortcut," which allows sampling a noisy image directly from the original in one step: .
  • Reverse Process is Learned Denoising: The reverse process aims to learn . This is framed as approximating the true but intractable posterior .
  • Noise Prediction is the Objective: The model is parameterized as a Gaussian, and training is simplified to a regression task where a neural network learns to predict the noise component of a noisy image .

Preview of the Next Lesson:

Now that we understand the what and why of the forward and reverse processes, the next logical step is to understand how we train the model to perform this noise prediction task. In the next lesson, we will derive the simplified DDPM training objective function. We will see how the complex objective of maximizing the data log-likelihood (via the Evidence Lower Bound) elegantly reduces to a simple Mean Squared Error loss between the true noise and the predicted noise.

Can't find a good explanation? Sign up and we'll make it for you

Sign up