Importance Weighted Autoencoders
- Publication
- ICLR 2016Conference
- First submitted to arXiv
- arXiv:1509.00519
Cite this paper · BibTeX
The citation identifies the linked source version. It cites the original paper and its authors.
@misc{imagegenerationiwae2015,
title = {{Importance Weighted Autoencoders}},
author = {Yuri Burda and Roger Grosse and Ruslan Salakhutdinov},
year = {2015},
eprint = {1509.00519},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/1509.00519}
}
Suppose that for every image, the encoder draws five latent variables, and only one of them lands where the true posterior probability is high, while the other four are wrong guesses. Is this a good encoder or a bad one?
The VAE objective is harsh on this situation. Each of the five samples gets its own bound, the bounds are averaged, and the four wrong guesses drag the average way down, so both the inference network and the generative network get led around by those four samples. In Section 3 of their paper, Burda, Grosse, and Salakhutdinov of the University of Toronto write that a recognition network placing "only a small fraction (e.g. 20%) of its samples" in the region of high posterior probability "may still be sufficient for performing accurate inference."
They wanted to lower the standards a little and see whether the generative model would learn better as a result.
Defining the problem
The VAE bound makes the model play along
First, a recap of the VAE's evidence lower bound, Eq. (3) of the paper:
Here the latent variable is written , as in the paper. The inequality in the middle comes from Jensen's inequality; is concave, so the log of an expectation is no less than the expectation of the log. The gap is exactly .
This gap feeds back into the generative model. The VAE's approximate posterior is usually a diagonal Gaussian, with parameters regressed from by a neural network. If the generative model's posterior doesn't look like a diagonal Gaussian, say it has two peaks or bends into an arc, the KL gap gets large and the bound is pulled down. Maximizing the bound therefore pushes the model toward representations in which "the posterior happens to be easy to approximate."
The paper concedes that this has a benefit, since inference becomes easy; the cost is that the generative model's expressive power is tied down by this assumption.
The paper's experiments also record another phenomenon. Given 50 latent dimensions, the VAE actually uses only about 20; in the other dimensions, the approximate posterior is almost identical to the prior and doesn't change with the input.
So the question this paper sets out to answer is whether we can keep the VAE's architecture but switch to a more lenient bound, so that the generative model no longer has to play along with the inference network's assumptions and can learn richer representations as a result.
Multilayer VAEs
The experiments use two layers of random variables, so the notation comes first. The generative model is a chain of top-down conditional distributions (Eq. 1):
The top-level prior is a standard normal, and every other layer is a diagonal Gaussian whose mean and variance an MLP computes from the layer above; when the observations are binary images, is Bernoulli. The inference model goes the other way, inferring layer by layer from the bottom up (Eq. 2):
Every layer can be reparameterized, , so the whole chain of samples is a deterministic function of and . The single-layer case is the original VAE.
Solving the problem
A bound from samples
IWAE's architecture is exactly the same as the VAE's; only the training objective changes. Draw independent samples from the inference network, and compute an importance weight for each sample
then average the weights first and take the log afterward (Eq. 8):
Why is this a lower bound? The expectation of each is exactly :
So is an unbiased estimate of ; this is importance sampling. Apply Jensen's inequality once more (Eq. 9):
At , , which is exactly the VAE bound.
Compared with a VAE using samples, the only difference is the order of averaging and taking the log. A VAE with samples uses , taking the log first and then averaging; IWAE uses , averaging first and then taking the log.
Five samples, worked out
Back to the scenario from the beginning. Suppose the inference network draws five samples for some image, with log weights of
The first sample lands where the posterior probability is high, the second and third are passable, and the last two are complete misses.
- The VAE estimate is the mean of the five log weights, .
- The IWAE estimate is . Pulling out the largest, , gives .
In the VAE, the two wrong guesses each contribute ; in IWAE, together they add only to the sum inside the log, with almost no effect. The best sample determines most of the value, at a cost of at most , here nats.
This is what "lowering the standards" means. As long as a few of the samples are good draws, IWAE's bound won't be too bad; the VAE demands that every sample be a good draw.
The larger , the tighter the bound
Theorem 1 of the paper says that for all ,
and that as long as is bounded, converges to as .
Compare with first, to see where the monotonicity comes from. Take the average of two weights, , and its log; since is concave,
Take the expectation of both sides under . The right side is the average of two copies of , which is just ; the left side is . So .
General and use the same trick (Appendix A). The average of weights equals "pick of the at random and average those ," averaged over all possible picks; since the log is concave, moving inside this average can only make the value smaller or leave it unchanged, and the expectation after moving it inside is exactly .
As for the limit, when is bounded, converges almost surely to by the strong law of large numbers, and after taking the log, converges to .
A well-known weakness of importance sampling is that when the proposal distribution differs too much from the target, the variance of the weights can be infinite. Appendix B shows that things are much better after taking the log. The probability that overestimates by more than nats is at most (by Markov's inequality), from which it follows that the mean absolute deviation of is at most , where is the gap between and .
The gradient is a weighted VAE gradient
Training again uses the reparameterization, with each sample , so the gradient can move inside the expectation (Eqs. 11 and 12). What remains is differentiating :
The second equality uses . Let be the normalized importance weights; the gradient is then (Eqs. 13 and 14, where is the weight computed from ):
Here includes the parameters of both the generative network and the inference network. Each sample computes its own VAE-style gradient , which is then weighted by . A VAE with samples gives every sample a weight of ; IWAE lets the samples that explain the data well speak louder. At , , and we are back to the VAE update.
For the five samples above, the normalized weights are roughly , so the gradient comes almost entirely from the first sample.
itself splits into two terms (Eq. 15):
The first term asks the generative network to give this a high probability, while also having the inference network move to where the generative network predicts better; the second term encourages the inference network's distribution to spread out a little.
There is one thing IWAE can't do. The VAE can compute the KL term in closed form and sample only the reconstruction term, which gives lower variance; for , the KL is wrapped inside the sum within the log and can't be separated out, so the whole thing has to be sampled. A footnote in the paper says that "in principle, the IWAE updates may be higher variance for this reason," but at they "observed that the performance of the two update rules was indistinguishable."
In code
In implementation, is just a logsumexp over :
import math
import torch
def iwae_bound(log_w):
# log_w: shape [batch, k], log p(x, h_i) − log q(h_i | x) for each sample
k = log_w.shape[1]
# log (1/k) Σ_i w_i; logsumexp subtracts the max internally so exp doesn't underflow
return torch.logsumexp(log_w, dim=1) - math.log(k)
loss = -iwae_bound(log_w).mean()
The partial derivative of logsumexp with respect to its -th input is a softmax, that is, , so automatic differentiation produces Eq. (14) directly, and there is no need to compute the weights by hand. easily reaches or . Take exp of these directly in float32, and leaves only a tiny value with very poor precision (about ), while becomes exactly 0, so the maximum must be subtracted first; the official Theano code subtracts each row's maximum, normalizes to get , and multiplies by it as a constant, to the same effect.
The cost is proportional to . Each sample needs its own forward and backward pass, and the official implementation copies each training example times within the minibatch to compute them in parallel. The paper also mentions a way to save computation. The weights need only a forward pass, so you can first draw a single sample according to and backpropagate through it alone; when is large, this cuts the multiply-adds to about a third, at the price of higher gradient variance.
Relation to reweighted wake-sleep
Reweighted wake-sleep (RWS), proposed a little earlier by Bornschein and Bengio, also uses importance weights. Its generative network update is exactly gradient ascent on ; but RWS interprets this as a biased estimate of , its two update steps for the inference network have nothing to do with , and it doesn't use the reparameterization. IWAE has both networks maximize the same .
Discussion
Experimental setup
The data are MNIST (60,000 training and 10,000 test images) and Omniglot (handwritten characters from many writing systems around the world, 24,345 training and 8,070 test images), all reduced to binary images. Binarization treats the grayscale values as probabilities and samples a fresh image each time; a footnote in the paper points out that "the generative modeling literature is inconsistent about the method of binarization," and that "different choices can lead to considerably different log-likelihood values."
Two architectures:
- One stochastic layer: has 50 dimensions, with two deterministic layers of 200 units between and .
- Two stochastic layers: has 100 dimensions and has 50; there are two layers of 200 units between and , and two layers of 100 units between and .
All deterministic layers use tanh, and the variances pass through exp to stay positive. Optimization uses Adam (, , ) with minibatches of 20; the learning rate follows eight stages, with stage running epochs at a learning rate of , for 3,280 epochs in total.
takes the values 1, 5, and 50, and for each both a VAE (averaging the log weights over samples) and an IWAE are trained, with roughly the same training time.
For evaluation, each test image uses , the IWAE bound with 5,000 samples, as its log-likelihood estimate. It is still a lower bound, but much tighter than the ones used in training. The later Ladder VAE and VampPrior both estimate test log-likelihood with 5,000 importance samples, and both take their Omniglot data straight from the IWAE repository.
Log-likelihood

The full numbers are in the two tables below. NLL is the negative log-likelihood (nats, lower is better), and Active is the number of active units; for two-layer models it is written "first layer + second layer."
| Layers | VAE NLL | Active | IWAE NLL | Active | |
|---|---|---|---|---|---|
| 1 | 1 | 86.76 | 19 | 86.76 | 19 |
| 1 | 5 | 86.47 | 20 | 85.54 | 22 |
| 1 | 50 | 86.35 | 20 | 84.78 | 25 |
| 2 | 1 | 85.33 | 16+5 | 85.33 | 16+5 |
| 2 | 5 | 85.01 | 17+5 | 83.89 | 21+5 |
| 2 | 50 | 84.78 | 17+5 | 82.90 | 26+7 |
Compiled from Table 1 of the paper; MNIST.
| Layers | VAE NLL | Active | IWAE NLL | Active | |
|---|---|---|---|---|---|
| 1 | 1 | 108.11 | 28 | 108.11 | 28 |
| 1 | 5 | 107.62 | 28 | 106.12 | 34 |
| 1 | 50 | 107.80 | 28 | 104.67 | 41 |
| 2 | 1 | 107.58 | 28+4 | 107.56 | 30+5 |
| 2 | 5 | 106.31 | 30+5 | 104.79 | 38+6 |
| 2 | 50 | 106.30 | 30+5 | 103.38 | 44+7 |
Compiled from Table 1 of the paper; Omniglot.
At the two algorithms are identical, so their numbers should differ only by random variation; the 107.58 versus 107.56 in Omniglot's two-layer row is exactly that kind of difference.
- More samples help the VAE only a little. On MNIST with one layer, it goes from 86.76 to 86.35, an improvement of 0.41 nats; on Omniglot with two layers it improves by 1.28 nats, and with one layer, is actually worse than .
- IWAE improves steadily with . On MNIST, one layer goes from 86.76 to 84.78 and two layers from 85.33 to 82.90; on Omniglot, one layer goes from 108.11 to 104.67. The one-layer IWAE () catches up with the two-layer VAE, both at 84.78.
- Two layers beat one. This holds for both methods.
Compared with other models, the two-layer IWAE's −82.90 on MNIST beats the deep belief network's roughly −84.55 and DARN's −84.13; DRAW, which exploits the spatial structure of images, reaches −80.97 under different conditions. On Omniglot, the best result, −103.38, falls slightly short of an RBM with 500 hidden units trained with persistent contrastive divergence (−100.46).
Overfitting is mild. On MNIST, training and test log-likelihoods differ by only 0.62 to 0.79 nats, and on Omniglot by 2.39 to 2.65 nats. Appendix D redoes the experiments on the fixed binarization of MNIST common in the literature (50,000 training images); overfitting increases noticeably, the numbers are generally two or three nats worse, the two-layer IWAE () gets 85.32 and the two-layer VAE 87.86, and the relative conclusions hold. The README of the official code adds that the VAE models whose log-likelihoods were reported in the literature "used the fixed binarization dataset augmented by samples with added flip-bit or drop-out noise." Before comparing MNIST log-likelihoods across papers, check how the data were binarized.
Unused dimensions
How does the paper decide whether a dimension is in use? If a dimension really records information about the data, its approximate posterior should change with the input. So for each dimension , the authors first compute each image's posterior mean in that dimension, , and then look at the variance of this mean across the whole dataset:
counts as active. If the encoder outputs the same mean no matter what the input is, that dimension has recorded nothing.

This is the distribution of over the 50 dimensions of a one-layer VAE. The two clusters are far apart with nothing in between, so where the threshold goes hardly matters; the right cluster has about 19, matching the number of active units for the one-layer VAE () in Table 1. The authors also removed all the inactive dimensions and re-evaluated, and the test log-likelihood changed by less than 0.06 nats every time; these dimensions really do nothing.
Now look at the "Active" columns of the two tables above.
- In every setting, the active units are far fewer than the total. The one-layer VAE on MNIST uses only 19 to 20 of its 50 dimensions.
- The second layer of the two-layer models is barely used; its active count never exceeds 10.
- For , IWAE uses more dimensions than the VAE every single time. On Omniglot with one layer, the VAE stays at 28 dimensions, while IWAE uses 41 at .
In the caption of its MNIST lower-bound experiment, the VAE paper wrote that "more latent variables does not result in more overfitting," attributing this to "the regularizing effect of the lower bound." This paper gives a more direct answer, which is that the extra dimensions are never used at all.
Objective or optimization?
Units "dying" is common in neural networks and is often blamed on optimization. Once a unit goes inactive, it may sit in a region where the gradient is flat and never receive a meaningful gradient again; a different initialization might fix it.
The authors use a swap experiment to tell the two explanations apart. They take the one-layer VAE with and the one-layer IWAE with from Table 1, swap their objectives, and train each for another 37 epochs at a learning rate of :
| Exp. | Stage 1 | NLL | Active | Stage 2 | NLL | Active |
|---|---|---|---|---|---|---|
| 1 | VAE | 86.76 | 19 | IWAE | 84.88 | 22 |
| 2 | IWAE | 84.78 | 25 | VAE | 86.02 | 23 |
Compiled from Table 2 of the paper; MNIST. The stage-2 IWAE uses .
When the VAE continues training with the IWAE objective, its active units go from 19 up to 22 and its NLL drops from 86.76 to 84.88; when the IWAE switches to the VAE objective, its active units fall from 25 to 23 and its NLL rises to 86.02. The VAE objective actively switches dimensions off and pulls the log-likelihood down, so inactivity is mainly caused by the objective. Still, neither experiment gets all the way to the other's numbers in Table 1, so optimization plays a part too.
The shape of the posterior

Finally, a set of models whose latent space has only 2 dimensions. Each row is the true posterior of one training example; the left column is the VAE, the middle is IWAE with , and the right is IWAE with .
The posteriors the VAE learns are mostly single, clean ellipses, exactly the shape a diagonal Gaussian approximates easily. IWAE's posteriors don't follow the rules so well; in the second row, the middle one has two peaks, and in the fourth row, the right one bends into an arc. In the paper's words, "The IWAE prefers less regular posteriors and more spread out posterior predictions." This is exactly what the beginning of this note described; the VAE's bound forces the generative model to play along with the inference network, and IWAE relaxes that demand.
What came later
After IWAE, several papers looked at this bound from other angles.
In 2017, Cremer, Morris, and Duvenaud proposed another interpretation. IWAE amounts to optimizing an ordinary ELBO, except that its approximate posterior is replaced by a more complex implicit distribution, obtained by resampling the samples according to their weights.
In 2018, Rainforth et al.'s "Tighter Variational Bounds are Not Necessarily Better" analyzed the signal-to-noise ratio of the gradients. For the generative network, the gradient's signal-to-noise ratio grows as as increases; for the inference network, it falls as . As the bound tightens, the generative network learns better, while the signal reaching the inference network gets weaker and weaker. They therefore proposed several compromises, such as giving the two networks different objectives. Later the same year, Tucker et al. rewrote the inference network's gradient with a second reparameterization (DReG), so that it no longer degrades as grows.
Conclusion
IWAE doesn't change a single layer of the VAE; it only swaps "take the log, then average" for "average, then take the log." At the two are the same; as grows, the bound tightens monotonically, the generative model no longer has to play along with a diagonal-Gaussian inference network, more latent dimensions get used, and the negative log-likelihood of the two-layer MNIST model drops from 85.33 to 82.90. The objective-swap experiment also shows that the main cause of the VAE's idle dimensions is the objective itself.
The most common use of this paper is evaluation. When reporting the log-likelihood of a VAE-type model, estimate it with using a few thousand samples, and state and how the data were binarized. Raising during training makes the computation grow linearly and weakens the inference network's gradient; if the quality of the inference network matters too, you can give the two networks different objectives as Rainforth et al. do, or switch to DReG gradients.
The last paragraph of Section 4 adds that only after the paper was published did the authors learn that Laurent Dinh and Vincent Dumoulin had independently tried training VAEs with an importance-weighted bound, with preliminary results presented at a summer school in 2014.