Black-box variational inference and the VAE
How to get the gradient of the ELBO for any model that can only evaluate $\log p(x, z)$: the score-function estimator and its variance, Rao–Blackwellization and baselines, the reparameterization gradient, amortized inference and the VAE as its special case, and a short comparison with GANs.
When conjugacy is gone
The topic Stochastic variational inference made variational inference fast on large data, but it kept one requirement from the start: every complete conditional must lie in the exponential family, so that each update and each natural gradient has a closed form. Many models we care about break that.
Take Bayesian logistic regression with one weight, as in the topic The Laplace approximation and Bayesian classification. The prior is , the labels are , and with the sigmoid . Choose . The ELBO contains
the expectation of a log-sigmoid under a Gaussian. It has no closed form. Coordinate ascent has no formula to iterate, and SVI has no natural gradient to step along. The classic way out is a model-specific trick: the Laplace approximation, or a hand-made bound on the sigmoid.
This topic asks for something more general. Can we get the gradient of the ELBO for any model, using only the ability to evaluate at a point? This is black-box variational inference (BBVI; Ranganath, Gerrish and Blei, 2014). The idea fits in one line: the ELBO is an expectation under , so its gradient can be written as an expectation too, and expectations can be estimated by Monte Carlo (the topic Monte Carlo and Markov chains).
The score-function estimator
Let be the variational family with parameters ; for a Gaussian, . Start with a plain expectation of a function that does not depend on . Its gradient cannot simply be pushed inside as : sits in the distribution, not in .
The way around is the log-derivative identity. Since ,
Move the gradient inside the integral and apply it:
The vector is the score of . It has mean zero: .
The ELBO is with , and here does depend on . The product rule gives one more term, , so it disappears. Replacing the expectation with an average over samples gives the score-function estimator:
Reinforcement learning knows the same estimator as REINFORCE (Williams, 1992); statistics calls it the likelihood-ratio method. The model enters only through values of , so it really can be a black box.
One condition hides in "move the gradient inside the integral". It needs the integrand to be dominated by an integrable bound, and it fails when the support of moves with . For the derivative of the integral picks up a boundary term that no score can produce.
What unbiasedness costs
The estimator is unbiased. The question is how noisy it is. Take the smallest possible example: and , so and the exact gradient is .
Write with . The score in is , so one sample gives
For the variance we need the moments of the standard normal: odd moments vanish, , . Then and
At the variance is for a gradient equal to . The noise grows with even though the gradient does not. The reason is visible in the formula: , and the score is multiplied by the value of , including its large constant part . That part contributes nothing on average, since , but it contributes to the variance.
The same happens in the ELBO. is a sum over all data points and is usually a large negative number, so a plain score-function estimate is very noisy. Ranganath et al. needed two variance-reduction tools, and the next block introduces them.
Rao–Blackwellization and baselines
Rao–Blackwellization. Let be mean-field, , and let be the score of factor . For any function of the other variables, independence under gives
So every term of that does not involve can be dropped from the gradient in . What remains are the factors of that contain , which depend only on its Markov blanket, and . Dropping a term whose expectation is known exactly (here, zero) is a Rao–Blackwellization: the mean is unchanged, and the variance only falls. In a model with many local variables, the gradient for one of them stops paying for the noise of all the others.
Control variates. Take any with a known mean and use
It is unbiased for every , and its variance is a parabola in with the minimum at
BBVI takes , the score itself, whose mean is zero. Then : subtract a constant baseline from before multiplying by the score. In the toy example and , so , and has variance at every . The baseline has removed exactly the constant part that was costing . In practice is estimated from a few extra samples.
The reparameterization gradient
There is a second, quite different estimator. Suppose a sample from can be written as a differentiable function of and of noise whose distribution does not depend on . For a Gaussian,
This is the reparameterization trick from the topic The reparameterization trick of the course Generative Models; there it trains an encoder, here it is a general estimator. Now lives inside the integrand, and the gradient goes through the sample:
For the ELBO with a Gaussian , the entropy of is plus a constant, so
The estimator now uses the derivative of the model, not just its values, and that is a lot more information per sample. In the toy example with the reparameterized gradient is for every sample: variance zero against . For at , (exact gradient ), the single-sample variances are:
| Estimator | Variance |
|---|---|
| score function | |
| score function with the optimal baseline | |
| reparameterization, |
The reparameterization gradient asks for more in return:
- must be differentiable in ;
- must be a differentiable transform of fixed noise: location–scale families, inverse CDFs, compositions such as of a Gaussian for a log-normal.
The score function needs neither. It works with discrete latent variables, with non-differentiable likelihoods, and with simulators that only return values. The two estimators complement each other.
Amortized inference and the VAE
In models with local variables, BBVI keeps one pair per data point, as SVI did. Each pair is optimized on its own, and a new point needs a fresh optimization.
Amortized inference replaces the table of local parameters with a function. An encoder network with weights maps any to its parameters, , and the ELBO is maximized over . Inference for a new point becomes one forward pass.
Now let the likelihood be a neural network too, a decoder with the prior . The per-point ELBO is
This is the variational autoencoder, and it is BBVI with four specific choices:
- a mean-field Gaussian and the reparameterization gradient, one sample per point;
- the encoder for the local parameters (amortization);
- mini-batches, so the estimate is doubly stochastic: random in the batch and in , still unbiased;
- the decoder weights fitted by the same ELBO in the same gradient steps, like an M-step of EM.
The KL between two Gaussians is computed in closed form rather than sampled: compute exactly what can be computed, the same move as Rao–Blackwellization. The derivation of this ELBO and a full training step are in the topics Latent variables and the ELBO and The reparameterization trick of the course Generative Models; we do not repeat them here.
Amortization has a price. The encoder must serve all points at once, so its ELBO cannot exceed what per-point optimization would reach. Cremer, Li and Duvenaud (2018) split the gap into an approximation gap (the family of is too poor) and an amortization gap (the encoder is not the best in that family); the second is often the larger one.
VAE, GAN and what comes next
Two warnings keep the VAE in its place within this course. First, there are two different KLs. The gap between and the ELBO is , to the true posterior; the loss contains , to the prior. Second, a VAE is Bayesian only about . The weights and are point estimates, found by gradient ascent.
A GAN (see the topic GANs as a two-player game in Generative Models) also generates from noise , but trains it against a discriminator :
| VAE | GAN | |
|---|---|---|
| Training | maximize the ELBO | adversarial game |
| Encoder | yes, | none in the basic model |
| Density of | defined, is bounded by the ELBO | implicit, no formula |
| Typical trouble | posterior approximation too crude, blurry samples with some likelihoods | unstable game, mode collapse |
Blur and mode collapse are typical problems of the basic models, not theorems about every member of each family.
BBVI turns posterior inference for any differentiable model into stochastic optimization, and the VAE applies it with a neural network as the likelihood. What it leaves untouched are the network weights themselves. The extension topic Bayesian deep learning makes the weights the latent variable: Bayes by backprop is the reparameterization gradient applied to over millions of weights, and it is compared there with cheaper approximations — MC dropout, deep ensembles and the Laplace approximation.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.