Diffusion Tutorial
Diffusion Tutorial
HAL Authorization
To appear as a part of an upcoming textbook on deep learning.
Abstract
Diffusion models are a family of generative mod-
els which work based on a Markovian process. In
their forward process, they gradually add noise
to data until it becomes a complete noise. In the
backward process, the data are gradually gener-
ated out of noise. In this tutorial paper, the De-
noising Diffusion Probabilistic Model (DDPM)
is fully explained. Detailed simplification of the
Figure 1. The forward and backward processes of the diffusion
variational lower bound of its likelihood, param-
model. The credit of the used images is for Arash Vahdat.
eters of the distributions, and the loss function of
the diffusion model are discussed. Some mod-
ifications to the original DDPM, including non-
fixed covariance matrix, reducing the gradient
noise, improving the noise schedule, and non-
standard Gaussian noise distribution, and condi-
tional diffusion model are introduced. Finally,
continuous noise schedule by Stochastic Differ-
Figure 2. The diffusion model as an autoencoder.
ential Equation (SDE), where the noise schedule
is in a continuous domain, is explained.
are learned.
1. Introduction
Diffusion models take their inspiration from the principles
2. Denoising Diffusion Probabilistic Model
of non-equilibrium thermodynamics. They use a Markov (DDPM)
chain (Markovian process) to gradually introduce noise The first and original diffusion model is the Denoising Dif-
into data and then they learn to reverse this process to recre- fusion Probabilistic Model (DDPM), proposed in (Ho et al.,
ate the desired data from the noise. 2020). This section explains DDPM in details.
As shown in Fig. 1, a diffusion model has two processes The process of adding noise to data can be seen as a se-
with opposite directions. In the fixed forward diffusion pro- quence or chain {x0 , x1 , . . . , xT } where x0 ∈ Rd and
cess, it starts with the data and gradually adds noise to it xT ∈ Rd are the original data and data completely cor-
until it becomes a complete noise. Then, in the generative rupted with noise, respectively. Figure 3 depicts such a
reverse denoising process, it reverses that forward process chain. In this notation, xt+1 is xt plus some noise. As
and generates data out of the noise by denoising it gradu- the noise is random, there can be a conditional distribu-
ally. tion q(xt+1 |xt ) to model the probability of obtaining xt+1
As Fig. 2 depicts, the forward and reverse processes can from xt . The goal of diffusion models is to learn the con-
be modeled by the encoder and decoder of an autoencoder, ditional distribution q(xt |xt+1 ) modeling how to gener-
respectively. As the forward process merely needs to make ate data out of noise. This is because when q(xt |xt+1 ) is
data noisy, it does not involve any learning and thus is fixed. learned, t can be swept from T to 0 to generate a non-noisy
Therefore, only the decoder needs to be learned in diffusion data instance out of the complete noise.
models. This is one of the differences of diffusion models The distribution q(xt |xt+1 ) is intractable and it should be
from variational autoencoder (Kingma & Welling, 2014; approximated using a neural network. Let pθ (xt |xt+1 )
Ghojogh et al., 2022) in which both encoder and decoder denote the neural network with weights θ which approx-
2
Therefore, the joint distribution of the generative process els can be simplified as follows:
is:
L = Eq(x1:T |x0 ) log pθ (x0 |x1:T )
T
pθ (x0:T ) = p(xT )
Y
pθ (xt−1 |xt ), (10) − KL q(x1:T |x0 )∥pθ (x1:T )
t=1 (a)
= Eq(x1:T |x0 ) log pθ (x0 |x1:T )
q(x1:T |x0 )
Z
where x0:T represents all states from xT to x0 and xT is − q(x1:T |x0 ) log dx1:T
pθ (x1:T )
the state corresponding to the complete noise. Usually, the (b)
p(xT ) is defined to be the noise with standard Gaussian = Eq(x1:T |x0 ) log pθ (x0 |x1:T )
distribution: h q(x1:T |x0 ) i
− Eq(x1:T |x0 ) log
pθ (x1:T )
(c)
h q(x1:T |x0 ) i
p(xT ) = N (0, I). (11) = Eq(x1:T |x0 ) log pθ (x0 |x1:T ) − log
pθ (x1:T )
so their summation becomes a Gaussian distribution whose ties in this Bayes’ rule are:
variance is summation of the two variances:
p p
αt αt−1 − αt αt−1 αt−2 ϵ + 1 − αt αt−1 ϵ
(18)
∼ N 0, 1 − αt αt−1 αt−2 ). q(xt |xt−1 , x0 ) = q(xt |xt−1 )
(22),(24) √
= N (xt ; αt xt−1 , βt I),
As a result, Eq. (29) can be stated as: √
1 (xt − αt xt−1 )2
∝ exp − ,
2 βt
√ p
xt = αt αt−1 αt−2 xt−3 + 1 − αt αt−1 αt−2 ϵ. (31) √
q(xt−1 |x0 ) = N xt−1 ; ᾱt−1 x0 , (1 − ᾱt−1 )I ,
√
1 (xt−1 − ᾱt−1 x0 )2
By induction, continuing this recursion until x0 gives xt in ∝ exp − ,
2 1 − ᾱt−1
terms of the original data x0 and the noise ϵ:
(31) √
q(xt |x0 ) = N xt ; ᾱt x0 , (1 − ᾱt )I
√
1 (xt − ᾱt x0 )2
v v
u t u t
uY u Y ∝ exp − .
xt = t αi x0 + 1 −
t αi ϵ 2 1 − ᾱt
i=1 i=1
(23) √ √
= ᾱt x0 + 1 − ᾱt ϵ, (30)
Therefore:
which states xt in terms of the original data x0 and noise
ϵ. As x0 is not random and ϵ has standard Gaussian dis-
tribution, the distribution of xt in this equation has mean
√
ᾱt x0 and covariance (1 − ᾱt )I. Therefore: q(xt−1 |xt , x0 )
√ √
1 (xt − αt xt−1 )2 (xt−1 − ᾱt−1 x0 )2
√ (33)
∝ exp − +
q(xt |x0 ) = N xt ; ᾱt x0 , (1 − ᾱt )I . (31) 2 βt 1 − ᾱt−1
√ 2
(xt − ᾱt x0 )
By this conditional distribution, the noisy data xt at any −
1 − ᾱt
iteration t can be sampled based on the original data x0 . 2 √
(a) 1 xt − 2 αt xt xt−1 + αt x2t−1
= exp −
2.4.2. T HE G ROUND T RUTH D ENOISING 2 βt
D ISTRIBUTION √
x2t−1 − 2 ᾱt−1 x0 xt−1 + ᾱt−1 x20
+
A neural network is required to learn the parameters, i.e., 1 − ᾱt−1
the mean and covariance, of the backward process in Eq. √
(xt − ᾱt x0 )2
(7). Similar to Eq. (7), the ground truth denoising distribu- −
1 − ᾱt
tion can be a Gaussian distribution:
(b) 1 αt 1 2
= exp − + x
2 βt 1 − ᾱt−1 t−1
q(xt−1 |xt , x0 ) = N xt−1 ; µ
e (xt , x0 ), Σ(x
e t , x0 ) , (32) √ √
αt ᾱt−1
−2 xt + x0 xt−1 + C(xt , x0 )
βt 1 − ᾱt−1
√
where µ e (xt , x0 ) ∈ Rd and Σ(x
e t , x0 ) ∈ Rd×d are the √
αt ᾱt−1 2
βt xt + 1−ᾱt−1 x0
mean and covariance of the distribution, respectively. For xt−1 − αt 1
(c) 1 βt + 1−ᾱt−1
simplicity, similar to Eq. (8), the covariance is usually set ∝ exp − , (34)
to be isotropic and diagonal, i.e., Σ(x et2 I.
e t , x0 ) = σ 2 1/( αβtt + 1−ᾱ1t−1 )
By Bayes’ rule, we have:
2.4.3. M EAN OF THE G ROUND T RUTH D ENOISING where q(x) = N (x; µ e and p(x) = N (x; µ , Σθ ).
e , Σ) θ
D ISTRIBUTION There is a closed form expression for the KL divergence of
According to Eq. (34), the mean of the ground truth de- two Gaussian distributions:
noising distribution, in Eq. (32), is as follows: 1
KL(q∥p) = tr(Σ−1
θ Σ) + (µθ − µ
e e )⊤ Σ−1
θ (µθ − µ
e)
√
αt
√
ᾱt−1
√
αt
√
ᾱt−1
2
βt xt + 1−ᾱt−1 x0 βt xt + 1−ᾱt−1 x0 det(Σθ )
µ
e (xt , x0 ) = αt 1 = αt −αt ᾱt−1 +βt
− d + ln , (39)
βt + 1−ᾱt−1 det(Σ)
e
βt (1−ᾱt−1 )
√ √
αt ᾱt−1 where d is the dimensionality of x, and tr(.) and det(.)
(a) βt xt + 1−ᾱt−1 x0
= 1−ᾱt denote trace and determinant of matrix, respectively. For
βt (1−ᾱt−1 ) simplicity, assume the covariances of the two distributions
√α √
ᾱt−1 1 − ᾱ are diagonal and equal, i.e., Σθ = Σq = λI (see Eq. (8)).
t t−1
= xt + x0 βt
βt 1 − ᾱt−1 1 − ᾱt Then, Eq. (39) is simplified to:
√ √
αt (1 − ᾱt−1 ) ᾱt−1 βt 1
= xt + x0 , (35) KL(q∥p) = e )⊤ Σ−1
(µ − µ θ (µθ − µ
e ),
1 − ᾱt 1 − ᾱt 2 θ
where (a) is because αt + βt = 1 and αt ᾱt−1 = ᾱt by which is the squared Mahalanobis distance between the
Eqs. (22) and (23). According to Eq. (30), we have: means of distributions. This makes sense because the two
distributions p and q are normal distributions and their co-
√ √
xt = ᾱt x0 + 1 − ᾱt ϵ variances – related to the second moment – are equal. So,
1 √ for making them the same, their first moments, i.e., their
=⇒ x0 = √ (xt − 1 − ᾱt ϵ). (36) means, should become the same.
ᾱt
As a result, the loss function can be written as the squared
Substituting Eq. (36) in Eq. (35) gives: Mahalanobis distance between the means of distributions:
√ h 1 i
αt (1 − ᾱt−1 ) ℓt := Ex0 ,ϵ 2 µ(xt , x0 ) − µθ (xt , t)∥22
∥e
µ
e (xt , x0 ) = xt 2∥Σθ (xt , t)∥2
1 − ᾱt
√ (a)
1 1 1 − αt
ᾱt−1 βt 1 √ = Ex0 ,ϵ √ xt − √ ϵ
+ √ (xt − 1 − ᾱt ϵ) 2∥Σθ (xt , t)∥22 αt 1 − ᾱt
1 − ᾱt ᾱt
1 1 − αt 2
(a) 1 1 − αt (22) 1 βt
−√ xt − √ ϵθ
= √ xt − √ ϵ = √ xt − √ ϵ , αt 1 − ᾱt 2
αt 1 − ᾱt αt 1 − ᾱt
2
(37) (1 − αt ) 2
= Ex0 ,ϵ ∥ϵ − ϵθ (xt , t)∥2
2αt (1 − ᾱt )∥Σθ (xt , t)∥22
where (a) is because of simplification of the expression
(1 − αt )2
(30)
while noticing Eqs. (22) and (23). Eq. (37) is the mean = Ex0 ,ϵ ×
of the tractable backward distribution q(xt−1 |xt , x0 ) as in 2αt (1 − ᾱt )∥Σθ (xt , t)∥22
√ √
Eq. (32). 2
ϵ − ϵθ ( ᾱt x0 + 1 − ᾱt ϵ, t) 2 , (40)
Let µθ (xt , t) denote the mean of the distribution of the
backward process, i.e., pθ (xt−1 |xt ). This mean also has where (a) is because of Eqs. (37) and (38) while noticing
a similar format as the mean µ
e (xt , x0 ); therefore: Eq. (22).
1 βt Ho et al. (Ho et al., 2020) discovered empirically that the
µθ (xt , t) = √ xt − √ ϵθ (xt , t) , (38) diffusion model produces better-quality images when using
αt 1 − ᾱt
a simplified loss function, omitting the weighting term:
where ϵθ (xt , t) is a function approximator (i.e., neural net-
√ √
2
work) intended to predict ϵ from xt . In summary, the back- ℓ = Et∼[1,T ],x0 ,ϵ ϵ − ϵθ ( ᾱt x0 + 1 − ᾱt ϵ, t) 2 ,
ward process pθ (xt−1 |xt ) has a Gaussian distribution as in
(41)
the Eq. (7), whose mean and covariance are Eqs. (38) and
(8), respectively. where t ∼ [1, T ] means sampling t from the set
{1, 2, . . . , T } uniformly. Recall that ᾱt is calculated by
2.5. The Loss Function of the Diffusion Model Eq. (22) based on the selected noise schedule βt in Eq.
Recall the KL-divergence in the loss function of Eq. (21): (3). Note that the expectation can be approximated by aver-
age according to the Monte Carlo approximation (Ghojogh
KL q(xt−1 |xt , x0 )∥pθ (xt−1 |xt ) , et al., 2020).
8
be used to start with a complete noise and generate (or sam- where ℓt and ℓ are defined in Eqs. (40) and (41), respec-
ple) data out of it in a backward process. tively, and λ = 0.001 is the regularization hyperparameter.
The inference phase starts with the complete noise, i.e., Note that Σθ (xt ; t), defined by Eq. (44), is used in ℓt so its
xT ∼ N (0, I). Let σt I denote the covariance of xt . v is learned by backpropagation.
By use of the reparameterization technique (Kingma &
Welling, 2014) and according to Eq. (38) while noticing 3.2. Reducing the Gradient Noise
Eq. (22), we have: One might wonder why a regularized loss function, as in
Eq. (45), is required while ℓ is the simplification of sum-
mation of ℓt ’s; meaning that the information of the first
1 1 − αt
term is contained in the second term of Eq. (45). By use
xt−1 = √ xt − √ ϵθ (xt , t) + σt ϵ, (42)
αt 1 − ᾱt of gradient noise scale (McCandlish et al., 2018), Nichol
et al. (Nichol & Dhariwal, 2021) empirically observed that
the gradient noise of ℓt is much more than that of ℓ. They
where ϵ ∼ N (0, I) and αt and ᾱt are calculated by Eqs.
observed that this gradient noise is caused by the uniform
(22) and (23) based on the selected noise schedule βt in Eq.
sampling of t in Algorithm 1. By replacing the uniform
(3). Note that ϵθ (xt , t) is outputted by the trained network.
sampling with importance sampling (Ghojogh et al., 2020),
Iterating over t from T to 1 gives the generated data x0 .
which records a history of previous values, they reduced
The inference phase of DDPM is in Algorithm 2.
this gradient [Link] using importance sampling, they
T
could use merely t=0 ℓt as the loss function.
3. Modifications to the Original DDPM
3.1. Non-fixed Covariance Matrix 3.3. Improving the Noise Schedule
Recall Eq. (8) in which Σθ is assumed to be fixed and Recall the noise schedule stated in Eq. (3). While lin-
diagonal, i.e., Σθ (xt ; t) = σt2 I, in the original DDPM (Ho ear noise schedule used in the original DDPM (Ho et al.,
9
(t − 1). Performing this iteratively from t = T to t = 1 and the ∇x log pt (x) is the time-dependant gradient field,
gives the sample similar to the reference sample. called score briefly. As long as the score gets estimated,
It is noteworthy that n is a positive integer selected by the this reverse-time SDE can be solved and therefore the sam-
user. The smaller the n, the more similar the sample will ple can be generated out of noise in the backward process
be to the reference. This is because a small n makes the of diffusion model.
bandwidth of the low pass filter larger, replacing most of According to (Song et al., 2021b), the loss function of the
the frequencies of the sample with the reference data. original DDPM, for maximizing the ELBO, can be stated
in the form of a weighted sum of denoising score matching
5. Continuous Noise Schedule by Stochastic (Vincent, 2011):
Differential Equation T
X
The noise schedule in DDPM (Ho et al., 2020) is in the ℓ= (1 − αt )Epdata (x) Eq(xt |x0 ) sθ (xt , t)
discrete domain; meaning that there are T discrete steps t=1
from the original data to the complete noise and backwards.
However, it is possible to have noise schedule of diffusion − ∇xt log q(xt |x0 ) , (55)
models in a continuous domain; meaning that t ∈ [0, T ] can
be continuous rather than being discrete t ∈ {1, 2, . . . , T }. where sθ (xt , t) is the output of the neural network with
The continuous noise schedule converts the Markov chain weights θ. Likewise, the loss function in the diffusion
of diffusion model to a Stochastic Differential Equation model with SDE is (Song et al., 2021b):
(SDE) (Song et al., 2021b). "
Consider Eq. (25):
ℓ = Et λ(t) Ex(0) Ex(t)|x(0) sθ (xt , t)
√ √ (22) p p
xt = αt xt−1 + 1 − αt ϵ = 1 − βt xt−1 + βt ϵ,
#
− ∇x(t) log q x(t)|x(0) , (56)
in DDPM in which t ∈ {1, 2, . . . , T }. If T → ∞, the num-
ber of steps in the noise schedule becomes huge making
the flow of time almost continuous. By T → ∞, Eq. (25) where λ : [0, T ] → R>0 is a positive weighting func-
converges to the following SDE (Song et al., 2021b): tion, t is sampled from the uniform distribution in range
[0, T ], the x(0) is a sampled data instance from the train-
1
p
dx = − β(t) x dt + β(t) dw, (52) ing dataset, and x(t) ∼ q x(t)|x(0) . This loss function
2 is minimized by backpropagation until the output of net-
work, i.e., sθ (xt , t), becomes a good estimator of the score
where w ∈ Rd is the standard Wiener process (Brownian ∇x(t) log q x(t)|x(0) .
motion), x ∈ Rd is the data variable, and dx and dw are
After training, the neural network becomes a function ap-
the differentials of x and w. As a result, DDPM (Ho et al.,
proximator for the score. Therefore, the reverse-time SDE,
2020) is a special case of diffusion model with SDE (Song
Eq. (54), can be solved by numerical SDE solvers to
et al., 2021b).
generate data in the backward process of diffusion model.
In a more general case, the forward process in the diffusion Various numerical methods exist for solving SDEs, some
model with SDE is the following SDE (Song et al., 2021b): of which are Euler-Maruyama and stochastic Runge-Kutta
(Kloeden & Platen, 1992). Any SDE solver can be used to
dx = f (x, t) dt + g(t) dw, (53)
solve the reverse-time SDE of the diffusion model to gen-
where w ∈ Rd is the standard Wiener process (Brownian erate data.
motion), f (., t) : Rd → Rd is a function named the drift It is noteworthy that continuous noise schedule by SDE
coefficient of x(t), and g(.) : R → R is a function called has been also extended for discrete-state (categorical) data
the diffusion coefficient of x(t). The backward process of (Campbell et al., 2022).
the diffusion model with SDE is the following reverse-time
SDE (Song et al., 2021b): 6. Conclusion
This was a tutorial paper about diffusion models – a fam-
dx = f (x, t) − g(t)2 ∇x log pt (x) dt + g(t) dw̄, (54) ily of generative models in machine learning. The original
DDPM, its forward and backward processes, its variational
where w̄ is the standard Wiener process when time flows lower bound, and parameters of distributions were covered.
backwards from T to 0 and dt is the infinitesimal nega- Some modifications and improvements to DDPm were also
tive time. The operator ∇x is the derivative with respect to introduced. Then, conditional diffusion model for generat-
x, the pt (x) is the probability distribution of x at time t, ing data similar to a reference data instance was explained.
11
Finally, the diffusion model using SDE was covered for Advances in neural information processing systems, 27,
having a continuous noise schedule. 2014.
Ghojogh, Benyamin, Karray, Fakhri, and Crowley, Mark. McCandlish, Sam, Kaplan, Jared, Amodei, Dario, and
Hidden Markov model: Tutorial. Engineering Archive, Team, OpenAI Dota. An empirical model of large-batch
2019. training. arXiv preprint arXiv:1812.06162, 2018.
Ghojogh, Benyamin, Nekoei, Hadi, Ghojogh, Aydin, Kar- Nichol, Alexander Quinn and Dhariwal, Prafulla. Im-
ray, Fakhri, and Crowley, Mark. Sampling algorithms, proved denoising diffusion probabilistic models. In In-
from survey sampling to Monte Carlo methods: Tutorial ternational Conference on Machine Learning, pp. 8162–
and literature review. arXiv preprint arXiv:2011.00901, 8171. PMLR, 2021.
2020.
Song, Jiaming, Meng, Chenlin, and Ermon, Stefano. De-
Ghojogh, Benyamin, Ghodsi, Ali, Karray, Fakhri, and noising diffusion implicit models. In International Con-
Crowley, Mark. Factor analysis, probabilistic princi- ference on Learning Representations, 2021a.
pal component analysis, variational inference, and vari-
Song, Yang, Sohl-Dickstein, Jascha, Kingma, Diederik P,
ational autoencoder: Tutorial and survey. arXiv preprint
Kumar, Abhishek, Ermon, Stefano, and Poole, Ben.
arXiv:2101.00734, 2021.
Score-based generative modeling through stochastic dif-
Ghojogh, Benyamin, Crowley, Mark, Karray, Fakhri, and ferential equations. In International Conference on
Ghodsi, Ali. Variational autoencoders. In Elements of Learning Representations, 2021b.
Dimensionality Reduction and Manifold Learning, pp. Vincent, Pascal. A connection between score matching
563–576. Springer, 2022. and denoising autoencoders. Neural computation, 23(7):
Ghojogh, Benyamin, Crowley, Mark, Karray, Fakhri, and 1661–1674, 2011.
Ghodsi, Ali. Factor analysis and probabilistic principal
component analysis. In Elements of Dimensionality Re-
duction and Manifold Learning, pp. 355–374. Springer,
2023.