Recently, diffusion models have demonstrated remarkable performance on several generative tasks, ranging from unconditional or text-guided image generation, audio synthesis, natural language generation, human motion synthesis to 3D pointcloud generation, most of which were previously dominated by GAN-based models. Research also has shown that diffusion models are considerably better than GANs in terms of generation quality while also being able to control the results . In this blog, we will go through the mathematics and intuition behind this compelling generative model.
Figure 1. Overview of Diffusion Model.
Mechanism behind Diffusion Model
The underlying principle of diffusion model is based on a popular idea of non-equilibrium statistical thermodynamics: we use a Markov chain to gradually convert one distribution into another . Intuitively, it is called "diffusion" because in this model, data points in our original data space are gradually diffused from one position into another until reaching a state that is just a set of noise. Unlike the common VAE or GAN models where the data is typically compressed into a low dimensional latent space, diffusion models are learned with a fixed procedure and with high dimensional latent variable (same as the original data, see Figure 1). Now let's define the forward diffusion process.
Forward diffusion process
Figure 2. Example of forward diffusion
Given a sample from our real data distribution , we define the forward diffusion process as a Markov chain, which gradually adds random Gaussian noise (with diagonal covariance) to the data under a pre-defined schedule up to steps. Specifically, the distribution of the noised sample at step only depends on the previous step :
where is the noise schedule controlling the step size or how slow the noises are added in the process. Typically, they are between and should hold the followings:
An interesting property of the forward process, which makes it a very powerful generative model, is that when the noise step size is small enough and , the distribution at the final step becomes a standard Gaussian distribution . In other words, we can easily sample from this (pure) noise distribution and then follow a reverse process to reconstruct the real data sample, which we will discuss later.
Another notable property of this process is that we can directly sample at any time step without going through the whole chain. Specifically, let , from Eq. 1, we can derive the sample using reparameterization trick as follow:
where . Note that when we add two Gaussian with variances and , the merged distribution is also a Gaussian and should have variance . Therefore, we obtain the merged variance (the right sum of the above equation) as:
Thus can be written as:
where . We can also re-write the forward diffusion as a distribution depended on as:
This nice property can make the training much more efficient since we only need to compute the loss at some random timesteps instead of a whole process, which we will see later.
Reverse diffusion process
We have seen that the forward diffusion essentially pushes a sample off the data manifold and slowly transforms it into noise. The main goal of the reverse process is to learn a trajectory back to the manifold and thus reconstruct the sample in the original data distribution from the noise.
Feller et. al  demonstrated that for Gaussian diffusion process with small step size , the reverse of the diffusion process has the identical functional form as the forward process. This means that is a Gaussian distribution, and if is small, then will also be a Gaussian distribution.
Unfortunately, estimating reverse distribution is very difficult because in early steps of the reverse process ( is near ), our sample is just a random noise with high variance. In other words, there exist too many trajectories for us to arrive at our original data from that noise, which we may need to use the entire dataset to integrate. And ultimately, our final goal is to learn a model to approximate these reverse distributions in order to build the generative reverse denoising process, specifically:
where is the mean predicted by the model (a neural network with parameters ), and is the variance that is typically kept fixed for efficient learning.
Fortunately, by conditioning on , the posterior of the reverse process is tractable and becomes a Gaussian distribution . Our target now is to find the posterior mean and the variance . Following Bayes rule, we can write:
because the process is a Markov chain, where the future state () is conditionally independent with all previous states () given the present state (). We also leverage the fact that both of the distributions on the right hand side are Gaussian, which means we can apply the density function () as follow:
where is a function only depending on and but not , so it can be effectively ignored. Therefore we have:
Comparing with the Gaussian density function , we can see some similaries. Recall that and . Matching the variance term, we have the posterior variance:
We also obtain the mean:
Now we obtain the final formula for the reverse distribution:
So far we have provided the core concepts behind the two essential constituents of diffusion model: forward process and reverse process. In the next post, we will go into detail about the training loss as well as the sampling algorithm of diffusion model.
 Dhariwal P, Nichol A. Diffusion models beat gans on image synthesis. In NIPS 2021
 Sohl-Dickstein J, Weiss E, Maheswaranathan N, Ganguli S. Deep unsupervised learning using nonequilibrium thermodynamics. In ICML 2015.
 Feller W. On the theory of stochastic processes, with particular reference to applications. In Selected Papers I 2015 (pp. 769-798). Springer, Cham.
 Ho J, Jain A, Abbeel P. Denoising diffusion probabilistic models. In NIPS 2020