Variational autoencoders are probabilistic generative models; they aim to learn a distribution over the data. After training, we can draw (generate) samples from this distribution. However, unlike normalizing flows, VAEs cannot evaluate the probability of new samples exactly, although we can approximate it using importance sampling.

Note that the VAE is not the model of ; it is the neural architecture that is designed to learn the model for . The final model for contains neither the “variational” nor the “autoencoder” parts and might be better described as a nonlinear latent variable model, where we model a joint distribution of the data and an unobserved hidden or latent variable .

Specifically, we use:

  • A prior distribution over the latent variable.
  • A network that maps a latent to the data space (decoder).

Then, the likelihood (conditional to ) can be found as

which can then be marginalized over to get the data probability:

ELBO Objective

To train the model, we maximize the log-likelihood over a training dataset with respect to the model parameters. For simplicity, we assume that the variance term in the likelihood expression is known and concentrate on learning :

where

Unfortunately, this is intractable. There is no closed-form expression for the integral and no easy way to evaluate it for a particular value of .

To make progress, we define a lower bound on the log-likehood with ELBO. Given some distribution with parameters , the ELBO can be written in 3 forms:

  • The first form is a naive derivation from Jensen’s Inequality
  • The second form shows that ELBO is equal to the log-likelihood (tight) when
  • The third form expresses ELBO in terms of a reconstruction accuracy between the latent variable and the data (first term) and the similarity between the auxiliary distribution and the prior.

To learn the nonlinear latent variable model, we maximize this quantity as a function of both and . The neural architecture that computes this quantity is the VAE.

Variational approximation

We saw that ELBO tight when is the posterior . In principle, we can compute the posterior using Bayes’ rule:

This is unfortunately also intractable because we can’t evaluate the evidence term in the denominator.

One solution is to make a variational approximation: we choose a simple parametric form for and use this to approximate the true posterior . Here, we choose a multivariate normal distribution with mean and diagonal covariance . This will not always match the posterior well, but will be better for some values of and than others. During training, we will find the normal distribution that is “closest” to the true posterior, which conceptually corresponds to minimizing the KL divergence in the second form of ELBO above. We cannot evaluate this posterior KL directly because the true posterior is intractable; instead, we maximize the algebraically equivalent reconstruction-minus-prior-KL form of ELBO (third form above) to train as we can use the known prior .

Since the optimal choice for was the posterior , and this depends on the data example , the variational approximation should do the same, so we choose:

where is a second neural network (encoder) with parameters that predicts the mean and variance of the normal variational approximation.

VAE Formulation

Finally, we can describe the VAE. We build a network that computes the third form of ELBO:

where the distribution is the approximation from above.

The first term still involves an intractable integral, but since it is an expectation with respect to , we can approximate it by sampling. For any function we have:

where is the -th sample from . This is known as the Monte Carlo estimate.

For a very approximate estimate, we can just use a single sample from :

The second term is the KL divergence between the variational distribution and the prior . The KL divergence between two normal distributions can be calculated in closed form. In this case, one of the distributions has parameters and the other is a standard normal, giving us

where is the dimensionality of the latent space.

Architecture

So, we want to build a model that computes the evidence lower bound for a point . Then we use an optimization algorithm to maximize this lower bound over the dataset and hence improve the log-likelihood.

To compute the ELBO we:

  • Compute the mean and variance of the variational posterior distribution for this data point using the network .
  • Draw a sample from the distribution.
  • Compute the ELBO using the boxed equation above.

The associated architecture is shown below.

This architecture is:

  • Variational because it computes a Gaussian approximation to the posterior distribution.
  • Autoencoder because it starts with a data point , computes a lower-dimensional latent vector from this, and then uses this to vector to recreate the data point as closely as possible.
    • The mapping from the data to the latent variable by the network is called the encoder.
    • The mapping from the latent variable to the data by the network is called the decoder.

The VAE computes the ELBO as a function of both and . To maximize this bound, we run mini-batches of samples through the network and update these parameters with an optimization algorithm such as SGD or Adam. The gradients of the ELBO with respect to the parameters are computed as usual using automatic differentiation. During this process, we are both moving between the colored curves (changing ) and along them (changing ). During this process, the parameters change to assign the data a higher likelihood in the nonlinear latent variable model.

  • The encoder approximates the posterior distribution over the latent variables to make the bound tight, and the decoder maximizes this bound.

Reparameterization trick

The network involves a sampling step, and it is difficult to differentiate through this stochastic component. However, we need to differentiate past this step to update the parameters that precede it in the network.

Fortunately, there is a simple solution; we move the stochastic part into a branch of the network that draws from a sample from and then use the relation:

to draw from the intended Gaussian. Now, we can compute the derivatives as usual because the backpropagation algorithm does not need to pass down the stochastic branch. This is known as the reparameterization trick.

Sampling

To sample from a VAE, we can simply draw from the prior over the latent variable, pass the result through the decoder, and add noise according to . In practice, for high-quality generation, we want to use some tricks.

dl
How do you generate new samples with a VAE?::Sample from the latent prior distribution, pass the result through the decoder, and add independent Gaussian noise.

For VAE training, how do we deal with not being able to get exact likelihood of a data point?::The exact likelihood being intractable poses problems for training with maximum likelihood. Thus, we define a lower bound (ELBO) and maximize this bound.

What is the variational posterior approximation in a VAE?::For the ELBO bound to be tight, we need to compute the posterior probability of the latent variable given the observed data . This is unfortunately also intractable, so we use the variational approximation – a simpler distribution (Gaussian) that approximates the posterior, whose parameters are computed by the encoder network.