Understanding Diffusion Models: A Unified Perspective

Calvin Luo
Google Research, Brain Team
calvinluo@google.com
August 26, 2022

Introduction: Generative Models

Given observed samples from a distribution of interest, the goal of a generative model is to learn to model its true data distribution . Once learned, we can generate new samples from our approximate model at will. Furthermore, under some formulations, we are able to use the learned model to evaluate the likelihood of observed or sampled data as well.

There are several well-known directions in current literature, that we will only introduce briefly at a high level. Generative Adversarial Networks (GANs) model the sampling procedure of a complex distribution, which is learned in an adversarial manner. Another class of generative models, termed "likelihood-based", seeks to learn a model that assigns a high likelihood to the observed data samples. This includes autoregressive models, normalizing flows, and Variational Autoencoders (VAEs). Another similar approach is energy-based modeling, in which a distribution is learned as an arbitrarily flexible energy function that is then normalized. Score-based generative models are highly related; instead of learning to model the energy function itself, they learn the score of the energy-based model as a neural network. In this work we explore and review diffusion models, which as we will demonstrate, have both likelihood-based and score-based interpretations. We showcase the math behind such models in excruciating detail, with the aim that anyone can follow along and understand what diffusion models are and how they work.

Background: ELBO, VAE, and Hierarchical VAE

For many modalities, we can think of the data we observe as represented or generated by an associated unseen latent variable, which we can denote by random variable . The best intuition for expressing this idea is through Plato’s Allegory of the Cave. In the allegory, a group of people are chained inside a cave their entire life and can only see the two-dimensional shadows projected onto a wall in front of them, which are generated by unseen three-dimensional objects passed before a fire. To such people, everything they observe is actually determined by higher-dimensional abstract concepts that they can never behold.

Analogously, the objects that we encounter in the actual world may also be generated as a function of some higher-level representations; for example, such representations may encapsulate abstract properties such as color, size, shape, and more. Then, what we observe can be interpreted as a three-dimensional projection or instantiation of such abstract concepts, just as what the cave people observe is actually a two-dimensional projection of three-dimensional objects. Whereas the cave people can never see (or even fully comprehend) the hidden objects, they can still reason and draw inferences about them; in a similar way, we can approximate latent representations that describe the data we observe.

Whereas Plato’s Allegory illustrates the idea behind latent variables as potentially unobservable representations that determine observations, a caveat of this analogy is that in generative modeling, we generally seek to learn lower-dimensional latent representations rather than higher-dimensional ones. This is because trying to learn a representation of higher dimension than the observation is a fruitless endeavor without strong priors. On the other hand, learning lower-dimensional latents can also be seen as a form of compression, and can potentially uncover semantically meaningful structure describing observations.

Evidence Lower Bound

Mathematically, we can imagine the latent variables and the data we observe as modeled by a joint distribution . Recall one approach of generative modeling, termed "likelihood-based", is to learn a model to maximize the likelihood of all observed . There are two ways we can manipulate this joint distribution to recover the likelihood of purely our observed data ; we can explicitly marginalize out the latent variable :

(1)

or, we could also appeal to the chain rule of probability:

(2)

Directly computing and maximizing the likelihood is difficult because it either involves integrating out all latent variables in Equation 1, which is intractable for complex models, or it involves having access to a ground truth latent encoder in Equation 2. However, using these two equations, we can derive a term called the Evidence Lower Bound (ELBO), which as its name suggests, is a lower bound of the evidence. The evidence is quantified in this case as the log likelihood of the observed data. Then, maximizing the ELBO becomes a proxy objective with which to optimize a latent variable model; in the best case, when the ELBO is powerfully parameterized and perfectly optimized, it becomes exactly equivalent to the evidence. Formally, the equation of the ELBO is:

(3)

To make the relationship with the evidence explicit, we can mathematically write:

(4)

Here, is a flexible approximate variational distribution with parameters that we seek to optimize. Intuitively, it can be thought of as a parameterizable model that is learned to estimate the true distribution over latent variables for given observations ; in other words, it seeks to approximate true posterior . As we will see when exploring the Variational Autoencoder, as we increase the lower bound by tuning the parameters to maximize the ELBO, we gain access to components that can be used to model the true data distribution and sample from it, thus learning a generative model. For now, let us try to dive deeper into why the ELBO is an objective we would like to maximize.

Let us begin by deriving the ELBO, using Equation 1:

(Apply Equation 1) (5)
(Multiply by ) (6)
(Definition of Expectation) (7)
(Apply Jensen’s Inequality) (8)

In this derivation, we directly arrive at our lower bound by applying Jensen’s Inequality. However, this does not supply us much useful information about what is actually going on underneath the hood; crucially, this proof gives no intuition on exactly why the ELBO is actually a lower bound of the evidence, as Jensen’s Inequality handwaves it away. Furthermore, simply knowing that the ELBO is truly a lower bound of the data does not really tell us why we want to maximize it as an objective. To better understand the relationship between the evidence and the ELBO, let us perform another derivation, this time using Equation 2:

(Multiply by ) (9)
(Bring evidence into integral) (10)
(Definition of Expectation) (11)
(Apply Equation 2) (12)
(Multiply by ) (13)
(Split the Expectation) (14)
(Definition of KL Divergence) (15)
(KL Divergence always ) (16)

From this derivation, we clearly observe from Equation 15 that the evidence is equal to the ELBO plus the KL Divergence between the approximate posterior and the true posterior . In fact, it was this KL Divergence term that was magically removed by Jensen’s Inequality in Equation 8 of the first derivation. Understanding this term is the key to understanding not only the relationship between the ELBO and the evidence, but also the reason why optimizing the ELBO is an appropriate objective at all.

Firstly, we now know why the ELBO is indeed a lower bound: the difference between the evidence and the ELBO is a strictly non-negative KL term, thus the value of the ELBO can never exceed the evidence.

Secondly, we explore why we seek to maximize the ELBO. Having introduced latent variables that we would like to model, our goal is to learn this underlying latent structure that describes our observed data. In other words, we want to optimize the parameters of our variational posterior to exactly match the true posterior distribution , which is achieved by minimizing their KL Divergence (ideally to zero). Unfortunately, it is intractable to minimize this KL Divergence term directly, as we do not have access to the ground truth distribution. However, notice that on the left hand side of Equation 15, the likelihood of our data (and therefore our evidence term ) is always a constant with respect to , as it is computed by marginalizing out all latents from the joint distribution and does not depend on whatsoever. Since the ELBO and KL Divergence terms sum up to a constant, any maximization of the ELBO term with respect to necessarily invokes an equal minimization of the KL Divergence term. Thus, the ELBO can be maximized as a proxy for learning how to perfectly model the true latent posterior distribution; the more we optimize the ELBO, the closer our approximate posterior gets to the true posterior. Additionally, once trained, the ELBO can be used to estimate the likelihood of observed or generated data as well, since it is learned to approximate the model evidence .

Variational Autoencoders

A Variational Autoencoder graphically represented. Here, encoder
Figure 1: A Variational Autoencoder graphically represented. Here, encoder defines a distribution over latent variables for observations , and decodes latent variables into observations.

In the default formulation of the Variational Autoencoder (VAE) [7], we directly maximize the ELBO. This approach is variational, because we optimize for the best amongst a family of potential posterior distributions parameterized by . It is called an autoencoder because it is reminiscent of a traditional autoencoder model, where input data is trained to predict itself after undergoing an intermediate bottlenecking representation step. To make this connection explicit, let us dissect the ELBO term further:

(17)
(18)
(19)

In this case, we learn an intermediate bottlenecking distribution that can be treated as an encoder; it transforms inputs into a distribution over possible latents. Simultaneously, we learn a deterministic function to convert a given latent vector into an observation , which can be interpreted as a decoder.

The two terms in Equation 19 each have intuitive descriptions: the first term measures the reconstruction likelihood of the decoder from our variational distribution; this ensures that the learned distribution is modeling effective latents that the original data can be regenerated from. The second term measures how similar the learned variational distribution is to a prior belief held over latent variables. Minimizing this term encourages the encoder to actually learn a distribution rather than collapse into a Dirac delta function. Maximizing the ELBO is thus equivalent to maximizing its first term and minimizing its second term.

A defining feature of the VAE is how the ELBO is optimized jointly over parameters and . The encoder of the VAE is commonly chosen to model a multivariate Gaussian with diagonal covariance, and the prior is often selected to be a standard multivariate Gaussian:

(20)
(21)

Then, the KL divergence term of the ELBO can be computed analytically, and the reconstruction term can be approximated using a Monte Carlo estimate. Our objective can then be rewritten as:

(22)

where latents are sampled from , for every observation in the dataset. However, a problem arises in this default setup: each that our loss is computed on is generated by a stochastic sampling procedure, which is generally non-differentiable. Fortunately, this can be addressed via the reparameterization trick when is designed to model certain distributions, including the multivariate Gaussian.

The reparameterization trick rewrites a random variable as a deterministic function of a noise variable; this allows for the optimization of the non-stochastic terms through gradient descent. For example, samples from a normal distribution with arbitrary mean and variance can be rewritten as:

In other words, arbitrary Gaussian distributions can be interpreted as standard Gaussians (of which is a sample) that have their mean shifted from zero to the target mean by addition, and their variance stretched by the target variance . Therefore, by the reparameterization trick, sampling from an arbitrary Gaussian distribution can be performed by sampling from a standard Gaussian, scaling the result by the target standard deviation, and shifting it by the target mean.

In a VAE, each is thus computed as a deterministic function of input and auxiliary noise variable :

where represents an element-wise product. Under this reparameterized version of , gradients can then be computed with respect to as desired, to optimize and . The VAE therefore utilizes the reparameterization trick and Monte Carlo estimates to optimize the ELBO jointly over and .

After training a VAE, generating new data can be performed by sampling directly from the latent space and then running it through the decoder. Variational Autoencoders are particularly interesting when the dimensionality of is less than that of input , as we might then be learning compact, useful representations. Furthermore, when a semantically meaningful latent space is learned, latent vectors can be edited before being passed to the decoder to more precisely control the data generated.

Hierarchical Variational Autoencoders

A Hierarchical Variational Autoencoder (HVAE) [9, 15] is a generalization of a VAE that extends to multiple hierarchies over latent variables. Under this formulation, latent variables themselves are interpreted as generated from other higher-level, more abstract latents. Intuitively, just as we treat our three-dimensional observed objects as generated from a higher-level abstract latent, the people in Plato’s cave treat three-dimensional objects as latents that generate their two-dimensional observations. Therefore, from the perspective of Plato’s cave dwellers, their observations can be treated as modeled by a latent hierarchy of depth two (or more).

Whereas in the general HVAE with hierarchical levels, each latent is allowed to condition on all previous latents, in this work we focus on a special case which we call a Markovian HVAE (MHVAE). In a MHVAE, the generative process is a Markov chain; that is, each transition down the hierarchy is Markovian, where decoding each latent only conditions on previous latent . Intuitively, and visually, this can be seen as simply stacking VAEs on top of each other, as depicted in Figure 2; another appropriate term describing this model is a Recursive VAE. Mathematically, we represent the joint distribution and the posterior of a Markovian HVAE as:

(23)
(24)

Then, we can easily extend the ELBO to be:

(Apply Equation 1) (25)
(Multiply by 1 = ) (26)
(Definition of Expectation) (27)
(Apply Jensen’s Inequality) (28)
A Markovian Hierarchical Variational Autoencoder with
Figure 2: A Markovian Hierarchical Variational Autoencoder with hierarchical latents. The generative process is modeled as a Markov chain, where each latent is generated only from the previous latent .

We can then plug our joint distribution (Equation 23) and posterior (Equation 24) into Equation 28 to produce an alternate form:

(29)

As we will show below, when we investigate Variational Diffusion Models, this objective can be further decomposed into interpretable components.

A visual representation of a Variational Diffusion Model;
Figure 3: A visual representation of a Variational Diffusion Model; represents true data observations such as natural images, represents pure Gaussian noise, and is an intermediate noisy version of . Each is modeled as a Gaussian distribution that uses the output of the previous state as its mean.

Variational Diffusion Models

The easiest way to think of a Variational Diffusion Model (VDM) [14, 3, 8] is simply as a Markovian Hierarchical Variational Autoencoder with three key restrictions:

  • The latent dimension is exactly equal to the data dimension

  • The structure of the latent encoder at each timestep is not learned; it is pre-defined as a linear Gaussian model. In other words, it is a Gaussian distribution centered around the output of the previous timestep

  • The Gaussian parameters of the latent encoders vary over time in such a way that the distribution of the latent at final timestep is a standard Gaussian

Furthermore, we explicitly maintain the Markov property between hierarchical transitions from a standard Markovian Hierarchical Variational Autoencoder.

Let us expand on the implications of these assumptions. From the first restriction, with some abuse of notation, we can now represent both true data samples and latent variables as , where represents true data samples and represents a corresponding latent with hierarchy indexed by . The VDM posterior is the same as the MHVAE posterior (Equation 24), but can now be rewritten as:

(30)

From the second assumption, we know that the distribution of each latent variable in the encoder is a Gaussian centered around its previous hierarchical latent. Unlike a Markovian HVAE, the structure of the encoder at each timestep is not learned; it is fixed as a linear Gaussian model, where the mean and standard deviation can be set beforehand as hyperparameters [3], or learned as parameters [8]. We parameterize the Gaussian encoder with mean , and variance , where the form of the coefficients are chosen such that the variance of the latent variables stay at a similar scale; in other words, the encoding process is variance-preserving. Note that alternate Gaussian parameterizations are allowed, and lead to similar derivations. The main takeaway is that is a (potentially learnable) coefficient that can vary with the hierarchical depth , for flexibility. Mathematically, encoder transitions are denoted as:

(31)

From the third assumption, we know that evolves over time according to a fixed or learnable schedule structured such that the distribution of the final latent is a standard Gaussian. We can then update the joint distribution of a Markovian HVAE (Equation 23) to write the joint distribution for a VDM as:

(32)
where,
(33)

Collectively, what this set of assumptions describes is a steady noisification of an image input over time; we progressively corrupt an image by adding Gaussian noise until eventually it becomes completely identical to pure Gaussian noise. Visually, this process is depicted in Figure 3.

Note that our encoder distributions are no longer parameterized by , as they are completely modeled as Gaussians with defined mean and variance parameters at each timestep. Therefore, in a VDM, we are only interested in learning conditionals , so that we can simulate new data. After optimizing the VDM, the sampling procedure is as simple as sampling Gaussian noise from and iteratively running the denoising transitions for steps to generate a novel .

Like any HVAE, the VDM can be optimized by maximizing the ELBO, which can be derived as:

(34)
(35)
(36)
(37)
(38)
(39)
(40)
(41)
(42)
(43)
(44)
(45)

The derived form of the ELBO can be interpreted in terms of its individual components:

Under our first derivation, a VDM can be optimized by ensuring that for every intermediate
Figure 4: Under our first derivation, a VDM can be optimized by ensuring that for every intermediate , the posterior from the latent above it matches the Gaussian corruption of the latent before it . In this figure, for each intermediate , we minimize the difference between the distributions represented by the pink and green arrows.
  1. can be interpreted as a reconstruction term, predicting the log probability of the original data sample given the first-step latent. This term also appears in a vanilla VAE, and can be trained similarly.

  2. is a prior matching term; it is minimized when the final latent distribution matches the Gaussian prior. This term requires no optimization, as it has no trainable parameters; furthermore, as we have assumed a large enough such that the final distribution is Gaussian, this term effectively becomes zero.

  3. is a consistency term; it endeavors to make the distribution at consistent, from both forward and backward processes. That is, a denoising step from a noisier image should match the corresponding noising step from a cleaner image, for every intermediate timestep; this is reflected mathematically by the KL Divergence. This term is minimized when we train to match the Gaussian distribution , which is defined in Equation 31.

Visually, this interpretation of the ELBO is depicted in Figure 4. The cost of optimizing a VDM is primarily dominated by the third term, since we must optimize over all timesteps .

Under this derivation, all terms of the ELBO are computed as expectations, and can therefore be approximated using Monte Carlo estimates. However, actually optimizing the ELBO using the terms we just derived might be suboptimal; because the consistency term is computed as an expectation over two random variables for every timestep, the variance of its Monte Carlo estimate could potentially be higher than a term that is estimated using only one random variable per timestep. As it is computed by summing up consistency terms, the final estimated value of the ELBO may have high variance for large values.

Let us instead try to derive a form for our ELBO where each term is computed as an expectation over only one random variable at a time. The key insight is that we can rewrite encoder transitions as , where the extra conditioning term is superfluous due to the Markov property. Then, according to Bayes rule, we can rewrite each transition as:

(46)

Armed with this new equation, we can retry the derivation resuming from the ELBO in Equation 37:

(47)
(48)
(49)
(50)
(51)
(52)
(53)
(54)
(55)
(56)
(57)
(58)

We have therefore successfully derived an interpretation for the ELBO that can be estimated with lower variance, as each term is computed as an expectation of at most one random variable at a time. This formulation also has an elegant interpretation, which is revealed when inspecting each individual term:

  1. can be interpreted as a reconstruction term; like its analogue in the ELBO of a vanilla VAE, this term can be approximated and optimized using a Monte Carlo estimate.

  2. represents how close the distribution of the final noisified input is to the standard Gaussian prior. It has no trainable parameters, and is also equal to zero under our assumptions.

  3. is a denoising matching term. We learn desired denoising transition step as an approximation to tractable, ground-truth denoising transition step . The transition step can act as a ground-truth signal, since it defines how to denoise a noisy image with access to what the final, completely denoised image should be. This term is therefore minimized when the two denoising steps match as closely as possible, as measured by their KL Divergence.

As a side note, one observes that in the process of both ELBO derivations (Equation 45 and Equation 58), only the Markov assumption is used; as a result these formulae will hold true for any arbitrary Markovian HVAE. Furthermore, when we set , both of the ELBO interpretations for a VDM exactly recreate the ELBO equation of a vanilla VAE, as written in Equation 19.

In this derivation of the ELBO, the bulk of the optimization cost once again lies in the summation term, which dominates the reconstruction term. Whereas each KL Divergence term is difficult to minimize for arbitrary posteriors in arbitrarily complex Markovian HVAEs due to the added complexity of simultaneously learning the encoder, in a VDM we can leverage the Gaussian transition assumption to make optimization tractable. By Bayes rule, we have:

Depicted is an alternate, lower-variance method to optimize a VDM; we compute the form of ground-truth denoising step
Figure 5: Depicted is an alternate, lower-variance method to optimize a VDM; we compute the form of ground-truth denoising step using Bayes rule, and minimize its KL Divergence with our approximate denoising step . This is once again denoted visually by matching the distributions represented by the green arrows with those of the pink arrows. Artistic liberty is at play here; in the full picture, each pink arrow must also stem from , as it is also a conditioning term.

As we already know that from our assumption regarding encoder transitions (Equation 31), what remains is deriving for the forms of and . Fortunately, these are also made tractable by utilizing the fact that the encoder transitions of a VDM are linear Gaussian models. Recall that under the reparameterization trick, samples can be rewritten as:

(59)

and that similarly, samples can be rewritten as:

(60)

Then, the form of can be recursively derived through repeated applications of the reparameterization trick. Suppose that we have access to 2 random noise variables . Then, for an arbitrary sample , we can rewrite it as:

(61)
(62)
(63)
(64)
(65)
(66)
(67)
(68)
(69)
(70)

where in Equation 64 we have utilized the fact that the sum of two independent Gaussian random variables remains a Gaussian with mean being the sum of the two means, and variance being the sum of the two variances. Interpreting as a sample from Gaussian , and as a sample from Gaussian , we can then treat their sum as a random variable sampled from Gaussian . A sample from this distribution can then be represented using the reparameterization trick as , as in Equation 66.

We have therefore derived the Gaussian form of . This derivation can be modified to also yield the Gaussian parameterization describing . Now, knowing the forms of both and , we can proceed to calculate the form of by substituting into the Bayes rule expansion:

(71)
(72)
(73)
(74)
(75)
(76)
(77)
(78)
(79)
(80)
(81)
(82)
(83)
(84)

where in Equation 75, is a constant term with respect to computed as a combination of only , , and values; this term is implicitly returned in Equation 84 to complete the square.

We have therefore shown that at each step, is normally distributed, with mean that is a function of and , and variance as a function of coefficients. These coefficients are known and fixed at each timestep; they are either set permanently when modeled as hyperparameters, or treated as the current inference output of a network that seeks to model them. Following Equation 84, we can rewrite our variance equation as , where:

(85)

In order to match approximate denoising transition step to ground-truth denoising transition step as closely as possible, we can also model it as a Gaussian. Furthermore, as all terms are known to be frozen at each timestep, we can immediately construct the variance of the approximate denoising transition step to also be . We must parameterize its mean as a function of , however, since does not condition on .

Recall that the KL Divergence between two Gaussian distributions is:

(86)

In our case, where we can set the variances of the two Gaussians to match exactly, optimizing the KL Divergence term reduces to minimizing the difference between the means of the two distributions:

(87)
(88)
(89)
(90)
(91)
(92)

where we have written as shorthand for , and as shorthand for for brevity. In other words, we want to optimize a that matches , which from our derived Equation 84, takes the form:

(93)

As also conditions on , we can match closely by setting it to the following form:

(94)

where is parameterized by a neural network that seeks to predict from noisy image and time index . Then, the optimization problem simplifies to:

(95)
(96)
(97)
(98)
(99)

Therefore, optimizing a VDM boils down to learning a neural network to predict the original ground truth image from an arbitrarily noisified version of it [3]. Furthermore, minimizing the summation term of our derived ELBO objective (Equation 58) across all noise levels can be approximated by minimizing the expectation over all timesteps:

(100)

which can then be optimized using stochastic samples over timesteps.

Learning Diffusion Noise Parameters

Let us investigate how the noise parameters of a VDM can be jointly learned. One potential approach is to model using a neural network with parameters . However, this is inefficient as inference must be performed multiple times at each timestep to compute . Whereas caching can mitigate this computational cost, we can also derive an alternate way to learn the diffusion noise parameters. By substituting our variance equation from Equation 85 into our derived per-timestep objective in Equation 99, we can reduce:

(101)
(102)
(103)
(104)
(105)
(106)
(107)
(108)

Recall from Equation 70 that is a Gaussian of form . Then, following the definition of the signal-to-noise ratio (SNR) as , we can write the SNR at each timestep as:

(109)

Then, our derived Equation 108 (and Equation 99) can be simplified as:

(110)

As the name implies, the SNR represents the ratio between the original signal and the amount of noise present; a higher SNR represents more signal and a lower SNR represents more noise. In a diffusion model, we require the SNR to monotonically decrease as timestep increases; this formalizes the notion that perturbed input becomes increasingly noisy over time, until it becomes identical to a standard Gaussian at .

Following the simplification of the objective in Equation 110, we can directly parameterize the SNR at each timestep using a neural network, and learn it jointly along with the diffusion model. As the SNR must monotonically decrease over time, we can represent it as:

(111)

where is modeled as a monotonically increasing neural network with parameters . Negating results in a monotonically decreasing function, whereas the exponential forces the resulting term to be positive. Note that the objective in Equation 100 must now optimize over as well. By combining our parameterization of SNR in Equation 111 with our definition of SNR in Equation 109, we can also explicitly derive elegant forms for the value of as well as for the value of :

(112)
(113)
(114)

These terms are necessary for a variety of computations; for example, during optimization, they are used to create arbitrarily noisy from input using the reparameterization trick, as derived in Equation 69.

Three Equivalent Interpretations

As we previously proved, a Variational Diffusion Model can be trained by simply learning a neural network to predict the original natural image from an arbitrary noised version and its time index . However, has two other equivalent parameterizations, which leads to two further interpretations for a VDM.

Firstly, we can utilize the reparameterization trick. In our derivation of the form of , we can rearrange Equation 69 to show that:

(115)

Plugging this into our previously derived true denoising transition mean , we can rederive as:

(116)
(117)
(118)
(119)
(120)
(121)
(122)
(123)
(124)

Therefore, we can set our approximate denoising transition mean as:

(125)

and the corresponding optimization problem becomes:

(126)
(127)
(128)
(129)
(130)

Here, is a neural network that learns to predict the source noise that determines from . We have therefore shown that learning a VDM by predicting the original image is equivalent to learning to predict the noise; empirically, however, some works have found that predicting the noise resulted in better performance [3, 12].

To derive the third common interpretation of Variational Diffusion Models, we appeal to Tweedie’s Formula [2]. In English, Tweedie’s Formula states that the true mean of an exponential family distribution, given samples drawn from it, can be estimated by the maximum likelihood estimate of the samples (aka empirical mean) plus some correction term involving the score of the estimate. In the case of just one observed sample, the empirical mean is just the sample itself. It is commonly used to mitigate sample bias; if observed samples all lie on one end of the underlying distribution, then the negative score becomes large and corrects the naive maximum likelihood estimate of the samples towards the true mean.

Mathematically, for a Gaussian variable , Tweedie’s Formula states that:

In this case, we apply it to predict the true posterior mean of given its samples. From Equation 70, we know that:

Then, by Tweedie’s Formula, we have:

(131)

where we write as for notational simplicity. According to Tweedie’s Formula, the best estimate for the true mean that is generated from, , is defined as:

(132)
(133)

Then, we can plug Equation 133 into our ground-truth denoising transition mean once again and derive a new form:

(134)
(135)
(136)
(137)
(138)
(139)
(140)
(141)
(142)

Therefore, we can also set our approximate denoising transition mean as:

(143)

and the corresponding optimization problem becomes:

(144)
(145)
(146)
(147)
(148)

Here, is a neural network that learns to predict the score function , which is the gradient of in data space, for any arbitrary noise level .

The astute reader will notice that the score function looks remarkably similar in form to the source noise . This can be shown explicitly by combining Tweedie’s Formula (Equation 133) with the reparameterization trick (Equation 115):

(149)
(150)
(151)

As it turns out, the two terms are off by a constant factor that scales with time! The score function measures how to move in data space to maximize the log probability; intuitively, since the source noise is added to a natural image to corrupt it, moving in its opposite direction "denoises" the image and would be the best update to increase the subsequent log probability. Our mathematical proof justifies this intuition; we have explicitly shown that learning to model the score function is equivalent to modeling the negative of the source noise (up to a scaling factor).

We have therefore derived three equivalent objectives to optimize a VDM: learning a neural network to predict the original image , the source noise , or the score of the image at an arbitrary noise level . The VDM can be scalably trained by stochastically sampling timesteps and minimizing the norm of the prediction with the ground truth target.

Score-based Generative Models

We have shown that a Variational Diffusion Model can be learned simply by optimizing a neural network to predict the score function . However, in our derivation, the score term arrived from an application of Tweedie’s Formula; this doesn’t necessarily provide us with great intuition or insight into what exactly the score function is or why it is worth modeling. Fortunately, we can look to another class of generative models, Score-based Generative Models [16, 20, 17], for exactly this intuition. As it turns out, we can show that the VDM formulation we have previously derived has an equivalent Score-based Generative Modeling formulation, allowing us to flexibly switch between these two interpretations at will.

To begin to understand why optimizing a score function makes sense, we take a detour and revisit energy-based models [10, 19]. Arbitrarily flexible probability distributions can be written in the form:

(152)

where is an arbitrarily flexible, parameterizable function called the energy function, often modeled by a neural network, and is a normalizing constant to ensure that . One way to learn such a distribution is maximum likelihood; however, this requires tractably computing the normalizing constant , which may not be possible for complex functions.

Visualization of three random sampling trajectories generated with Langevin dynamics, all starting from the same initialization point, for a Mixture of Gaussians. The left figure plots these sampling trajectories on a three-dimensional contour, while the right figure plots the sampling trajectories against the ground-truth score function. From the same initialization point, we are able to generate samples from different modes due to the stochastic noise term in the Langevin dynamics sampling procedure; without it, sampling from a fixed point would always deterministically follow the score to the same mode every trial. Visualization of three random sampling trajectories generated with Langevin dynamics, all starting from the same initialization point, for a Mixture of Gaussians. The left figure plots these sampling trajectories on a three-dimensional contour, while the right figure plots the sampling trajectories against the ground-truth score function. From the same initialization point, we are able to generate samples from different modes due to the stochastic noise term in the Langevin dynamics sampling procedure; without it, sampling from a fixed point would always deterministically follow the score to the same mode every trial.
Figure 6: Visualization of three random sampling trajectories generated with Langevin dynamics, all starting from the same initialization point, for a Mixture of Gaussians. The left figure plots these sampling trajectories on a three-dimensional contour, while the right figure plots the sampling trajectories against the ground-truth score function. From the same initialization point, we are able to generate samples from different modes due to the stochastic noise term in the Langevin dynamics sampling procedure; without it, sampling from a fixed point would always deterministically follow the score to the same mode every trial.

One way to avoid calculating or modeling the normalization constant is by using a neural network to learn the score function of distribution instead. This is motivated by the observation that taking the derivative of the log of both sides of Equation 152 yields:

(153)
(154)
(155)
(156)

which can be freely represented as a neural network without involving any normalization constants. The score model can be optimized by minimizing the Fisher Divergence with the ground truth score function:

(157)

What does the score function represent? For every , taking the gradient of its log likelihood with respect to essentially describes what direction in data space to move in order to further increase its likelihood. Intuitively, then, the score function defines a vector field over the entire space that data inhabits, pointing towards the modes. Visually, this is depicted in the right plot of Figure 6. Then, by learning the score function of the true data distribution, we can generate samples by starting at any arbitrary point in the same space and iteratively following the score until a mode is reached. This sampling procedure is known as Langevin dynamics, and is mathematically described as:

(158)

where is randomly sampled from a prior distribution (such as uniform), and is an extra noise term to ensure that the generated samples do not always collapse onto a mode, but hover around it for diversity. Furthermore, because the learned score function is deterministic, sampling with a noise term involved adds stochasticity to the generative process, allowing us to avoid deterministic trajectories. This is particularly useful when sampling is initialized from a position that lies between multiple modes. A visual depiction of Langevin dynamics sampling and the benefits of the noise term is shown in Figure 6.

Note that the objective in Equation 157 relies on having access to the ground truth score function, which is unavailable to us for complex distributions such as the one modeling natural images. Fortunately, alternative techniques known as score matching [6, 13, 18, 21] have been derived to minimize this Fisher divergence without knowing the ground truth score, and can be optimized with stochastic gradient descent.

Collectively, learning to represent a distribution as a score function and using it to generate samples through Markov Chain Monte Carlo techniques, such as Langevin dynamics, is known as Score-based Generative Modeling [16, 20, 17].

There are three main problems with vanilla score matching, as detailed by  Song and Ermon [16]. Firstly, the score function is ill-defined when lies on a low-dimensional manifold in a high-dimensional space. This can be seen mathematically; all points not on the low-dimensional manifold would have probability zero, the log of which is undefined. This is particularly inconvenient when trying to learn a generative model over natural images, which is known to lie on a low-dimensional manifold of the entire ambient space.

Secondly, the estimated score function trained via vanilla score matching will not be accurate in low density regions. This is evident from the objective we minimize in Equation 157. Because it is an expectation over , and explicitly trained on samples from it, the model will not receive an accurate learning signal for rarely seen or unseen examples. This is problematic, since our sampling strategy involves starting from a random location in the high-dimensional space, which is most likely random noise, and moving according to the learned score function. Since we are following a noisy or inaccurate score estimate, the final generated samples may be suboptimal as well, or require many more iterations to converge on an accurate output.

Lastly, Langevin dynamics sampling may not mix, even if it is performed using the ground truth scores. Suppose that the true data distribution is a mixture of two disjoint distributions:

(159)

Then, when the score is computed, these mixing coefficients are lost, since the log operation splits the coefficient from the distribution and the gradient operation zeros it out. To visualize this, note that the ground truth score function shown in the right Figure 6 is agnostic of the different weights between the three distributions; Langevin dynamics sampling from the depicted initialization point has a roughly equal chance of arriving at each mode, despite the bottom right mode having a higher weight in the actual Mixture of Gaussians.

It turns out that these three drawbacks can be simultaneously addressed by adding multiple levels of Gaussian noise to the data. Firstly, as the support of a Gaussian noise distribution is the entire space, a perturbed data sample will no longer be confined to a low-dimensional manifold. Secondly, adding large Gaussian noise will increase the area each mode covers in the data distribution, adding more training signal in low density regions. Lastly, adding multiple levels of Gaussian noise with increasing variance will result in intermediate distributions that respect the ground truth mixing coefficients.

Formally, we can choose a positive sequence of noise levels and define a sequence of progressively perturbed data distributions:

(160)

Then, a neural network is learned using score matching to learn the score function for all noise levels simultaneously:

(161)

where is a positive weighting function that conditions on noise level . Note that this objective almost exactly matches the objective derived in Equation 148 to train a Variational Diffusion Model. Furthermore, the authors propose annealed Langevin dynamics sampling as a generative procedure, in which samples are produced by running Langevin dynamics for each in sequence. The initialization is chosen from some fixed prior (such as uniform), and each subsequent sampling step starts from the final samples of the previous simulation. Because the noise levels steadily decrease over timesteps , and we reduce the step size over time, the samples eventually converge into a true mode. This is directly analogous to the sampling procedure performed in the Markovian HVAE interpretation of a Variational Diffusion Model, where a randomly initialized data vector is iteratively refined over decreasing noise levels.

Therefore, we have established an explicit connection between Variational Diffusion Models and Score-based Generative Models, both in their training objectives and sampling procedures.

One question is how to naturally generalize diffusion models to an infinite number of timesteps. Under the Markovian HVAE view, this can be interpreted as extending the number of hierarchies to infinity . It is clearer to represent this from the equivalent score-based generative model perspective; under an infinite number of noise scales, the perturbation of an image over continuous time can be represented as a stochastic process, and therefore described by a stochastic differential equation (SDE). Sampling is then performed by reversing the SDE, which naturally requires estimating the score function at each continuous-valued noise level [20]. Different parameterizations of the SDE essentially describe different perturbation schemes over time, enabling flexible modeling of the noising procedure [8].

Guidance

So far, we have focused on modeling just the data distribution . However, we are often also interested in learning conditional distribution , which would enable us to explicitly control the data we generate through conditioning information . This forms the backbone of image super-resolution models such as Cascaded Diffusion Models [4], as well as state-of-the-art image-text models such as DALL-E 2 [11] and Imagen [12].

A natural way to add conditioning information is simply alongside the timestep information, at each iteration. Recall our joint distribution from Equation 32:

Then, to turn this into a conditional diffusion model, we can simply add arbitrary conditioning information at each transition step as:

(162)

For example, could be a text encoding in image-text generation, or a low-resolution image to perform super-resolution on. We are thus able to learn the core neural networks of a VDM as before, by predicting , , or for each desired interpretation and implementation.

A caveat of this vanilla formulation is that a conditional diffusion model trained in this way may potentially learn to ignore or downplay any given conditioning information. Guidance is therefore proposed as a way to more explicitly control the amount of weight the model gives to the conditioning information, at the cost of sample diversity. The two most popular forms of guidance are known as Classifier Guidance [20, 1] and Classifier-Free Guidance [5].

Classifier Guidance

Let us begin with the score-based formulation of a diffusion model, where our goal is to learn , the score of the conditional model, at arbitrary noise levels . Recall that is shorthand for in the interest of brevity. By Bayes rule, we can derive the following equivalent form:

(163)
(164)
(165)

where we have leveraged the fact that the gradient of with respect to is zero.

Our final derived result can be interpreted as learning an unconditional score function combined with the adversarial gradient of a classifier . Therefore, in Classifier Guidance [20, 1], the score of an unconditional diffusion model is learned as previously derived, alongside a classifier that takes in arbitrary noisy and attempts to predict conditional information . Then, during the sampling procedure, the overall conditional score function used for annealed Langevin dynamics is computed as the sum of the unconditional score function and the adversarial gradient of the noisy classifier.

In order to introduce fine-grained control to either encourage or discourage the model to consider the conditioning information, Classifier Guidance scales the adversarial gradient of the noisy classifier by a hyperparameter term. The score function learned under Classifier Guidance can then be summarized as:

(166)

Intuitively, when the conditional diffusion model learns to ignore the conditioning information entirely, and when is large the conditional diffusion model learns to produce samples that heavily adhere to the conditioning information. This would come at the cost of sample diversity, as it would only produce data that would be easy to regenerate the provided conditioning information from, even at noisy levels.

One noted drawback of Classifier Guidance is its reliance on a separately learned classifier. Because the classifier must handle arbitrarily noisy inputs, which most existing pretrained classification models are not optimized to do, it must be learned ad hoc alongside the diffusion model.

Classifier-Free Guidance

In Classifier-Free Guidance [5], the authors ditch the training of a separate classifier model in favor of an unconditional diffusion model and a conditional diffusion model. To derive the score function under Classifier-Free Guidance, we can first rearrange Equation 165 to show that:

(167)

Then, substituting this into Equation 166, we get:

(168)
(169)
(170)

Once again, is a term that controls how much our learned conditional model cares about the conditioning information. When , the learned conditional model completely ignores the conditioner and learns an unconditional diffusion model. When , the model explicitly learns the vanilla conditional distribution without guidance. When , the diffusion model not only prioritizes the conditional score function, but also moves in the direction away from the unconditional score function. In other words, it reduces the probability of generating samples that do not use conditioning information, in favor of the samples that explicitly do. This also has the effect of decreasing sample diversity at the cost of generating samples that accurately match the conditioning information.

Because learning two separate diffusion models is expensive, we can learn both the conditional and unconditional diffusion models together as a singular conditional model; the unconditional diffusion model can be queried by replacing the conditioning information with fixed constant values, such as zeros. This is essentially performing random dropout on the conditioning information. Classifier-Free Guidance is elegant because it enables us greater control over our conditional generation procedure while requiring nothing beyond the training of a singular diffusion model.

Closing

Allow us to recapitulate our findings over the course of our explorations. First, we derive Variational Diffusion Models as a special case of a Markovian Hierarchical Variational Autoencoder, where three key assumptions enable tractable computation and scalable optimization of the ELBO. We then prove that optimizing a VDM boils down to learning a neural network to predict one of three potential objectives: the original source image from any arbitrary noisification of it, the original source noise from any arbitrarily noisified image, or the score function of a noisified image at any arbitrary noise level. Then, we dive deeper into what it means to learn the score function, and connect it explicitly with the perspective of Score-based Generative Modeling. Lastly, we cover how to learn a conditional distribution using diffusion models.

In summary, diffusion models have shown incredible capabilities as generative models; indeed, they power the current state-of-the-art models on text-conditioned image generation such as Imagen and DALL-E 2. Furthermore, the mathematics that enable these models are exceedingly elegant. However, there still remain a few drawbacks to consider:

  • It is unlikely that this is how we, as humans, naturally model and generate data; we do not generate samples as random noise that we iteratively denoise.

  • The VDM does not produce interpretable latents. Whereas a VAE would hopefully learn a structured latent space through the optimization of its encoder, in a VDM the encoder at each timestep is already given as a linear Gaussian model and cannot be optimized flexibly. Therefore, the intermediate latents are restricted as just noisy versions of the original input.

  • The latents are restricted to the same dimensionality as the original input, further frustrating efforts to learn meaningful, compressed latent structure.

  • Sampling is an expensive procedure, as multiple denoising steps must be run under both formulations. Recall that one of the restrictions is that a large enough number of timesteps is chosen to ensure the final latent is completely Gaussian noise; during sampling we must iterate over all these timesteps to generate a sample.

As a final note, the success of diffusion models highlights the power of Hierarchical VAEs as a generative model. We have shown that when we generalize to infinite latent hierarchies, even if the encoder is trivial and the latent dimension is fixed and Markovian transitions are assumed, we are still able to learn powerful models of data. This suggests that further performance gains can be achieved in the case of general, deep HVAEs, where complex encoders and semantically meaningful latent spaces can be potentially learned.

Acknowledgments: I would like to acknowledge Josh Dillon, Yang Song, Durk Kingma, Ben Poole, Jonathan Ho, Yiding Jiang, Ting Chen, Jeremy Cohen, and Chen Sun for reviewing drafts of this work and providing many helpful edits and comments. Thanks so much!

References

  • [1] P. Dhariwal and A. Nichol (2021) Diffusion models beat gans on image synthesis. Advances in Neural Information Processing Systems 34, pp. 8780–8794. Cited by: Classifier Guidance, Guidance.
  • [2] B. Efron (2011) Tweedie’s formula and selection bias. Journal of the American Statistical Association 106 (496), pp. 1602–1614. Cited by: Three Equivalent Interpretations.
  • [3] J. Ho, A. Jain, and P. Abbeel (2020) Denoising diffusion probabilistic models. Advances in Neural Information Processing Systems 33, pp. 6840–6851. Cited by: Three Equivalent Interpretations, Variational Diffusion Models, Variational Diffusion Models, Variational Diffusion Models.
  • [4] J. Ho, C. Saharia, W. Chan, D. J. Fleet, M. Norouzi, and T. Salimans (2022) Cascaded diffusion models for high fidelity image generation.. J. Mach. Learn. Res. 23, pp. 47–1. Cited by: Guidance.
  • [5] J. Ho and T. Salimans (2021) Classifier-free diffusion guidance. In NeurIPS 2021 Workshop on Deep Generative Models and Downstream Applications, Cited by: Classifier-Free Guidance, Guidance.
  • [6] A. Hyvärinen and P. Dayan (2005) Estimation of non-normalized statistical models by score matching.. Journal of Machine Learning Research 6 (4). Cited by: Score-based Generative Models.
  • [7] D. P. Kingma and M. Welling (2013) Auto-encoding variational bayes. arXiv preprint arXiv:1312.6114. Cited by: Variational Autoencoders.
  • [8] D. Kingma, T. Salimans, B. Poole, and J. Ho (2021) Variational diffusion models. Advances in neural information processing systems 34, pp. 21696–21707. Cited by: Variational Diffusion Models, Variational Diffusion Models, Score-based Generative Models.
  • [9] D. P. Kingma, T. Salimans, R. Jozefowicz, X. Chen, I. Sutskever, and M. Welling (2016) Improved variational inference with inverse autoregressive flow. Advances in neural information processing systems 29. Cited by: Hierarchical Variational Autoencoders.
  • [10] Y. LeCun, S. Chopra, R. Hadsell, M. Ranzato, and F. Huang (2006) A tutorial on energy-based learning. Predicting structured data 1 (0). Cited by: Score-based Generative Models.
  • [11] A. Ramesh, P. Dhariwal, A. Nichol, C. Chu, and M. Chen (2022) Hierarchical text-conditional image generation with clip latents. arXiv preprint arXiv:2204.06125. Cited by: Guidance.
  • [12] C. Saharia, W. Chan, S. Saxena, L. Li, J. Whang, E. Denton, S. K. S. Ghasemipour, B. K. Ayan, S. S. Mahdavi, R. G. Lopes, et al. (2022) Photorealistic text-to-image diffusion models with deep language understanding. arXiv preprint arXiv:2205.11487. Cited by: Three Equivalent Interpretations, Guidance.
  • [13] S. Saremi, A. Mehrjou, B. Schölkopf, and A. Hyvärinen (2018) Deep energy estimator networks. arXiv preprint arXiv:1805.08306. Cited by: Score-based Generative Models.
  • [14] J. Sohl-Dickstein, E. Weiss, N. Maheswaranathan, and S. Ganguli (2015) Deep unsupervised learning using nonequilibrium thermodynamics. In International Conference on Machine Learning, pp. 2256–2265. Cited by: Variational Diffusion Models.
  • [15] C. K. Sønderby, T. Raiko, L. Maaløe, S. K. Sønderby, and O. Winther (2016) Ladder variational autoencoders. Advances in neural information processing systems 29. Cited by: Hierarchical Variational Autoencoders.
  • [16] Y. Song and S. Ermon (2019) Generative modeling by estimating gradients of the data distribution. Advances in Neural Information Processing Systems 32. Cited by: Score-based Generative Models, Score-based Generative Models, Score-based Generative Models.
  • [17] Y. Song and S. Ermon (2020) Improved techniques for training score-based generative models. Advances in neural information processing systems 33, pp. 12438–12448. Cited by: Score-based Generative Models, Score-based Generative Models.
  • [18] Y. Song, S. Garg, J. Shi, and S. Ermon (2020) Sliced score matching: a scalable approach to density and score estimation. In Uncertainty in Artificial Intelligence, pp. 574–584. Cited by: Score-based Generative Models.
  • [19] Y. Song and D. P. Kingma (2021) How to train your energy-based models. arXiv preprint arXiv:2101.03288. Cited by: Score-based Generative Models.
  • [20] Y. Song, J. Sohl-Dickstein, D. P. Kingma, A. Kumar, S. Ermon, and B. Poole (2020) Score-based generative modeling through stochastic differential equations. arXiv preprint arXiv:2011.13456. Cited by: Score-based Generative Models, Score-based Generative Models, Score-based Generative Models, Classifier Guidance, Guidance.
  • [21] P. Vincent (2011) A connection between score matching and denoising autoencoders. Neural computation 23 (7), pp. 1661–1674. Cited by: Score-based Generative Models.