Ladder Variational Autoencoders
- Publication
- NeurIPS 2016Conference
- First submitted to arXiv
- arXiv:1602.02282
Cite this paper · BibTeX
The citation identifies the linked source version. It cites the original paper and its authors.
@misc{imagegenerationladdervae2016,
title = {{Ladder Variational Autoencoders}},
author = {Casper Kaae Sønderby and Tapani Raiko and Lars Maaløe and Søren Kaae Sønderby and Ole Winther},
year = {2016},
eprint = {1602.02282},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/1602.02282}
}
The first version of this paper on arXiv was titled "How to Train Deep Variational Autoencoders and Probabilistic Ladder Networks." The abstract listed three improvements: an inference model modeled on the Ladder Network, a warm-up period, and batch normalization. The three stood on equal footing, and the goal was to stack a VAE five layers of stochastic latent variables deep for the first time.
In the second version, a little over three months later, the title was down to just "Ladder Variational Autoencoders." Warm-up and BN were still there, demoted to the observation that they are "crucial for training variational models with many stochastic layers." The README of the official repo still carries the old title from the first version.
This note takes all three apart. First, where multilayer VAEs get stuck; then how LVAE's inference combines the prior handed down from "the floor above" with the evidence from the data; and last, whether all five layers of latent variables are really doing any work.
Defining the Problem
Stacking more layers of latent variables
The original VAE has a single set of latent variables with a standard normal prior, and the decoder generates from . A hierarchical VAE splits the latent variables into layers: the top layer is sampled from a standard normal, each layer below is a Gaussian conditioned on the layer above it, and finally generates the image:
The subscript marks this mean and variance as belonging to the generative model; the inference model's will carry the subscript later. Each conditional distribution is a diagonal Gaussian whose dimensions are independent of each other; but once the layers are stacked, the marginal distribution of can have strong correlations, while the computation still only ever deals with diagonal Gaussians. The observation model is Bernoulli on binary images such as MNIST and Gaussian on the grayscale NORB.
The paper's largest model has five layers, with latent dimensions 64, 32, 16, 8, 4 from bottom to top.
Upper layers are trained only through the KL
Write out the ELBO for the two-layer case first. Standard VAE inference runs bottom-up, inferring from and then from , . Substitute this into the ELBO's definition and split the joint distribution into three factors:
The first term is the reconstruction term; each of the other two measures how far one layer's approximate posterior is from its prior, i.e., each layer's KL term (with the sampled layer above inside the expectation).
Look at where shows up. It is absent from the reconstruction term and appears only in the last two terms. The job of is to give a better prior so that the second term shrinks; it makes no direct contribution to reconstruction. The first author said the same when answering a question in issue #1 of the official repo, describing the upper layers as trained only indirectly through the KL term and only trying to match the distribution of the layers below, and calling this one of the weaknesses of VAEs.
That leads to the first bottleneck. Break the ELBO down further and each layer's KL can be split across its individual latent units. If a unit has a KL of 0, its approximate posterior is identical to the prior; whatever image comes in, it outputs the same distribution and carries no information about the data, and the paper calls it an inactive unit. Early in training, reconstruction is still poor, and for the upper layers pushing the KL to 0 is the cheapest way to lower the loss. The authors observed that units that collapse this early almost never wake up for the rest of training, as if stuck in a local minimum or saddle point at .
The second bottleneck is in the inference network itself. The bottom-up and the generative are two unrelated networks, and neither can see the other. Early in training the generative distribution moves around a lot, and the inference network has to chase it on its own to keep each layer's KL small.
Before this paper, VAEs mostly used just one or two layers of stochastic latent variables; the authors found that purely bottom-up inference trained with gradient methods could only partly use even two.
So the question this paper answers is whether, with the generative model left alone, a different inference model can put all five layers of latent variables to work.
Solving the Problem
Two passes

In the figure, circles are random variables and diamonds are deterministic features. On the left, a) is the standard VAE, where inference and generation run in opposite directions. On the right, b) is the LVAE, where inference takes two passes.
The first pass goes up and is entirely deterministic. Starting from , each layer goes through a two-layer MLP, and then two linear layers read out that layer's estimates:
Take the five-layer MNIST model. The 784-dimensional goes through an MLP of width 512 to get , from which the 64-dimensional and are read out; goes through an MLP of width 256 to get , which gives 32-dimensional estimates; and so on up to , which gives 4-dimensional estimates. Softplus is , which keeps the variance positive.
This pass only computes what each layer would guess "from the data alone"; nothing has been sampled yet.
The second pass goes down, sampling as it goes. The top layer uses the estimates from the upward pass directly, , and is sampled to get . Then goes through the generative model's mapping, giving the generative model's prior for , and . Layer 4 now holds two opinions, one from the data downstairs and one from the prior upstairs; it merges the two into the approximate posterior, samples , and moves down another floor.
The whole approximate posterior thus has the same top-down structure as the generative model:
The "shared" between the two arrows in b) means that the second pass of inference reuses the generative model's mapping directly. The generative model is identical to the VAE's and the inference model has a similar number of parameters; the two differ only in how inference proceeds.
Precision weighting
How are the two opinions merged? Equations 17 and 18 of the paper read:
The reciprocal of the variance, , is called the precision. The new precision is the sum of the two precisions, and the new mean is the average of the two means weighted by their precisions.
These formulas come from multiplying two Gaussians. The authors treat the estimate from downstairs as an approximate Gaussian likelihood and the prior from upstairs as a Gaussian prior, and the posterior is proportional to their product. For a single dimension, dropping the constants that don't depend on :
Expand the two squares in the exponent and collect by powers of :
The exponent of a Gaussian expands to . Matching term by term, the coefficient of gives , and the coefficient of gives . Dividing one by the other gives the above.
Run the numbers twice for one unit. Suppose the prior from upstairs is , , with precision 1.
- Downstairs is confident: , , precision 4. The merged precision is 5, , . The data pulls the posterior almost all the way over.
- Downstairs isn't sure: , , precision 0.25. The merged precision is 1.25, , . The posterior stays roughly near the prior.
Next, compute this unit's KL. The KL between two one-dimensional Gaussians has a closed form:
The first case gives nats; the second gives nats.
This is what the paper means when it says the downward pass "recursively corrects the generative distribution" with the data. Upstairs first states the generative model's current view, and the posterior is pushed toward the data as far as downstairs' confidence warrants; how much KL this unit pays for this image follows downstairs' confidence as well. The inference network doesn't have to rebuild a distribution close to the prior from scratch, because its starting point is the prior itself.
Top-down sampling has a computational bonus too. Once has been sampled, and are both diagonal Gaussians with known parameters, so this layer's KL can use the closed form above directly, summed over the dimensions.
Warm-up
The second improvement deals with units collapsing too early. The authors put a coefficient in front of the KL term:
Training starts at , which rises linearly to 1 over the first epochs, after which the objective is the original ELBO. For MNIST, training runs for 2,000 epochs in total, with warm-up over the first 200.
At only the reconstruction term is left and the variance of the approximate posterior carries no penalty, so the best solution shrinks the noise to 0, and the model is a deterministic autoencoder. The authors describe this starting point as a delta-function solution at zero temperature. As grows, the KL starts pulling each unit toward the prior; units that already learned useful representations during the autoencoder phase have a reason to resist that pull. In the official code, is simply multiplied by a temperature that rises from 0 to 1.
Batch normalization
The third improvement adds BN to every MLP in both the VAE and the LVAE. BN injects noise between mini-batches during training, which doesn't sound like a good idea for a model that already runs on sampling; the authors say in their conclusion that they were surprised BN helped this much and don't yet fully understand why.
Experimental setup
Three datasets:
- MNIST: 28×28 handwritten digits, a Bernoulli observation model and leaky ReLU activations (). Trained for 2,000 epochs with learning rate 0.001. As in IWAE, the binarized training images are resampled from the grayscale values every epoch to avoid overfitting.
- OMNIGLOT: 28×28 binary images of 50 alphabets, with 20 handwritten samples per character, trained for 1,500 epochs.
- NORB: 32×32 grayscale images of five kinds of toy objects under different lighting and angles, a Gaussian observation model and tanh activations.
All mappings are two-layer MLPs, with width 512 between and the first layer and then 256, 128, 64, 32 going up; the VAE and LVAE use the same widths, and shallower models simply drop the top layers. The optimizer is Adam, with batch size 256 and 1 Monte Carlo sample during training.
Two numbers are reported. is the ELBO with 1 sample; is the importance-weighted bound with 5,000 samples, much tighter than the ELBO, which the paper uses as an approximation of the true log-likelihood. Both are in nats, and higher is better; the gap between them can be read as how far the inference model still is from the true posterior.
Discussion
MNIST: adding one layer at a time

The horizontal axis is the number of stochastic latent layers and the vertical axis is log-likelihood. The four lines are the VAE without BN or warm-up (red), with BN (green), with warm-up added as well (purple), and the LVAE, also with BN and warm-up (orange).
Start with the test-set in the middle panel, b). The red line never improves after one layer and gets worse the more layers it has; the extra latent variables are of no use to it at all. The green and purple lines level off after two or three layers, at around −87.5; in this plot they almost overlap. The orange line goes up with every added layer, reaching −85.23 at five layers, 2.26 nats above the best VAE (three layers, −87.49).
Then look at in panel c) on the right. The LVAE is at −82.12 and the best VAE at −82.74, and the gap shrinks to 0.62 nats.
The two gaps only get interesting when read together. The two models have the same generative architecture; is closer to the true log-likelihood and measures the learned generative model itself, while also includes the inference model's error. Of the LVAE's 2.26 nats, about 0.62 nats come from a better generative model and the rest from a tighter bound, that is, an approximate posterior closer to the true posterior. As the paper puts it, the LVAE gets both a higher log-likelihood and a significantly tighter lower bound.
Also, the purple and green lines nearly overlap in b) and only separate in c); at five layers, warm-up lifts from about −83.4 to about −82.8 (read off the plot). What warm-up mainly improves is the learned generative model; it has almost no effect on the 1-sample ELBO.
The authors then fine-tuned the five-layer LVAE for another 2,000 epochs, multiplying the learning rate by 0.75 every 200 epochs and raising both the Monte Carlo and the importance-weighted sample counts to 10, and compared it with other methods of the time:
| Model | |
|---|---|
| VAE, 1 layer + normalizing flow | −85.10 |
| IWAE, 2 layers, IW=1 | −85.33 |
| IWAE, 2 layers, IW=50 | −82.90 |
| VAE, 2 layers + VGP | −81.90 |
| LVAE, 5 layers | −82.12 |
| LVAE, 5 layers, fine-tuned | −81.84 |
| LVAE, 5 layers, fine-tuned, IW=10 | −81.74 |
Compiled from Table 1 of the paper, MNIST test set.
Trained with just 1 sample, the LVAE already beats the IWAE trained with 50 importance-weighted samples by 0.78 nats; the variational Gaussian process (VGP) at −81.90 is only overtaken once the LVAE is fine-tuned. The authors themselves note that these methods use different training procedures, so the numbers in the table are not directly comparable. They also say that none of the models showed any overfitting.
Who is doing the work

As mentioned earlier, each unit's KL can be pulled out on its own. This figure draws every unit of the five-layer MNIST models as a row, with training epochs on the horizontal axis and log KL as the color; the whiter, the closer to 0, i.e., the less active. Units within each layer are sorted by magnitude.
In the standard VAE, layers 3 to 5 are white from start to finish, and layer 2 has only one line left. With BN, layer 1 has more active units and layer 3 gets a line too. With warm-up, every layer starts as a large dark patch early in training; then the weight of the KL term rises and units get pruned one by one, but more of them survive in the end than without warm-up, and the representation is more distributed. Without warm-up, each unit's KL barely changes over the whole of training; whatever collapsed at the start never woke up again.
The LVAE keeps large swaths of active units in all five layers, and the 4 units of layer 5 are working almost the whole time.
Summing the units of a layer gives the per-layer KL, which can be read as how much information each layer encodes for an image.
The standard VAE puts almost all the information in layer 1, about 22 nats, with under 2 nats in layer 2 and 0 above that. The VAEs with BN or warm-up use one or two more layers, but layer 1 still takes most of it, about 19 nats. The LVAE's layer 1 has only about 7.6 nats, layer 2 about 8.6, layer 3 about 3.6, layer 4 under 1, and the top layer still about 4.2. Summed over five layers, the LVAE has about 25 nats, close to the standard VAE's roughly 24; what it does is move a similar amount of information up into the higher layers.

Last comes a qualitative check. The authors sample from each layer's approximate posterior, project the samples to two dimensions with PCA, and color them by the true digit class.
In the standard VAE, everything above layer 2 is a standard normal cloud with the colors completely mixed, meaning these layers have collapsed back to the prior. BN and warm-up each bring one more layer back to life. The LVAE has structure in all five layers, and clusters more by class the higher you go; the 4 units of layer 5 already cut the ten digits into several fan-shaped pieces. From this the authors speculate that such upper-layer representations might be useful for semi-supervised learning.
OMNIGLOT and NORB
Beyond MNIST, here are the test-set log-likelihoods on the other two datasets. The left column is the number of latent layers; with one layer the LVAE is the same as the VAE, so that cell is left empty.
| Layers | VAE | +BN | +BN+WU | LVAE |
|---|---|---|---|---|
| 1 | −111.21 | −105.62 | −104.51 | − |
| 2 | −110.58 | −105.51 | −102.61 | −102.63 |
| 3 | −111.26 | −106.09 | −102.52 | −102.18 |
| 4 | −111.58 | −105.66 | −102.66 | −102.21 |
| 5 | −110.46 | −105.45 | −102.48 | −102.11 |
Compiled from Table 2 of the paper, OMNIGLOT.
On OMNIGLOT, the five-layer LVAE's −102.11 is the best result, and also above the best figure in the IWAE paper, −103.38; that IWAE used two latent layers of 100-50 and trained with 50 importance-weighted samples on top of that.
Read the table from left to right, though, and the two biggest jumps come from BN (five layers going from −110.46 to −105.45) and warm-up (on to −102.48). The LVAE adds only 0.37 nats on top of that.
| Layers | VAE | +BN | +BN+WU | LVAE |
|---|---|---|---|---|
| 1 | 2741 | 3198 | 3338 | − |
| 2 | 2792 | 3224 | 3483 | 3272 |
| 3 | 2786 | 3235 | 3492 | 3519 |
| 4 | 2689 | 3201 | 3482 | 3449 |
| 5 | 2654 | 3198 | 3422 | 3455 |
Compiled from Table 2 of the paper, NORB.
The NORB numbers are positive because NORB uses a continuous Gaussian observation model; a probability density can exceed 1, so the log-likelihood can naturally be positive, and higher is still better. The LVAE does best at three layers, 27 above the VAE's best; but the two-layer LVAE is actually 211 below VAE+BN+WU. No model improves beyond three layers. The authors believe the Gaussian observation model is harder to optimize than the Bernoulli one, which may be why the upper layers get used less.
Three things that can't be pulled apart
Looking back over these experiments, one combination never shows up. Every LVAE is LVAE+BN+WU; there is no LVAE without warm-up, and none without BN. A figure caption in the appendix does slip in one sentence saying that the LVAE without BN performed very poorly, with some results falling outside the plot's range; but that line isn't drawn, and no numbers are given.
So we know LVAE with BN and warm-up is very good, and we know BN and warm-up help the VAE a lot by themselves, but we don't know how much the precision-weighting trick carries on its own. On OMNIGLOT the answer looks like not much. That is probably also why the first version's title listed all three side by side.
The official code
The official repo is written in Theano and Lasagne, and its README includes one command for the VAE and one for the LVAE.
Figure 1 of the paper draws "shared" between the two arrows of inference and generation, and the text stresses that the inference and generative models share information and parameters. Yet the README's LVAE command reads -ladder_share_params False (an arrow drawn in the figure, switched off on the command line). As the code is written, this setting gives the second inference pass a separate top-down MLP of its own, and the merge uses the mean and variance that MLP computes rather than the generative model's and .
Someone asked in issue #1 whether the paper's results used the default parameters. The author answered on October 10, 2016 that the results in the NIPS version of the paper had been computed with slightly different code, "I'll hopefully get it online soon," and that the default parameters probably wouldn't give the paper's numbers. The repo's last commit is dated March 28, 2016, more than half a year before that reply (that "soon" is now ten years old). The next day the author added that the repo version and the paper's results should be within about 0.2 nats of each other; nobody in the issue reported verifying that number.
Conclusion
Ladder VAE changes only the inference model. One pass up computes each layer's evidence from the data, and one pass down combines it with the generative model's prior by precision weighting, so the approximate posterior starts from the "prior" and gets corrected outward according to how confident the data is. On MNIST it puts all five layers of latent variables to work, with 2.26 nats above the best VAE and 0.62 nats above it; on OMNIGLOT, most of the gain actually comes from BN and warm-up; on NORB, nobody gets past three layers.
When training a hierarchical VAE, if the upper layers' KL stays at 0, first plot each unit's KL the way Figure 4 does and see whether the units collapsed at the start or were pruned gradually, then decide whether to add warm-up, change the inference structure, or just stack fewer layers. This paper's experience is that units that collapse at the start almost never wake up on their own later.
Precision weighting itself was rarely copied afterward; what stuck was the top-down order of inference. Four years later, NVAE had the approximate posterior predict a relative shift and scale with the prior as its reference; VDVAE says outright that the top-down VAE it uses was introduced by Sønderby et al. In December 2020, a newcomer on VDVAE's GitHub couldn't tell encoder features from latent variables, and the advice VDVAE's author, Child, gave was to read the Ladder VAE paper first.