Auto-Encoding Variational Bayes

Diederik P Kingma, Max Welling

Introduction

How can we perform efficient approximate inference and learning with directed probabilistic models whose continuous latent variables and/or parameters have intractable posterior distributions? The variational Bayesian (VB) approach involves the optimization of an approximation to the intractable posterior. Unfortunately, the common mean-field approach requires analytical solutions of expectations w.r.t. the approximate posterior, which are also intractable in the general case. We show how a reparameterization of the variational lower bound yields a simple differentiable unbiased estimator of the lower bound; this SGVB (Stochastic Gradient Variational Bayes) estimator can be used for efficient approximate posterior inference in almost any model with continuous latent variables and/or parameters, and is straightforward to optimize using standard stochastic gradient ascent techniques.

For the case of an i.i.d. dataset and continuous latent variables per datapoint, we propose the Auto-Encoding VB (AEVB) algorithm. In the AEVB algorithm we make inference and learning especially efficient by using the SGVB estimator to optimize a recognition model that allows us to perform very efficient approximate posterior inference using simple ancestral sampling, which in turn allows us to efficiently learn the model parameters, without the need of expensive iterative inference schemes (such as MCMC) per datapoint. The learned approximate posterior inference model can also be used for a host of tasks such as recognition, denoising, representation and visualization purposes. When a neural network is used for the recognition model, we arrive at the variational auto-encoder.

Method

The strategy in this section can be used to derive a lower bound estimator (a stochastic objective function) for a variety of directed graphical models with continuous latent variables. We will restrict ourselves here to the common case where we have an i.i.d. dataset with latent variables per datapoint, and where we like to perform maximum likelihood (ML) or maximum a posteriori (MAP) inference on the (global) parameters, and variational inference on the latent variables. It is, for example, straightforward to extend this scenario to the case where we also perform variational inference on the global parameters; that algorithm is put in the appendix, but experiments with that case are left to future work. Note that our method can be applied to online, non-stationary settings, e.g. streaming data, but here we assume a fixed dataset for simplicity.

Let us consider some dataset X={x(i)}i=1N\mathbf{X}=\{\mathbf{x}^{(i)}\}_{i=1}^{N} consisting of NN i.i.d. samples of some continuous or discrete variable x\mathbf{x}. We assume that the data are generated by some random process, involving an unobserved continuous random variable z\mathbf{z}. The process consists of two steps: (1) a value z(i)\mathbf{z}^{(i)} is generated from some prior distribution pθ(z)p_{\boldsymbol{\theta}^{*}}(\mathbf{z}); (2) a value x(i)\mathbf{x}^{(i)} is generated from some conditional distribution pθ(xz)p_{\boldsymbol{\theta}^{*}}(\mathbf{x}|\mathbf{z}). We assume that the prior pθ(z)p_{\boldsymbol{\theta}^{*}}(\mathbf{z}) and likelihood pθ(xz)p_{\boldsymbol{\theta}^{*}}(\mathbf{x}|\mathbf{z}) come from parametric families of distributions pθ(z)p_{\boldsymbol{\theta}}(\mathbf{z}) and pθ(xz)p_{\boldsymbol{\theta}}(\mathbf{x}|\mathbf{z}), and that their PDFs are differentiable almost everywhere w.r.t. both θ\boldsymbol{\theta} and z\mathbf{z}. Unfortunately, a lot of this process is hidden from our view: the true parameters θ\boldsymbol{\theta}^{*} as well as the values of the latent variables z(i)\mathbf{z}^{(i)} are unknown to us.

Very importantly, we do not make the common simplifying assumptions about the marginal or posterior probabilities. Conversely, we are here interested in a general algorithm that even works efficiently in the case of:

Intractability: the case where the integral of the marginal likelihood pθ(x)=pθ(z)pθ(xz)dzp_{\boldsymbol{\theta}}(\mathbf{x})=\int p_{\boldsymbol{\theta}}(\mathbf{z})p_{\boldsymbol{\theta}}(\mathbf{x}|\mathbf{z})\,d\mathbf{z} is intractable (so we cannot evaluate or differentiate the marginal likelihood), where the true posterior density pθ(zx)=pθ(xz)pθ(z)/pθ(x)p_{\boldsymbol{\theta}}(\mathbf{z}|\mathbf{x})=p_{\boldsymbol{\theta}}(\mathbf{x}|\mathbf{z})p_{\boldsymbol{\theta}}(\mathbf{z})/p_{\boldsymbol{\theta}}(\mathbf{x}) is intractable (so the EM algorithm cannot be used), and where the required integrals for any reasonable mean-field VB algorithm are also intractable. These intractabilities are quite common and appear in cases of moderately complicated likelihood functions pθ(xz)p_{\boldsymbol{\theta}}(\mathbf{x}|\mathbf{z}), e.g. a neural network with a nonlinear hidden layer.

A large dataset: we have so much data that batch optimization is too costly; we would like to make parameter updates using small minibatches or even single datapoints. Sampling-based solutions, e.g. Monte Carlo EM, would in general be too slow, since it involves a typically expensive sampling loop per datapoint.

We are interested in, and propose a solution to, three related problems in the above scenario:

Efficient approximate ML or MAP estimation for the parameters θ\boldsymbol{\theta}. The parameters can be of interest themselves, e.g. if we are analyzing some natural process. They also allow us to mimic the hidden random process and generate artificial data that resembles the real data.

Efficient approximate posterior inference of the latent variable z\mathbf{z} given an observed value x\mathbf{x} for a choice of parameters θ\boldsymbol{\theta}. This is useful for coding or data representation tasks.

Efficient approximate marginal inference of the variable x\mathbf{x}. This allows us to perform all kinds of inference tasks where a prior over x\mathbf{x} is required. Common applications in computer vision include image denoising, inpainting and super-resolution.

For the purpose of solving the above problems, let us introduce a recognition model qϕ(zx)q_{\boldsymbol{\phi}}(\mathbf{z}|\mathbf{x}): an approximation to the intractable true posterior pθ(zx)p_{\boldsymbol{\theta}}(\mathbf{z}|\mathbf{x}). Note that in contrast with the approximate posterior in mean-field variational inference, it is not necessarily factorial and its parameters ϕ\boldsymbol{\phi} are not computed from some closed-form expectation. Instead, we’ll introduce a method for learning the recognition model parameters ϕ\boldsymbol{\phi} jointly with the generative model parameters θ\boldsymbol{\theta}.

From a coding theory perspective, the unobserved variables z\mathbf{z} have an interpretation as a latent representation or code. In this paper we will therefore also refer to the recognition model qϕ(zx)q_{\boldsymbol{\phi}}(\mathbf{z}|\mathbf{x}) as a probabilistic encoder, since given a datapoint x\mathbf{x} it produces a distribution (e.g. a Gaussian) over the possible values of the code z\mathbf{z} from which the datapoint x\mathbf{x} could have been generated. In a similar vein we will refer to pθ(xz)p_{\boldsymbol{\theta}}(\mathbf{x}|\mathbf{z}) as a probabilistic decoder, since given a code z\mathbf{z} it produces a distribution over the possible corresponding values of x\mathbf{x}.

2 The variational bound

The marginal likelihood is composed of a sum over the marginal likelihoods of individual datapoints logpθ(x(1),,x(N))=i=1Nlogpθ(x(i))\log p_{\boldsymbol{\theta}}(\mathbf{x}^{(1)},\cdots,\mathbf{x}^{(N)})=\sum_{i=1}^{N}\log p_{\boldsymbol{\theta}}(\mathbf{x}^{(i)}), which can each be rewritten as:

The first RHS term is the KL divergence of the approximate from the true posterior. Since this KL-divergence is non-negative, the second RHS term L(θ,ϕ;x(i))\mathcal{L}(\boldsymbol{\theta},\boldsymbol{\phi};\mathbf{x}^{(i)}) is called the (variational) lower bound on the marginal likelihood of datapoint ii, and can be written as:

3 The SGVB estimator and AEVB algorithm

In this section we introduce a practical estimator of the lower bound and its derivatives w.r.t. the parameters. We assume an approximate posterior in the form qϕ(zx)q_{\boldsymbol{\phi}}(\mathbf{z}|\mathbf{x}), but please note that the technique can be applied to the case qϕ(z)q_{\boldsymbol{\phi}}(\mathbf{z}), i.e. where we do not condition on x\mathbf{x}, as well. The fully variational Bayesian method for inferring a posterior over the parameters is given in the appendix.

Under certain mild conditions outlined in section 2.4 for a chosen approximate posterior qϕ(zx)q_{\boldsymbol{\phi}}(\mathbf{z}|\mathbf{x}) we can reparameterize the random variable z~qϕ(zx)\widetilde{\mathbf{z}}\sim q_{\boldsymbol{\phi}}(\mathbf{z}|\mathbf{x}) using a differentiable transformation gϕ(ϵ,x)g_{\boldsymbol{\phi}}(\boldsymbol{\epsilon},\mathbf{x}) of an (auxiliary) noise variable ϵ\boldsymbol{\epsilon}:

See section 2.4 for general strategies for chosing such an approriate distribution p(ϵ)p(\boldsymbol{\epsilon}) and function gϕ(ϵ,x)g_{\boldsymbol{\phi}}(\boldsymbol{\epsilon},\mathbf{x}). We can now form Monte Carlo estimates of expectations of some function f(z)f(\mathbf{z}) w.r.t. qϕ(zx)q_{\boldsymbol{\phi}}(\mathbf{z}|\mathbf{x}) as follows:

We apply this technique to the variational lower bound (eq. (2)), yielding our generic Stochastic Gradient Variational Bayes (SGVB) estimator L~A(θ,ϕ;x(i))L(θ,ϕ;x(i))\widetilde{\mathcal{L}}^{A}(\boldsymbol{\theta},\boldsymbol{\phi};\mathbf{x}^{(i)})\simeq\mathcal{L}(\boldsymbol{\theta},\boldsymbol{\phi};\mathbf{x}^{(i)}):

Given multiple datapoints from a dataset X\mathbf{X} with NN datapoints, we can construct an estimator of the marginal likelihood lower bound of the full dataset, based on minibatches:

where the minibatch XM={x(i)}i=1M\mathbf{X}^{M}=\{\mathbf{x}^{(i)}\}_{i=1}^{M} is a randomly drawn sample of MM datapoints from the full dataset X\mathbf{X} with NN datapoints. In our experiments we found that the number of samples LL per datapoint can be set to 11 as long as the minibatch size MM was large enough, e.g. M=100M=100. Derivatives θ,ϕL~(θ;XM)\nabla_{\boldsymbol{\theta},\boldsymbol{\phi}}\widetilde{\mathcal{L}}(\boldsymbol{\theta};\mathbf{X}^{M}) can be taken, and the resulting gradients can be used in conjunction with stochastic optimization methods such as SGD or Adagrad [DHS10]. See algorithm 1 for a basic approach to compute the stochastic gradients.

A connection with auto-encoders becomes clear when looking at the objective function given at eq. (7). The first term is (the KL divergence of the approximate posterior from the prior) acts as a regularizer, while the second term is a an expected negative reconstruction error. The function gϕ(.)g_{\boldsymbol{\phi}}(.) is chosen such that it maps a datapoint x(i)\mathbf{x}^{(i)} and a random noise vector ϵ(l)\boldsymbol{\epsilon}^{(l)} to a sample from the approximate posterior for that datapoint: z(i,l)=gϕ(ϵ(l),x(i))\mathbf{z}^{(i,l)}=g_{\boldsymbol{\phi}}(\boldsymbol{\epsilon}^{(l)},\mathbf{x}^{(i)}) where z(i,l)qϕ(zx(i))\mathbf{z}^{(i,l)}\sim q_{\boldsymbol{\phi}}(\mathbf{z}|\mathbf{x}^{(i)}). Subsequently, the sample z(i,l)\mathbf{z}^{(i,l)} is then input to function logpθ(x(i)z(i,l))\log p_{\boldsymbol{\theta}}(\mathbf{x}^{(i)}|\mathbf{z}^{(i,l)}), which equals the probability density (or mass) of datapoint x(i)\mathbf{x}^{(i)} under the generative model, given z(i,l)\mathbf{z}^{(i,l)}. This term is a negative reconstruction error in auto-encoder parlance.

4 The reparameterization trick

In order to solve our problem we invoked an alternative method for generating samples from qϕ(zx)q_{\boldsymbol{\phi}}(\mathbf{z}|\mathbf{x}). The essential parameterization trick is quite simple. Let z\mathbf{z} be a continuous random variable, and zqϕ(zx)\mathbf{z}\sim q_{\boldsymbol{\phi}}(\mathbf{z}|\mathbf{x}) be some conditional distribution. It is then often possible to express the random variable z\mathbf{z} as a deterministic variable z=gϕ(ϵ,x)\mathbf{z}=g_{\boldsymbol{\phi}}(\boldsymbol{\epsilon},\mathbf{x}), where ϵ\boldsymbol{\epsilon} is an auxiliary variable with independent marginal p(ϵ)p(\boldsymbol{\epsilon}), and gϕ(.)g_{\boldsymbol{\phi}}(.) is some vector-valued function parameterized by ϕ\boldsymbol{\phi}.

This reparameterization is useful for our case since it can be used to rewrite an expectation w.r.t qϕ(zx)q_{\boldsymbol{\phi}}(\mathbf{z}|\mathbf{x}) such that the Monte Carlo estimate of the expectation is differentiable w.r.t. ϕ\boldsymbol{\phi}. A proof is as follows. Given the deterministic mapping z=gϕ(ϵ,x)\mathbf{z}=g_{\boldsymbol{\phi}}(\boldsymbol{\epsilon},\mathbf{x}) we know that qϕ(zx)idzi=p(ϵ)idϵiq_{\boldsymbol{\phi}}(\mathbf{z}|\mathbf{x})\prod_{i}dz_{i}=p(\boldsymbol{\epsilon})\prod_{i}d\epsilon_{i}. ThereforeNote that for infinitesimals we use the notational convention dz=idzid\mathbf{z}=\prod_{i}dz_{i}, qϕ(zx)f(z)dz=p(ϵ)f(z)dϵ=p(ϵ)f(gϕ(ϵ,x))dϵ\int q_{\boldsymbol{\phi}}(\mathbf{z}|\mathbf{x})f(\mathbf{z})\,d\mathbf{z}=\int p(\boldsymbol{\epsilon})f(\mathbf{z})\,d\boldsymbol{\epsilon}=\int p(\boldsymbol{\epsilon})f(g_{\boldsymbol{\phi}}(\boldsymbol{\epsilon},\mathbf{x}))\,d\boldsymbol{\epsilon}. It follows that a differentiable estimator can be constructed: qϕ(zx)f(z)dz1Ll=1Lf(gϕ(x,ϵ(l)))\int q_{\boldsymbol{\phi}}(\mathbf{z}|\mathbf{x})f(\mathbf{z})\,d\mathbf{z}\simeq\frac{1}{L}\sum_{l=1}^{L}f(g_{\boldsymbol{\phi}}(\mathbf{x},\boldsymbol{\epsilon}^{(l)})) where ϵ(l)p(ϵ)\boldsymbol{\epsilon}^{(l)}\sim p(\boldsymbol{\epsilon}). In section 2.3 we applied this trick to obtain a differentiable estimator of the variational lower bound.

For which qϕ(zx)q_{\boldsymbol{\phi}}(\mathbf{z}|\mathbf{x}) can we choose such a differentiable transformation gϕ(.)g_{\boldsymbol{\phi}}(.) and auxiliary variable ϵp(ϵ)\boldsymbol{\epsilon}\sim p(\boldsymbol{\epsilon})? Three basic approaches are:

Tractable inverse CDF. In this case, let ϵU(0,I)\boldsymbol{\epsilon}\sim\mathcal{U}(\mathbf{0},\mathbf{I}), and let gϕ(ϵ,x)g_{\boldsymbol{\phi}}(\boldsymbol{\epsilon},\mathbf{x}) be the inverse CDF of qϕ(zx)q_{\boldsymbol{\phi}}(\mathbf{z}|\mathbf{x}). Examples: Exponential, Cauchy, Logistic, Rayleigh, Pareto, Weibull, Reciprocal, Gompertz, Gumbel and Erlang distributions.

Analogous to the Gaussian example, for any ”location-scale” family of distributions we can choose the standard distribution (with location=0\text{location}=0, scale=1\text{scale}=1) as the auxiliary variable ϵ\boldsymbol{\epsilon}, and let g(.)=location+scaleϵg(.)=\text{location}+\text{scale}\cdot\boldsymbol{\epsilon}. Examples: Laplace, Elliptical, Student’s t, Logistic, Uniform, Triangular and Gaussian distributions.

Composition: It is often possible to express random variables as different transformations of auxiliary variables. Examples: Log-Normal (exponentiation of normally distributed variable), Gamma (a sum over exponentially distributed variables), Dirichlet (weighted sum of Gamma variates), Beta, Chi-Squared, and F distributions.

When all three approaches fail, good approximations to the inverse CDF exist requiring computations with time complexity comparable to the PDF (see e.g. [Dev86] for some methods).

Example: Variational Auto-Encoder

In this section we’ll give an example where we use a neural network for the probabilistic encoder qϕ(zx)q_{\boldsymbol{\phi}}(\mathbf{z}|\mathbf{x}) (the approximation to the posterior of the generative model pθ(x,z)p_{\boldsymbol{\theta}}(\mathbf{x},\mathbf{z})) and where the parameters ϕ\boldsymbol{\phi} and θ\boldsymbol{\theta} are optimized jointly with the AEVB algorithm.

Let the prior over the latent variables be the centered isotropic multivariate Gaussian pθ(z)=N(z;0,I)p_{\boldsymbol{\theta}}(\mathbf{z})=\mathcal{N}(\mathbf{z};\mathbf{0},\mathbf{I}). Note that in this case, the prior lacks parameters. We let pθ(xz)p_{\boldsymbol{\theta}}(\mathbf{x}|\mathbf{z}) be a multivariate Gaussian (in case of real-valued data) or Bernoulli (in case of binary data) whose distribution parameters are computed from z\mathbf{z} with a MLP (a fully-connected neural network with a single hidden layer, see appendix C). Note the true posterior pθ(zx)p_{\boldsymbol{\theta}}(\mathbf{z}|\mathbf{x}) is in this case intractable. While there is much freedom in the form qϕ(zx)q_{\boldsymbol{\phi}}(\mathbf{z}|\mathbf{x}), we’ll assume the true (but intractable) posterior takes on a approximate Gaussian form with an approximately diagonal covariance. In this case, we can let the variational approximate posterior be a multivariate Gaussian with a diagonal covariance structureNote that this is just a (simplifying) choice, and not a limitation of our method.:

where the mean and s.d. of the approximate posterior, μ(i)\boldsymbol{\mu}^{(i)} and σ(i)\boldsymbol{\sigma}^{(i)}, are outputs of the encoding MLP, i.e. nonlinear functions of datapoint x(i)\mathbf{x}^{(i)} and the variational parameters ϕ\boldsymbol{\phi} (see appendix C).

As explained in section 2.4, we sample from the posterior z(i,l)qϕ(zx(i))\mathbf{z}^{(i,l)}\sim q_{\boldsymbol{\phi}}(\mathbf{z}|\mathbf{x}^{(i)}) using z(i,l)=gϕ(x(i),ϵ(l))=μ(i)+σ(i)ϵ(l)\mathbf{z}^{(i,l)}=g_{\boldsymbol{\phi}}(\mathbf{x}^{(i)},\boldsymbol{\epsilon}^{(l)})=\boldsymbol{\mu}^{(i)}+\boldsymbol{\sigma}^{(i)}\odot\boldsymbol{\epsilon}^{(l)} where ϵ(l)N(0,I)\boldsymbol{\epsilon}^{(l)}\sim\mathcal{N}(\mathbf{0},\mathbf{I}). With \odot we signify an element-wise product. In this model both pθ(z)p_{\boldsymbol{\theta}}(\mathbf{z}) (the prior) and qϕ(zx)q_{\boldsymbol{\phi}}(\mathbf{z}|\mathbf{x}) are Gaussian; in this case, we can use the estimator of eq. (7) where the KL divergence can be computed and differentiated without estimation (see appendix B). The resulting estimator for this model and datapoint x(i)\mathbf{x}^{(i)} is:

As explained above and in appendix C, the decoding term logpθ(x(i)z(i,l))\log p_{\boldsymbol{\theta}}(\mathbf{x}^{(i)}|\mathbf{z}^{(i,l)}) is a Bernoulli or Gaussian MLP, depending on the type of data we are modelling.

Related work

The wake-sleep algorithm [HDFN95] is, to the best of our knowledge, the only other on-line learning method in the literature that is applicable to the same general class of continuous latent variable models. Like our method, the wake-sleep algorithm employs a recognition model that approximates the true posterior. A drawback of the wake-sleep algorithm is that it requires a concurrent optimization of two objective functions, which together do not correspond to optimization of (a bound of) the marginal likelihood. An advantage of wake-sleep is that it also applies to models with discrete latent variables. Wake-Sleep has the same computational complexity as AEVB per datapoint.

Stochastic variational inference [HBWP13] has recently received increasing interest. Recently, [BJP12] introduced a control variate schemes to reduce the high variance of the naïve gradient estimator discussed in section 2.1, and applied to exponential family approximations of the posterior. In [RGB13] some general methods, i.e. a control variate scheme, were introduced for reducing the variance of the original gradient estimator. In [SK13], a similar reparameterization as in this paper was used in an efficient version of a stochastic variational inference algorithm for learning the natural parameters of exponential-family approximating distributions.

The AEVB algorithm exposes a connection between directed probabilistic models (trained with a variational objective) and auto-encoders. A connection between linear auto-encoders and a certain class of generative linear-Gaussian models has long been known. In [Row98] it was shown that PCA corresponds to the maximum-likelihood (ML) solution of a special case of the linear-Gaussian model with a prior p(z)=N(0,I)p(\mathbf{z})=\mathcal{N}(0,\mathbf{I}) and a conditional distribution p(xz)=N(x;Wz,ϵI)p(\mathbf{x}|\mathbf{z})=\mathcal{N}(\mathbf{x};\mathbf{W}\mathbf{z},\epsilon\mathbf{I}), specifically the case with infinitesimally small ϵ\epsilon.

In relevant recent work on autoencoders [VLL+10] it was shown that the training criterion of unregularized autoencoders corresponds to maximization of a lower bound (see the infomax principle [Lin89]) of the mutual information between input XX and latent representation ZZ. Maximizing (w.r.t. parameters) of the mutual information is equivalent to maximizing the conditional entropy, which is lower bounded by the expected loglikelihood of the data under the autoencoding model [VLL+10], i.e. the negative reconstrution error. However, it is well known that this reconstruction criterion is in itself not sufficient for learning useful representations [BCV13]. Regularization techniques have been proposed to make autoencoders learn useful representations, such as denoising, contractive and sparse autoencoder variants [BCV13]. The SGVB objective contains a regularization term dictated by the variational bound (e.g. eq. (10)), lacking the usual nuisance regularization hyperparameter required to learn useful representations. Related are also encoder-decoder architectures such as the predictive sparse decomposition (PSD) [KRL08], from which we drew some inspiration. Also relevant are the recently introduced Generative Stochastic Networks [BTL13] where noisy auto-encoders learn the transition operator of a Markov chain that samples from the data distribution. In [SL10] a recognition model was employed for efficient learning with Deep Boltzmann Machines. These methods are targeted at either unnormalized models (i.e. undirected models like Boltzmann machines) or limited to sparse coding models, in contrast to our proposed algorithm for learning a general class of directed probabilistic models.

The recently proposed DARN method [GMW13], also learns a directed probabilistic model using an auto-encoding structure, however their method applies to binary latent variables. Even more recently, [RMW14] also make the connection between auto-encoders, directed proabilistic models and stochastic variational inference using the reparameterization trick we describe in this paper. Their work was developed independently of ours and provides an additional perspective on AEVB.

Experiments

We trained generative models of images from the MNIST and Frey Face datasetsAvailable at http://www.cs.nyu.edu/~roweis/data.html and compared learning algorithms in terms of the variational lower bound, and the estimated marginal likelihood.

The generative model (encoder) and variational approximation (decoder) from section 3 were used, where the described encoder and decoder have an equal number of hidden units. Since the Frey Face data are continuous, we used a decoder with Gaussian outputs, identical to the encoder, except that the means were constrained to the interval (0,1)(0,1) using a sigmoidal activation function at the decoder output. Note that with hidden units we refer to the hidden layer of the neural networks of the encoder and decoder.

Parameters are updated using stochastic gradient ascent where gradients are computed by differentiating the lower bound estimator θ,ϕL(θ,ϕ;X)\nabla_{\boldsymbol{\theta},\boldsymbol{\phi}}\mathcal{L}(\boldsymbol{\theta},\boldsymbol{\phi};\mathbf{X}) (see algorithm 1), plus a small weight decay term corresponding to a prior p(θ)=N(0,I)p(\boldsymbol{\theta})=\mathcal{N}(0,\mathbf{I}). Optimization of this objective is equivalent to approximate MAP estimation, where the likelihood gradient is approximated by the gradient of the lower bound.

We compared performance of AEVB to the wake-sleep algorithm [HDFN95]. We employed the same encoder (also called recognition model) for the wake-sleep algorithm and the variational auto-encoder. All parameters, both variational and generative, were initialized by random sampling from N(0,0.01)\mathcal{N}(0,0.01), and were jointly stochastically optimized using the MAP criterion. Stepsizes were adapted with Adagrad [DHS10]; the Adagrad global stepsize parameters were chosen from {0.01, 0.02, 0.1} based on performance on the training set in the first few iterations. Minibatches of size M=100M=100 were used, with L=1L=1 samples per datapoint.

We trained generative models (decoders) and corresponding encoders (a.k.a. recognition models) having 500500 hidden units in case of MNIST, and 200200 hidden units in case of the Frey Face dataset (to prevent overfitting, since it is a considerably smaller dataset). The chosen number of hidden units is based on prior literature on auto-encoders, and the relative performance of different algorithms was not very sensitive to these choices. Figure 2 shows the results when comparing the lower bounds. Interestingly, superfluous latent variables did not result in overfitting, which is explained by the regularizing nature of the variational bound.

Marginal likelihood

For very low-dimensional latent space it is possible to estimate the marginal likelihood of the learned generative models using an MCMC estimator. More information about the marginal likelihood estimator is available in the appendix. For the encoder and decoder we again used neural networks, this time with 100 hidden units, and 3 latent variables; for higher dimensional latent space the estimates became unreliable. Again, the MNIST dataset was used. The AEVB and Wake-Sleep methods were compared to Monte Carlo EM (MCEM) with a Hybrid Monte Carlo (HMC) [DKPR87] sampler; details are in the appendix. We compared the convergence speed for the three algorithms, for a small and large training set size. Results are in figure 3.

Visualisation of high-dimensional data

If we choose a low-dimensional latent space (e.g. 2D), we can use the learned encoders (recognition model) to project high-dimensional data to a low-dimensional manifold. See appendix A for visualisations of the 2D latent manifolds for the MNIST and Frey Face datasets.

Conclusion

We have introduced a novel estimator of the variational lower bound, Stochastic Gradient VB (SGVB), for efficient approximate inference with continuous latent variables. The proposed estimator can be straightforwardly differentiated and optimized using standard stochastic gradient methods. For the case of i.i.d. datasets and continuous latent variables per datapoint we introduce an efficient algorithm for efficient inference and learning, Auto-Encoding VB (AEVB), that learns an approximate inference model using the SGVB estimator. The theoretical advantages are reflected in experimental results.

Future work

Since the SGVB estimator and the AEVB algorithm can be applied to almost any inference and learning problem with continuous latent variables, there are plenty of future directions: (i) learning hierarchical generative architectures with deep neural networks (e.g. convolutional networks) used for the encoders and decoders, trained jointly with AEVB; (ii) time-series models (i.e. dynamic Bayesian networks); (iii) application of SGVB to the global parameters; (iv) supervised models with latent variables, useful for learning complicated noise distributions.

References

Appendix A Visualisations

See figures 4 and 5 for visualisations of latent space and corresponding observed space of models learned with SGVB.

The variational lower bound (the objective to be maximized) contains a KL term that can often be integrated analytically. Here we give the solution when both the prior pθ(z)=N(0,I)p_{\boldsymbol{\theta}}(\mathbf{z})=\mathcal{N}(0,\mathbf{I}) and the posterior approximation qϕ(zx(i))q_{\boldsymbol{\phi}}(\mathbf{z}|\mathbf{x}^{(i)}) are Gaussian. Let JJ be the dimensionality of z\mathbf{z}. Let μ\boldsymbol{\mu} and σ\boldsymbol{\sigma} denote the variational mean and s.d. evaluated at datapoint ii, and let μj\mu_{j} and σj\sigma_{j} simply denote the jj-th element of these vectors. Then:

When using a recognition model qϕ(zx)q_{\boldsymbol{\phi}}(\mathbf{z}|\mathbf{x}) then μ\boldsymbol{\mu} and s.d. σ\boldsymbol{\sigma} are simply functions of x\mathbf{x} and the variational parameters ϕ\boldsymbol{\phi}, as exemplified in the text.

Appendix C MLP’s as probabilistic encoders and decoders

In variational auto-encoders, neural networks are used as probabilistic encoders and decoders. There are many possible choices of encoders and decoders, depending on the type of data and model. In our example we used relatively simple neural networks, namely multi-layered perceptrons (MLPs). For the encoder we used a MLP with Gaussian output, while for the decoder we used MLPs with either Gaussian or Bernoulli outputs, depending on the type of data.

In this case let pθ(xz)p_{\boldsymbol{\theta}}(\mathbf{x}|\mathbf{z}) be a multivariate Bernoulli whose probabilities are computed from z\mathbf{z} with a fully-connected neural network with a single hidden layer:

where fσ(.)f_{\sigma}(.) is the elementwise sigmoid activation function, and where θ={W1,W2,b1,b2}\boldsymbol{\theta}=\{\mathbf{W}_{1},\mathbf{W}_{2},\mathbf{b}_{1},\mathbf{b}_{2}\} are the weights and biases of the MLP.

C.2 Gaussian MLP as encoder or decoder

In this case let encoder or decoder be a multivariate Gaussian with a diagonal covariance structure:

where {W3,W4,W5,b3,b4,b5}\{\mathbf{W}_{3},\mathbf{W}_{4},\mathbf{W}_{5},\mathbf{b}_{3},\mathbf{b}_{4},\mathbf{b}_{5}\} are the weights and biases of the MLP and part of θ\boldsymbol{\theta} when used as decoder. Note that when this network is used as an encoder qϕ(zx)q_{\boldsymbol{\phi}}(\mathbf{z}|\mathbf{x}), then z\mathbf{z} and x\mathbf{x} are swapped, and the weights and biases are variational parameters ϕ\boldsymbol{\phi}.

Appendix D Marginal likelihood estimator

We derived the following marginal likelihood estimator that produces good estimates of the marginal likelihood as long as the dimensionality of the sampled space is low (less then 5 dimensions), and sufficient samples are taken. Let pθ(x,z)=pθ(z)pθ(xz)p_{\boldsymbol{\theta}}(\mathbf{x},\mathbf{z})=p_{\boldsymbol{\theta}}(\mathbf{z})p_{\boldsymbol{\theta}}(\mathbf{x}|\mathbf{z}) be the generative model we are sampling from, and for a given datapoint x(i)\mathbf{x}^{(i)} we would like to estimate the marginal likelihood pθ(x(i))p_{\boldsymbol{\theta}}(\mathbf{x}^{(i)}).

The estimation process consists of three stages:

Sample LL values {z(l)}\{\mathbf{z}^{(l)}\} from the posterior using gradient-based MCMC, e.g. Hybrid Monte Carlo, using zlogpθ(zx)=zlogpθ(z)+zlogpθ(xz)\nabla_{\mathbf{z}}\log p_{\boldsymbol{\theta}}(\mathbf{z}|\mathbf{x})=\nabla_{\mathbf{z}}\log p_{\boldsymbol{\theta}}(\mathbf{z})+\nabla_{\mathbf{z}}\log p_{\boldsymbol{\theta}}(\mathbf{x}|\mathbf{z}).

Fit a density estimator q(z)q(\mathbf{z}) to these samples {z(l)}\{\mathbf{z}^{(l)}\}.

Again, sample LL new values from the posterior. Plug these samples, as well as the fitted q(z)q(\mathbf{z}), into the following estimator:

Appendix E Monte Carlo EM

The Monte Carlo EM algorithm does not employ an encoder, instead it samples from the posterior of the latent variables using gradients of the posterior computed with zlogpθ(zx)=zlogpθ(z)+zlogpθ(xz)\nabla_{\mathbf{z}}\log p_{\boldsymbol{\theta}}(\mathbf{z}|\mathbf{x})=\nabla_{\mathbf{z}}\log p_{\boldsymbol{\theta}}(\mathbf{z})+\nabla_{\mathbf{z}}\log p_{\boldsymbol{\theta}}(\mathbf{x}|\mathbf{z}). The Monte Carlo EM procedure consists of 10 HMC leapfrog steps with an automatically tuned stepsize such that the acceptance rate was 90%, followed by 5 weight updates steps using the acquired sample. For all algorithms the parameters were updated using the Adagrad stepsizes (with accompanying annealing schedule).

The marginal likelihood was estimated with the first 1000 datapoints from the train and test sets, for each datapoint sampling 50 values from the posterior of the latent variables using Hybrid Monte Carlo with 4 leapfrog steps.

Appendix F Full VB

As written in the paper, it is possible to perform variational inference on both the parameters θ\boldsymbol{\theta} and the latent variables z\mathbf{z}, as opposed to just the latent variables as we did in the paper. Here, we’ll derive our estimator for that case.

Let pα(θ)p_{\boldsymbol{\alpha}}(\boldsymbol{\theta}) be some hyperprior for the parameters introduced above, parameterized by α\boldsymbol{\alpha}. The marginal likelihood can be written as:

where the first RHS term denotes a KL divergence of the approximate from the true posterior, and where L(ϕ;X)\mathcal{L}(\boldsymbol{\phi};\mathbf{X}) denotes the variational lower bound to the marginal likelihood:

Note that this is a lower bound since the KL divergence is non-negative; the bound equals the true marginal when the approximate and true posteriors match exactly. The term logpθ(X)\log p_{\boldsymbol{\theta}}(\mathbf{X}) is composed of a sum over the marginal likelihoods of individual datapoints logpθ(X)=i=1Nlogpθ(x(i))\log p_{\boldsymbol{\theta}}(\mathbf{X})=\sum_{i=1}^{N}\log p_{\boldsymbol{\theta}}(\mathbf{x}^{(i)}), which can each be rewritten as:

where again the first RHS term is the KL divergence of the approximate from the true posterior, and L(θ,ϕ;x)\mathcal{L}(\boldsymbol{\theta},\boldsymbol{\phi};\mathbf{x}) is the variational lower bound of the marginal likelihood of datapoint ii:

The expectations on the RHS of eqs (14) and (16) can obviously be written as a sum of three separate expectations, of which the second and third component can sometimes be analytically solved, e.g. when both pθ(x)p_{\boldsymbol{\theta}}(\mathbf{x}) and qϕ(zx)q_{\boldsymbol{\phi}}(\mathbf{z}|\mathbf{x}) are Gaussian. For generality we will here assume that each of these expectations is intractable.

Under certain mild conditions outlined in section (see paper) for chosen approximate posteriors qϕ(θ)q_{\boldsymbol{\phi}}(\boldsymbol{\theta}) and qϕ(zx)q_{\boldsymbol{\phi}}(\mathbf{z}|\mathbf{x}) we can reparameterize conditional samples z~qϕ(zx)\widetilde{\mathbf{z}}\sim q_{\boldsymbol{\phi}}(\mathbf{z}|\mathbf{x}) as

where we choose a prior p(ϵ)p(\boldsymbol{\epsilon}) and a function gϕ(ϵ,x)g_{\boldsymbol{\phi}}(\boldsymbol{\epsilon},\mathbf{x}) such that the following holds:

The same can be done for the approximate posterior qϕ(θ)q_{\boldsymbol{\phi}}(\boldsymbol{\theta}):

where we, similarly as above, choose a prior p(ζ)p(\boldsymbol{\zeta}) and a function hϕ(ζ)h_{\boldsymbol{\phi}}(\boldsymbol{\zeta}) such that the following holds:

For notational conciseness we introduce a shorthand notation fϕ(x,z,θ)f_{\boldsymbol{\phi}}(\mathbf{x},\mathbf{z},\boldsymbol{\theta}):

Using equations (20) and (18), the Monte Carlo estimate of the variational lower bound, given datapoint x(i)\mathbf{x}^{(i)}, is:

where ϵ(l)p(ϵ)\boldsymbol{\epsilon}^{(l)}\sim p(\boldsymbol{\epsilon}) and ζ(l)p(ζ)\zeta^{(l)}\sim p(\boldsymbol{\zeta}). The estimator only depends on samples from p(ϵ)p(\boldsymbol{\epsilon}) and p(ζ)p(\boldsymbol{\zeta}) which are obviously not influenced by ϕ\boldsymbol{\phi}, therefore the estimator can be differentiated w.r.t. ϕ\boldsymbol{\phi}. The resulting stochastic gradients can be used in conjunction with stochastic optimization methods such as SGD or Adagrad [DHS10]. See algorithm 1 for a basic approach to computing stochastic gradients.

Let the prior over the parameters and latent variables be the centered isotropic Gaussian pα(θ)=N(z;0,I)p_{\boldsymbol{\alpha}}(\boldsymbol{\theta})=\mathcal{N}(\mathbf{z};\mathbf{0},\mathbf{I}) and pθ(z)=N(z;0,I)p_{\boldsymbol{\theta}}(\mathbf{z})=\mathcal{N}(\mathbf{z};\mathbf{0},\mathbf{I}). Note that in this case, the prior lacks parameters. Let’s also assume that the true posteriors are approximatily Gaussian with an approximately diagonal covariance. In this case, we can let the variational approximate posteriors be multivariate Gaussians with a diagonal covariance structure:

where μz\boldsymbol{\mu}_{\mathbf{z}} and σz\boldsymbol{\sigma}_{\mathbf{z}} are yet unspecified functions of x\mathbf{x}. Since they are Gaussian, we can parameterize the variational approximate posteriors:

With \odot we signify an element-wise product. These can be plugged into the lower bound defined above (eqs (21) and (22)).

In this case it is possible to construct an alternative estimator with a lower variance, since in this model pα(θ)p_{\boldsymbol{\alpha}}(\boldsymbol{\theta}), pθ(z)p_{\boldsymbol{\theta}}(\mathbf{z}), qϕ(θ)q_{\boldsymbol{\phi}}(\boldsymbol{\theta}) and qϕ(zx)q_{\boldsymbol{\phi}}(\mathbf{z}|\mathbf{x}) are Gaussian, and therefore four terms of fϕf_{\boldsymbol{\phi}} can be solved analytically. The resulting estimator is:

μj(i)\mu_{j}^{(i)} and σj(i)\sigma_{j}^{(i)} simply denote the jj-th element of vectors μ(i)\boldsymbol{\mu}^{(i)} and σ(i)\boldsymbol{\sigma}^{(i)}.