Lably
Skip to content

Diffusion model

Technique for the generative modeling of a continuous probability distribution

  • 27 min read
  • 6 chapters

Related on Lably

Through Machine learning, Computer vision and Natural language processing

There are various equivalent formalisms, including Markov chains, denoising diffusion probabilistic models, noise conditioned score networks, and stochastic differential equations. They are typically trained using variational inference. The model responsible for denoising is typically called its "backbone". The backbone may be of any kind, but they are typically U-nets or transformers.

As of 2024, diffusion models are mainly used for computer vision tasks, including image denoising, inpainting, super-resolution, image generation, and video generation. These typically involve training a neural network to sequentially denoise images blurred with Gaussian noise. The model is trained to reverse the process of adding noise to an image. After training to convergence, it can be used for image generation by starting with an image composed of random noise, and applying the network iteratively to denoise the image.

Diffusion-based image generators have seen widespread commercial interest, such as Stable Diffusion and DALL-E. These models typically combine diffusion models with other models, such as text-encoders and cross-attention modules to allow text-conditioned generation.

Other than computer vision, diffusion models have also found applications in natural language processing such as text generation and summarization, sound generation, and reinforcement learning.

Denoising diffusion model

Non-equilibrium thermodynamics

Diffusion models were introduced in 2015 as a method to train a model that can sample from a highly complex probability distribution. They used techniques from non-equilibrium thermodynamics, especially diffusion.

Consider, for example, how one might model the distribution of all naturally occurring photos. Each image is a point in the space of all images, and the distribution of naturally occurring photos is a "cloud" in space, which, by repeatedly adding noise to the images, diffuses out to the rest of the image space, until the cloud becomes all but indistinguishable from a Gaussian distribution N(0,I). A model that can approximately undo the diffusion can then be used to sample from the original distribution. This is studied in "non-equilibrium" thermodynamics, as the starting distribution is not in equilibrium, unlike the final distribution.

The equilibrium distribution is the Gaussian distribution N(0,I), with pdf ρ(x)∝e−12‖x‖2. This is just the Maxwell–Boltzmann distribution of particles in a potential well V(x)=12‖x‖2 at temperature 1. The initial distribution, being very much out of equilibrium, would diffuse towards the equilibrium distribution, making biased random steps that are a sum of pure randomness (like a Brownian walker) and gradient descent down the potential well. The randomness is necessary: if the particles were to undergo only gradient descent, then they will all fall to the origin, collapsing the distribution.

Denoising Diffusion Probabilistic Model (DDPM)

The 2020 paper proposed the Denoising Diffusion Probabilistic Model (DDPM), which improves upon the previous method by variational inference.

Forward diffusion

To present the model, some notation is required.

  • β1,...,βT∈(0,1) are fixed constants.
  • αt:=1−βt
  • α¯t:=α1⋯αt
  • σt:=1−α¯t
  • σ~t:=σt−1σtβt
  • μ~t(xt,x0):=αt(1−α¯t−1)xt+α¯t−1(1−αt)x0σt2
  • N(μ,Σ) is the Gaussian distribution with mean μ and variance Σ, and N(x|μ,Σ) is the probability density at x.
  • A vertical bar denotes conditioning.

A forward diffusion process starts at some starting point x0∼q, where q is the probability distribution to be learned, then repeatedly adds noise to it byxt=1−βtxt−1+βtztwhere z1,...,zT are IID (Independent and identically distributed random variables) samples from N(0,I). The coefficients 1−βt and βt ensure that Var(Xt)=I assuming that Var(X0)=I. The values of βt are chosen such that for any starting distribution of x0, if it has finite second moment, then limt→∞xt|x0 converges to N(0,I).

The entire diffusion process then satisfiesq(x0:T)=q(x0)q(x1|x0)⋯q(xT|xT−1)=q(x0)N(x1|α1x0,β1I)⋯N(xT|αTxT−1,βTI)orln⁡q(x0:T)=ln⁡q(x0)−∑t=1T12βt‖xt−1−βtxt−1‖2+Cwhere C is a normalization constant and often omitted. In particular, we note that x1:T|x0 is a Gaussian process, which affords us considerable freedom in reparameterization. For example, by standard manipulation with Gaussian process, xt|x0∼N(α¯tx0,σt2I)xt−1|xt,x0∼N(μ~t(xt,x0),σ~t2I)In particular, notice that for large t, the variable xt|x0∼N(α¯tx0,σt2I) converges to N(0,I). That is, after a long enough diffusion process, we end up with some xT that is very close to N(0,I), with all traces of the original x0∼q gone.

For example, sincext|x0∼N(α¯tx0,σt2I)we can sample xt|x0 directly "in one step", instead of going through all the intermediate steps x1,x2,...,xt−1.

Derivation by reparameterization

We know xt−1|x0 is a Gaussian, and xt|xt−1 is another Gaussian. We also know that these are independent. Thus we can perform a reparameterization: xt−1=α¯t−1x0+1−α¯t−1z xt=αtxt−1+1−αtz′ where z,z′ are IID Gaussians.

There are 5 variables x0,xt−1,xt,z,z′ and two linear equations. The two sources of randomness are z,z′, which can be reparameterized by rotation, since the IID Gaussian distribution is rotationally symmetric.

By plugging in the equations, we can solve for the first reparameterization: xt=α¯tx0+αt−α¯tz+1−αtz′⏟=σtz″ where z″ is a Gaussian with mean zero and variance one.

To find the second one, we complete the rotational matrix: [z″z‴]=[αt−α¯tσtβtσt??][zz′]

Since rotational matrices are all of the form [cos⁡θsin⁡θ−sin⁡θcos⁡θ], we know the matrix must be [z″z‴]=[αt−α¯tσtβtσt−βtσtαt−α¯tσt][zz′] and since the inverse of rotational matrix is its transpose,
[zz′]=[αt−α¯tσt−βtσtβtσtαt−α¯tσt][z″z‴]

Plugging back, and simplifying, we have xt=α¯tx0+σtz″ xt−1=μ~t(xt,x0)−σ~tz‴

Backward diffusion

The key idea of DDPM is to use a neural network parametrized by θ. The network takes in two arguments xt,t, and outputs a vector μθ(xt,t) and a matrix Σθ(xt,t), such that each step in the forward diffusion process can be approximately undone by xt−1∼N(μθ(xt,t),Σθ(xt,t)). This then gives us a backward diffusion process pθ defined bypθ(xT)=N(xT|0,I)pθ(xt−1|xt)=N(xt−1|μθ(xt,t),Σθ(xt,t))The goal now is to learn the parameters θ such that pθ(x0) is as close to q(x0) as possible. To do that, we use maximum likelihood estimation with variational inference.

Variational inference

The ELBO inequality states that ln⁡pθ(x0)≥Ex1:T∼q(⋅|x0)[ln⁡pθ(x0:T)−ln⁡q(x1:T|x0)], and taking one more expectation, we getEx0∼q[ln⁡pθ(x0)]≥Ex0:T∼q[ln⁡pθ(x0:T)−ln⁡q(x1:T|x0)]We see that maximizing the quantity on the right would give us a lower bound on the likelihood of observed data. This allows us to perform variational inference.

Define the loss functionL(θ):=−Ex0:T∼q[ln⁡pθ(x0:T)−ln⁡q(x1:T|x0)]and now the goal is to minimize the loss by stochastic gradient descent. The expression may be simplified toL(θ)=∑t=1TExt−1,xt∼q[−ln⁡pθ(xt−1|xt)]+Ex0∼q[DKL(q(xT|x0)‖pθ(xT))]+Cwhere C does not depend on the parameter, and thus can be ignored. Since pθ(xT)=N(xT|0,I) also does not depend on the parameter, the term Ex0∼q[DKL(q(xT|x0)‖pθ(xT))] can also be ignored. This leaves just L(θ)=∑t=1TLt with Lt=Ext−1,xt∼q[−ln⁡pθ(xt−1|xt)] to be minimized.

Noise prediction network

Since xt−1|xt,x0∼N(μ~t(xt,x0),σ~t2I), this suggests that we should use μθ(xt,t)=μ~t(xt,x0); however, the network does not have access to x0, and so it has to estimate it instead. Now, since xt|x0∼N(α¯tx0,σt2I), we may write xt=α¯tx0+σtz, where z is some unknown Gaussian noise. Now we see that estimating x0 is equivalent to estimating z.

Therefore, let the network output a noise vector ϵθ(xt,t), and let it predictμθ(xt,t)=μ~t(xt,xt−σtϵθ(xt,t)α¯t)=xt−ϵθ(xt,t)βt/σtαtIt remains to design Σθ(xt,t). The DDPM paper suggested not learning it (since it resulted in "unstable training and poorer sample quality"), but fixing it at some value Σθ(xt,t)=ζt2I, where either ζt2=βt or σ~t2 yielded similar performance.

With this, the loss simplifies to Lt=βt22αtσt2ζt2Ex0∼q;z∼N(0,I)[‖ϵθ(xt,t)−z‖2]+Cwhich may be minimized by stochastic gradient descent. The paper noted empirically that an even simpler loss functionLsimple,t=Ex0∼q;z∼N(0,I)[‖ϵθ(xt,t)−z‖2]resulted in better models.

Backward diffusion process

After a noise prediction network is trained, it can be used for generating data points in the original distribution in a loop as follows:

  1. Compute the noise estimate ϵ←ϵθ(xt,t)
  2. Compute the original data estimate x~0←(xt−σtϵ)/α¯t
  3. Sample the previous data xt−1∼N(μ~t(xt,x~0),σ~t2I)
  4. Change time t←t−1

Sign in to keep reading

5 more chapters are free with a Lably account.

More in Computing and communication