Latent Variable Models

Suppose you would like to model the world in terms of the probability distribution over its possible states with . The world may be complicated and we do not know what form should have. To account for it, we introduce another variable , which describes, or explains the content of . If is an image, can contain information about the number, type and appearance of objects visible in the scene as well as the background and lighting conditions. This new variable allows us to express as an infinite mixture model,

It is a mixture model, because for every possible value of , we add another conditional distribution to , weighted by its probability.

Having a setup like that, it is interesting to ask what the latent variables are, given an observation . Namely, we would like to know the posterior distribution . However, the relationship between and can be highly non-linear (e.g. implemented by a multi-layer neural network) and both , the dimensionality of our observations, and , the dimensionality of the latent variable, can be quite large. Since both marginal and posterior probability distributions require evaluation of the integral in eq. (1), they are intractable.

We could try to approximate eq. (1) by Monte-Carlo sampling as , , but since the volume of -space is potentially large, we would need millions of samples of to get a reliable estimate.

To train a probabilistic model, we can use a parametric distribution - parametrised by a neural network with parameters . We can now learn the parameters by maximum likelihood estimation,

The problem is, we cannot maximise an expression (eq. (1)), which we can’t even evaluate. To improve things, we can resort to importance sampling (IS). When we need to evaluate an expectation with respect to the original (nominal) probability density function (pdf), IS allows us to sample from a different probability distribution (proposal) and then weigh those samples with respect to the nominal pdf. Let be our proposal - a probability distribution parametrised by a neural network with parameters . We can write

From importance sampling literature we know that the optimal proposal is proportional to the nominal pdf times the function, whose expectation we are trying to approximate. In our setting, that function is just . From Bayes’ theorem, , we see that the optimal proposal is proportional to the posterior distribution, which is of course intractable.

Rise of a Variational Autoencoder

Fortunately, it turns out, we can kill two birds with one stone: by trying to approximate the posterior with a learned proposal, we can efficiently approximate the marginal probability . A bit by accident, we have just arrived at an autoencoding setup. To learn our model, we need

  • - the generative model, which consists of
    • - a probabilistic decoder, and
    • - a prior over the latent variables,
  • - a probabilistic encoder.

To approximate the posterior, we can use the KL-divergence (think of it as a distance between probability distributions) between the proposal and the posterior itself; and we can minimise it.

Our new problem is, of course, that to evaluate the KL we need to know the posterior distribution. Not all is lost, for doing a little algebra can give us an objective function that is possible to compute.

Where on the second line I expanded the logarithm, on the third line I used the Bayes’ theorem and the fact that is independent of . in the last line is a lower bound on the log probability of data - the so-called evidence-lower bound (ELBO). We can rewrite it as

We can approximate it using a single sample from the proposal distribution as

We train the model by finding and (usually by stochastic gradient descent) that maximise the ELBO:

By maximising the ELBO, we (1) maximise the marginal probability or (2) minimise the KL-divergence, or both. It is worth noting that the approximation of ELBO has the form of the log of importance-sampled expectation of , with importance weights .

What is wrong with this estimate?

If you look long enough at importance sampling, it becomes apparent that the support of the proposal distribution should be wider than that of the nominal pdf - both to avoid infinite variance of the estimator and numerical instabilities. In this case, it would be better to optimise the reverse , which has mode-averaging behaviour, as opposed to , which tries to match the mode of to one of the modes of . This would typically require taking samples from the true posterior, which is hard. Instead, we can use IS estimate of the ELBO, introduced as Importance Weighted Autoencoder (IWAE). The idea is simple: we take samples from the proposal and we use an average of probability ratios evaluated at those samples. We call each of the samples a particle.

This estimator has been shown to optimise the modified KL-divergence , with and defined as

While similar to the original distributions, and allow small variations in and that we would not have expected. Optimising this lower bound leads to better generative models, as shown in the original paper. It also leads to higher-entropy (wider, more scattered) estimates of the approximate posterior , effectively breaking the mode-matching behaviour of the original KL-divergence. As a curious consequence, if we increase the number of particles to infinity, we no longer need the inference model .

Posterior distribution of z for the IWAE (top row) and VAE (bottom row). Figure reproduced from the IWAE paper.

What is wrong with IWAE?

The importance-weighted ELBO, or the IWAE, generalises the original ELBO: for , we have . It is also true that . In other words, the more particles we use to estimate , the closer it gets in value to the true log probability of data - we say that the bound becomes tighter. This means that the gradient estimator, derived by differentiating the IWAE, points us in a better direction than the gradient of the original ELBO would. Additionally, as we increase , the variance of that gradient estimator shrinks.

It is great for the generative model, but, as we shown in our recent paper Tighter Variational Bounds are Not Necessarily Better, it turns out to be problematic for the proposal. The magnitude of the gradient with respect to proposal parameters goes to zero with increasing , and it does so much faster than its variance.

Let be a minibatch estimate of the gradient of an objective function we’re optimising (e.g. ELBO) with respect to . If we define signal-to-noise ratio (SNR) of the parameter update as

where and are expectation and variance, respectively, it turns out that SNR increases with for , but it decreases for . The conclusion here is simple: the more particles we use, the worse the inference model becomes. If we care about representation learning, we have a problem.

Better estimators

We can do better than the IWAE, as we’ve shown in our paper. The idea is to use separate objectives for the inference and the generative models. By doing so, we can ensure that both get non-zero low-variance gradients, which lead to better models.

Signal-to-Noise ratio for the encoder across training epochs
Signal-to-Noise ratio for the proposal across training epochs for different training objectives.

In the above plot, we compare SNR of the updates of parameters of the proposal acorss training epochs. VAE, which shows the highest SNR, is trained by optimising . IWAE, trained with , has the lowest SNR. The three curves in between use different combinations of for the generative model and or for the inference model. While not as good as the VAE under this metric, they all lead to training better proposals and generative models than either VAE or IWAE.

As a, perhaps surprising, side effect, models trained with our new estimators achieve higher bounds than the IWAE itself trained with this objective. Why? By looking at the effective sample-size (ESS) and the marginal log probability of data, it looks like optimising leads to producing the best quality proposals, but the worst generative models. If we combine a good proposal with an objective that leads to good generative models, we should be able to provide lower-variance estimate of this objective and thus learn even better models. Please see our paper for details.

Further Reading


I would like to thank Neil Dhir and Anton Troynikov for proofreading this post and suggestions on how to make it better.