Search

Search papers and lessons

Importance Weighted Autoencoders

Yuri Burda, Roger Grosse, Ruslan Salakhutdinov · 2015

The importance weighted autoencoder keeps the VAE's architecture but trains it on a tighter bound on the log-likelihood, built from several samples, which lets the model use more of its latent space.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Discrete math
  • Linear algebra
  • Sampling
  • Generative models
  • ReadReady to readNot yet

TL;DR

The importance weighted autoencoder, or IWAE, uses the same two networks as a variational autoencoder: a generative network that builds data from hidden codes, and a recognition network that guesses the codes for a given input. Only the training target changes. Instead of the VAE's bound, which uses one sample per input, the IWAE averages several importance weights inside a logarithm. That bound is tighter, and it rises as the number of samples grows.

On the MNIST and Omniglot handwriting datasets, IWAEs reached better held-out log-likelihood than VAEs with the same architecture and about the same training time. They also used more of their latent dimensions, which the authors read as a richer hidden representation.

Builds on: Variational Autoencoder, Importance Sampling · Leads to: Normalizing Flow, which the paper names as another route to a more flexible posterior

Why it mattered

The VAE was new when this paper appeared, and it became one of the main recipes for deep generative models. This paper showed that the VAE objective itself was holding those models back. Its strict penalty on posterior samples that fail to explain the data pushed the model toward simple hidden codes, and many latent dimensions went unused. Changing only the objective, with the architecture kept fixed, let the same networks use more of their capacity.

The paper's numbers were competitive with the best models of its time. On MNIST with two stochastic layers and 50 samples, the IWAE reached a log-likelihood of -82.90 nats per image. The paper compares this with -84.55 for deep belief networks, -84.13 for deep autoregressive networks, and -80.97 for a model by Gregor and colleagues that exploited spatial structure. The bound also travels beyond this model: the authors note that reweighted wake-sleep (Bornschein and Bengio, 2015) is related but trained on a different target, and that Dinh and Dumoulin had independently explored an importance weighted bound for VAEs, presented in preliminary form at a 2014 summer school.

The problem

Training a latent-variable model by maximum likelihood needs the posterior, the distribution over hidden codes given the data. For most models that posterior is intractable, so the model is trained on a lower bound that uses an approximate posterior q(h∣x)q(\mathbf{h}|\mathbf{x}). The VAE uses the bound

L(x)=Eq(h∣x)[log⁡p(x,h)q(h∣x)]≤log⁡p(x).\mathcal{L}(\mathbf{x}) = \mathbb{E}_{q(\mathbf{h}|\mathbf{x})}\left[\log \frac{p(\mathbf{x},\mathbf{h})}{q(\mathbf{h}|\mathbf{x})}\right] \leq \log p(\mathbf{x}).

The paper points out that the gap between this bound and the true log-likelihood is a KL divergence between the approximate and the true posterior. Maximising the bound therefore forces a trade-off: it raises the data log-likelihood and also pulls q(h∣x)q(\mathbf{h}|\mathbf{x}) toward a shape that is easy to predict with a neural network.

That shape is the cost. The VAE assumes the posterior is roughly factorial, meaning the hidden dimensions are independent, and that its parameters can be predicted by a feedforward network. The bound is tight only if those assumptions hold, and it punishes any sample that lands where the true posterior has mass but the approximation does not. The authors argue that a recognition network which puts only a fraction of its samples in the good region can still do accurate inference, if the objective allows it. The question is how to get a training signal that does not punish those samples so hard.

How it works

Take one input xx and draw k=3k = 3 hidden codes from the recognition network. Suppose the three importance weights the model produces are 22, 0.50.5 and 11.

  1. Draw several codes for one input. The recognition network q(h∣x)q(\mathbf{h}|x) gives a distribution over codes. Draw k=3k = 3 codes, h1\mathbf{h}_1, h2\mathbf{h}_2 and h3\mathbf{h}_3. Check: each code is a full hidden vector, and the three are independent draws.
  2. Score each code with a weight. For each code compute wi=p(x,hi)/q(hi∣x)w_i = p(x, \mathbf{h}_i) / q(\mathbf{h}_i|x). Suppose the three weights are 22, 0.50.5 and 11. Check: a weight above 1 means the model likes that code more than the recognition network expected.
  3. Average the weights. (2+0.5+1)/3=1.1667(2 + 0.5 + 1) / 3 = 1.1667. Check: this average is an unbiased estimate of p(x)p(x), the quantity we want.
  4. Take the log of the average. log⁡1.1667≈0.154\log 1.1667 \approx 0.154. This is the IWAE objective for this input. Check: the log sits outside the average, not inside it.
  5. Compare with averaging the logs. The logs of the weights are log⁡2≈0.693\log 2 \approx 0.693, log⁡0.5≈−0.693\log 0.5 \approx -0.693 and log⁡1=0\log 1 = 0. Their average is 00, which is what the VAE-style estimate gives over three draws. Check: 0.1540.154 is larger than 00, so the IWAE objective is the tighter of the two.
  6. Learn from normalised weights. Divide each weight by the total 3.53.5 to get 0.5710.571, 0.1430.143 and 0.2860.286, which sum to 1. The gradient for the model is a weighted sum of the per-sample gradients using these numbers. Check: the sample with the largest weight moves the model most, and the sample with the smallest weight moves it least.
flowchart LR
  X[Input x] --> R[Recognition network]
  R --> H[k codes h_1 to h_k]
  H --> W[Weights w_i = p of x and h_i over q of h_i given x]
  W --> A[Average the k weights]
  A --> L[Log of the average is the objective]
  W --> G[Normalised weights steer the gradient]

The shape of the objective explains the gain. The plot shows log⁡\log of the three weights from the walkthrough, and the point where the log of their average falls.

0.511.522.533.54−1−0.500.511.5weight wlog w
  • log x
The three weights 2, 0.5 and 1 sit on the curve at their logs, with mean log 0. The log of their average, 0.154, lies higher because the average of the weights is taken before the log.

The math

The IWAE bound with kk samples is

Lk(x)=Eh1,…,hk∼q(h∣x)[log⁡1k∑i=1kwi],wi=p(x,hi)q(hi∣x).\mathcal{L}_k(\mathbf{x}) = \mathbb{E}_{\mathbf{h}_1, \ldots, \mathbf{h}_k \sim q(\mathbf{h}|\mathbf{x})}\left[\log \frac{1}{k}\sum_{i=1}^{k} w_i\right], \qquad w_i = \frac{p(\mathbf{x}, \mathbf{h}_i)}{q(\mathbf{h}_i|\mathbf{x})}.

Here x\mathbf{x} is one observed image, hi\mathbf{h}_i is a hidden code drawn from the recognition network, and wiw_i is that code's importance weight. The expectation averages over all possible sets of kk draws.

Step 1 in symbols: the samples and weights

Draw h1,…,hk\mathbf{h}_1, \ldots, \mathbf{h}_k independently from q(h∣x)q(\mathbf{h}|\mathbf{x}), then compute wiw_i for each. With k=1k = 1 the sum has one term and the bound becomes E[log⁡w1]\mathbb{E}[\log w_1], which is the VAE bound. So the VAE is the special case L1\mathcal{L}_1.

Step 2 in symbols: the average inside the log

The weights are averaged, 1k∑iwi\frac{1}{k}\sum_i w_i, and the logarithm is taken of that average. Swapping the order of average and log would give 1k∑ilog⁡wi\frac{1}{k}\sum_i \log w_i, the VAE-style estimate. Taking the log after the average is what makes the bound tighter.

Step 3 in symbols: why the bound is valid

Since Eq[wi]=p(x)\mathbb{E}_q[w_i] = p(\mathbf{x}), the average of the weights is an unbiased estimate of p(x)p(\mathbf{x}). Jensen's inequality for the concave logarithm, E[log⁡Y]≤log⁡E[Y]\mathbb{E}[\log Y] \leq \log \mathbb{E}[Y], then gives

Lk≤log⁡p(x).\mathcal{L}_k \leq \log p(\mathbf{x}).

The bound never overshoots the true log-likelihood.

Step 4 in symbols: more samples never hurt

The paper proves that the bounds never decrease as kk grows,

log⁡p(x)≥Lk+1≥Lk.\log p(\mathbf{x}) \geq \mathcal{L}_{k+1} \geq \mathcal{L}_k.

The argument applies Jensen's inequality again, to a random subset of mm samples drawn from the kk available. If the weights are bounded, Lk\mathcal{L}_k approaches log⁡p(x)\log p(\mathbf{x}) as kk goes to infinity.

Step 5 in symbols: the gradient and normalised weights

Let w~i=wi/∑j=1kwj\widetilde{w}_i = w_i / \sum_{j=1}^{k} w_j. The gradient of the bound with respect to the model parameters θ\boldsymbol{\theta} is estimated by

∇θLk≈∑i=1kw~i ∇θlog⁡wi.\nabla_{\boldsymbol{\theta}} \mathcal{L}_k \approx \sum_{i=1}^{k} \widetilde{w}_i \, \nabla_{\boldsymbol{\theta}} \log w_i.

Each sample's gradient is weighted by w~i\widetilde{w}_i, so samples that explain the data well have more influence. The weights sum to 1. When k=1k = 1 the single normalised weight is 1 and the rule reduces to the VAE update.

Step 6 in symbols: the reparameterised sample

To take gradients through the sampling step, the paper writes each code as a deterministic function of the input and noise. For a Gaussian layer,

hℓ=μ(hℓ−1,θ)+Σ(hℓ−1,θ)1/2ϵℓ,ϵℓ∼N(0,I).\mathbf{h}^{\ell} = \boldsymbol{\mu}(\mathbf{h}^{\ell-1}, \boldsymbol{\theta}) + \boldsymbol{\Sigma}(\mathbf{h}^{\ell-1}, \boldsymbol{\theta})^{1/2} \boldsymbol{\epsilon}^{\ell}, \qquad \boldsymbol{\epsilon}^{\ell} \sim \mathcal{N}(\mathbf{0}, \mathbf{I}).

Here μ\boldsymbol{\mu} and Σ\boldsymbol{\Sigma} are the mean and covariance produced by the network, and ϵ\boldsymbol{\epsilon} is noise that does not depend on the parameters. This is the reparameterization trick applied to every sample.

Results

Density estimation on MNIST and Omniglot. The paper reports negative log-likelihood in nats, so lower is better. Two stochastic layers, k=50k = 50, gave the following. On MNIST the VAE reached 84.78 and the IWAE 82.90, a gap of 84.78−82.90=1.8884.78 - 82.90 = 1.88 nats. On Omniglot the VAE reached 106.30 and the IWAE 103.38, a gap of 106.30−103.38=2.92106.30 - 103.38 = 2.92 nats. The best Omniglot IWAE, at -103.38 nats, is slightly worse than the -100.46 nats of a restricted Boltzmann machine with 500 hidden units trained with persistent contrastive divergence, so the claim is about the VAE comparison, not state of the art on that dataset.

The gap grows with k. With one stochastic layer on MNIST, the VAE and IWAE are identical at k=1k = 1 (both 86.76), which the theory predicts. At k=5k = 5 the VAE reaches 86.47 and the IWAE 85.54, a gap of 0.93 nats. At k=50k = 50 the VAE reaches 86.35 and the IWAE 84.78, a gap of 1.57 nats. The VAE gains only slightly from more samples, while the IWAE keeps improving, as Step 4 of the math predicts.

Latent dimensions in use. Table 1 also counts active latent units, where a unit is active when its posterior mean varies across inputs. For two layers on MNIST with k=50k = 50, the VAE uses 17+5 units and the IWAE uses 26+7. In every case with k>1k > 1, the IWAE learned more active dimensions than the VAE. The authors read this as a richer latent representation, which coincided with higher likelihood.

Is the objective or the optimiser to blame? Table 2 swaps objectives after training. The VAE, trained to 86.76 with 19 active units, reached 84.88 with 22 units after more training with the IWAE objective at k=50k = 50. The IWAE, trained to 84.78 with 25 active units, fell to 86.02 with 23 units after training with the VAE objective. Switching objectives moves both quantities in the same direction. The authors conclude that the objective, not poor optimisation, drives the unused units, though optimisation plays some role.

Limits. The experiments use two small benchmarks with binarised images, and the paper notes that binarisation choices change likelihood values a lot, so comparisons must share a protocol. Overfitting was mild, with training log-likelihood 0.62 to 0.79 nats above test on MNIST. The basic update costs kk forward and backward passes per example, so training time grows with kk. Each of the kk samples passes through the networks on its own. The single-sample variant from the paper needs kk forward passes and one backward pass per example, roughly a factor of 3 saving for large kk, at the price of higher gradient variance; the authors found that trade-off favourable. The paper also notes that the VAE trick of splitting out the KL term has no clean counterpart for k>1k > 1, so the IWAE update may have higher variance in principle, although the two updates performed indistinguishably at k=1k = 1. Appendix B argues that the log of the average does not suffer the huge variance usually associated with importance weights: the mean absolute deviation of the estimate is bounded by 2+2δ2 + 2\delta, where δ\delta is the gap between the bound and the true log-likelihood.

Reading guide

Read the paper in this order, with these lessons in hand.

Check yourself

QuestionRecall: with a single sample, k=1k = 1, what does the IWAE bound reduce to?Show answer
The bound reduces to E[log⁡w1]\mathbb{E}[\log w_1], which is the variational lower bound of the VAE. The paper states that the IWAE update is identical to the VAE update in this case.
QuestionComputation: two importance weights are w1=3w_1 = 3 and w2=0.75w_2 = 0.75. What is the IWAE objective for this input, and what is the average of the logs?Show answer
The average of the weights is (3+0.75)/2=1.875(3 + 0.75)/2 = 1.875, and log⁡1.875≈0.629\log 1.875 \approx 0.629. The logs are log⁡3≈1.099\log 3 \approx 1.099 and log⁡0.75≈−0.288\log 0.75 \approx -0.288, whose average is 0.4060.406. The IWAE objective is 0.6290.629, which is larger than the average of the logs, 0.4060.406, as Jensen's inequality predicts.
QuestionWhy: why does the IWAE bound improve as the number of samples grows, while the VAE bound gains only a little?Show answer
Each extra sample adds another weight to the average inside the log. The average becomes a better estimate of p(x)p(\mathbf{x}), and Theorem 1 says the bound cannot decrease as kk increases. The VAE objective uses only one sample per input, so it has no averaging to improve, and its gains from larger kk are small in the paper's Table 1.
QuestionApplied: the basic IWAE update costs kk forward and backward passes per example. Why might the authors' single-sample variant be preferred for large kk, and what does it cost?Show answer
The variant draws one sample in proportion to its normalised weight. It needs kk forward passes but only one backward pass per example. The backward pass is roughly twice as costly as the forward pass, so for large kk this saves roughly a factor of 3 in the number of add-multiply operations. The price is higher variance in the gradient, which the authors judged a favourable trade-off.

Sources

Read the full paper

Yuri Burda, Roger Grosse, Ruslan Salakhutdinov, 2015. arXiv:1509.00519