Mathematics in VAE for Engineers.(WIP)
Background Knowledge
Bayesian Statistics Terminologies
| Name | Notation | Description |
|---|---|---|
| Prior | The overall distribution of the latent variable . For example, | |
| likelihood | The probability of the observed variable given the latent variable . It also describes the generative process, so it is also called probabilistic decoder. | |
| posterior | The probability of the latent variable given the observed variable . | |
| Evidence | You can think of Evidence as how likely a generative model is to generate a particular image or how reasonable the model considers this image to be. Also called Marginal Likelihood in original VAE paper |
These four distributions are related by Bayes’ theorem:
Research Background
Since Encoder-Decoder architecture has become super common in deep learning, we won’t discuss the application of VAE in detail. Instead, we will focus on the math behind VAE and try to restore the history background of when VAE was proposed.
Problem statement
A common misconception is that VAE was originally motivated purely by the task of generating new data. In fact, one of its central motivations was a classical problem in probabilistic modeling: when the posterior over the latent variables is intractable, how can we efficiently infer the latent variables and learn the parameters of a generative model? You might ask, why is the posterior inference problem important? Generative models seem to be much more popular. In statistics, a classical scenario is that we observed some data , which can be huge, and we want to know what process produced them.1
Since we are interested in the posterior inference problem, we can first assume that the decoder is a neural network, while its parameters remain unknown. We denote it as , where represents the parameters of the neural network. And, as the distribution over latent space, is determined by Encoder.2 Since both encoder and decoder are something we are going to find, we can simply impose a prior distribution(e.g., a standard isotropic Gaussian) to act as a “constraint” on both models. Then according to Bayes’ theorem, we can compute the posterior . The only problem is that the evidence is often intractable when the likelihood is parameterized by a nonlinear neural network, especially when the latent space is high-dimensional.
Wait, why can you just assume that the generative model is known? How about we assume that the posterior is known? Just use a neural network to fit it, then the problem is solved! Of course you can do that, but then the Bayes’ theorem would become , then is still intractable. In other words, once you know side of the Bayes’ theorem, you already have a theoretical solution provided by it; we just cannot obtain its explicit form.
What if we use two neural networks to model the generative process and the posterior inference process simultaneously? This is the central idea behind VAE. We use a decoder to model how data are generated from latent variables, and an encoder to approximate the intractable posterior (). However, this introduces a new challenge: how can we jointly optimize the two networks when the true posterior and the marginal likelihood are intractable?
Design: VAE Derivation
Remember that we now have a Decoder and a theoretical Encoder derived from our decoder via Bayes’ theorem. Since we can’t obtain the explicit form of this theoretical Encoder, we are now using another neural network, with its parameters denoted as , to approximate it. This approximated Encoder is denoted as .
Evidence Lower Bound (ELBO)
For simplicity’s sake, I will use to represent and to represent in this section.
Since one of our goals is to make to be as close to as possible, we can simply minimize its KL divergence.
And don’t forget that we still don’t know the Decoder’s parameters at this point. So another goal is naturally optimize our Decoder, which means to maximize the probability of generating data in our datasets . We will start with only KL divergence, in later steps you will see emerges by itself.
Because of the definition of expected value, we can rewrite this integral as
We know that , thus
is not related to , thus .
Here we get both variables we want to optimize in a single equation. And notice that
meaning by maximizing we can minimize the KL divergence and maximize at the same time! We now call this useful term Evidence Lower Bound or ELBO for short
, since
and , thus is the lower bound of log evidence .
Find the Gradient of ELBO
Now the issue becomes how do we maximize ELBO. A natural solution is to find its gradient, and then we can perform gradient descent to . Let’s first try to find the gradient w.r.t4 .
Since the second term is not related to , its gradient w.r.t is just zero.
After The Reparameterization Trick, we can perform Monte Carlo sampling to estimate the value of this expectation. The basic idea is just sampling bunch of latents out of the distribution , which can be done after The Reparameterization Trick, then calculate the average value of the gradient given those z.
Where means the number of Monte Carlo samples.
To sum up, so far there is nothing blocking us to find the gradient of the ELBO w.r.t . But we won’t be that lucky when we try to find the gradient w.r.t .
Once again, we can do Monte Carlo sampling to the first term since it is an expectation. But we can’t do so to the second term. That’s exactly where The Reparameterization Trick comes in.
The Reparameterization Trick
The origin of the problem is that , which is the distribution we’re sampling from, is related to the value we are finding gradient with respect to (). So the solution here is to replace with an unrelated distribution, while keeping the value of ELBO unchanged. That sounds impossible first, but a special design is applied here to make this possible.
The encoder network doesn’t directly output a latent variable , instead, it gives you mean and variance according to deterministically. Later we sample an from the Gaussian distribution given those parameters.
We denote the mean and variance as and , thus we have
This is also true for the decoder. (In some cases, the variance is fixed.)
We will use this feature later on.
Now we know that is sampled from an Gaussian distribution, thus
With that in mind, now let’s define a new variable , sampled from Standard Normal distribution . Then there is the key step in The Reparameterization Trick: we can use linear transformation(denoted as ) to convert to .
Let’s now replace all the with
At this point, the distribution used for sampling the expectation has been changed from the problematic to , which is independent of the optimizable parameters . Thus, finding gradient is not a problem anymore:
This optimization method is called Stochastic Gradient Variational Bayes or SGVB. To sum up, SGVB did 2 things:
- Monte Carlo estimates the expectation.
- Reparameterization separates the randomness from the optimizable parameters, making gradient can be propagated to .
Separate the Reconstruction Term and Regularization Term
Having gone through the journey above, we are now ready to optimize the ELBO. But we can do even better.
For simplicity, I will use the letter below to denote , but please keep in mind that this is the result after reparameterization.
This way, the Reconstruction Term can keep being estimated using Monte Carlo sampling, while the KL Divergence can be computed analytically when both distributions are Gaussian.( because we want our latent space to be Gaussian.)
As the same trick played in The Reparameterization Trick, can be represented as
Again, for simplicity, I will use the and below to denote and . Then the KL Divergence can be expanded as follows:
Using the properties of the Gaussian distribution:
Substituting these results:
Substituting the closed-form KL Divergence into the ELBO:
where and .
From Theory to Loss Function
As we mentioned earlier, the network gives you means and variances, thus is also Gaussian.
In the paper’s experiment they found that the number of samples per data point can be set to 1 as long as the minibatch size was large enough. Thus can be further simplified as
If we now substitute back and don’t consider for now, is proportional to , which is exactly MSE loss.
For the Gaussian Decoder with a fixed unit variance, the training Loss for each data point in the end is:
- 1For example, we observed a lot of symptoms of your body , then you might want to know what factors cause these symptoms, such as disease.
- 2In inference perspective, if you integrate over all the data , you do get the distribution of .
- 3Strictly speaking, Monte Carlo sampling is used to estimate the expectation of a distribution.
- 4with respect to