Foundations to Frontier
Tests
H. Generative Models › H3 Variational Autoencoders

Hierarchical VAEs: Ladder VAE and NVAE

A hierarchical VAE stacks several layers of latent variables, z_L → … → z_1 → x, so the prior over the lower layers is itself learned and can be far richer than a single standard Gaussian. Training deep hierarchies used to fail: upper layers went unused and optimisation was unstable. The Ladder VAE (Sønderby and colleagues, 2016) fixed inference by running it top-down, sharing the generative path and merging each layer's prior with a bottom-up message by precision weighting. NVAE (Vahdat and Kautz, 2020) and Very Deep VAEs (Child, 2021) scaled the idea to dozens of latent groups with residual cells, a residual parameterisation of the posterior, spectral regularisation and KL warm-up, reaching likelihoods competitive with autoregressive models. This page covers why hierarchy helps, bottom-up versus top-down inference, the precision-weighted merge, the NVAE tricks and the per-layer KL. In the exercises you implement the merge and the residual parameterisation. You then study a two-layer model whose top layer modulates the variance of the layer below, producing heavy tails. With strong modulation the hierarchy beat the best single Gaussian layer by 0.49 nats per point. Top-down inference was 7 times tighter than a factorised posterior (gaps 0.015 against 0.100 nats). When the top layer was not needed, it carried no information at all.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Latents that explain other latents

A single layer of latent variables with a standard bell-curve prior is a rigid starting point. A hierarchical VAE adds layers: a top layer that decides broad things, such as how much variation to expect, and lower layers that fill in details given those decisions. In images the top layers pick layout and the bottom layers pick texture. The richer prior lets the model capture data a single layer cannot, such as rare large values (heavy tails). The demo shows how one layer controlling the spread of another turns a bell curve into a sharp peak with long tails.

VAE lab
Kurtosis of z1 = 3e^(a²) = 8.2 (3 for a Gaussian). On a log scale the two-layer density (green) has a sharper peak and far heavier tails than the Gaussian of equal variance (yellow).
Why engineers care

Deep hierarchical VAEs (NVAE, VDVAE) give likelihood-based image models with fast sampling, and hierarchical latents structure world models and multi-scale compression of Earth-observation imagery.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A hierarchical VAE has…
  2. 2. Top layers usually capture…
  3. 3. A richer prior can model…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Generative model: p(z_L) p(z_(L−1) | z_L) … p(z_1 | z_2) p(x | z_1); in NVAE every group conditions on all groups above.
  • ELBO: E_q[log p(x | z)] − Σ_l E_q[KL(q(z_l | ·) ‖ p(z_l | z_(>l)))]: the rate splits into one KL per layer.
  • Bottom-up inference (the first hierarchical VAEs): q(z_1 | x) q(z_2 | z_1) …, a separate encoder path.
  • Top-down inference (Ladder VAE): a deterministic bottom-up pass computes messages (μ̂_l, σ̂_l²) from x; then, going down, each layer’s posterior merges the top-down prior with its message:
    • σ_q² = 1 / (1/σ_p² + 1/σ̂²), μ_q = σ_q² (μ_p/σ_p² + μ̂/σ̂²).
    • Prior N(0, 1) with message N(2, 1) gives N(1, 0.5); prior N(0, 4) with message N(1, 0.05) gives variance 1/(0.25 + 20) = 0.0494: the sharp message dominates.
  • Residual parameterisation (NVAE): q = N(μ_p + Δμ, (σ_p · Δσ)²), so KL = ½(Δμ²/σ_p² + Δσ² − 1 − ln Δσ²), zero at Δμ = 0, Δσ = 1 whatever the prior.
  • Example of a richer prior: z_2 ~ N(0, 1), z_1 | z_2 ~ N(0, e^(a z_2)). z_1 has kurtosis 3e^(a²): 8.15 at a = 1, 163.8 at a = 2 (a Gaussian has 3).
📝 Quick check (Level 1)
0/3 answered
  1. 1. Precision-weighted merging adds…
  2. 2. Merge of N(0, 1) and N(2, 1):
  3. 3. The residual KL is zero when…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Building hierarchical VAEs

Exercise 1.

  • The Ladder VAE precision-weighted merge.
  • NVAE’s residual normal parameterisation and its KL.
  • A check against the direct Gaussian KL.

Practice.

  • Use top-down inference that shares the decoder path; bottom-up-only hierarchies leave upper layers unused.
  • Warm up the KL (β from 0 to 1 over the first 30% of training) and use KL balancing across groups so no group switches off early.
  • Stabilise with spectral regularisation (NVAE), gradient skipping or clipping on rare huge gradients (VDVAE), and residual parameterisation of posteriors.
  • Log the KL per group; groups with KL near zero are dead and can be pruned.
  • Sample at reduced temperature (scale prior standard deviations by 0.6–0.9) for cleaner images; report likelihood at temperature 1.
def topdown_group(h_dec, h_enc, prior_net, post_net, sample=True):
    # h_dec: top-down state; h_enc: bottom-up features at the same scale (None when sampling)
    mu_p, log_sig_p = prior_net(h_dec).chunk(2, 1)
    if h_enc is None:                                    # generation
        z = mu_p + log_sig_p.exp() * torch.randn_like(mu_p); return z, 0.0
    d_mu, d_log_sig = post_net(torch.cat([h_dec, h_enc], 1)).chunk(2, 1)
    mu_q, sig_q = mu_p + d_mu, (log_sig_p + d_log_sig).exp()   # NVAE residual normal
    z = mu_q + sig_q * torch.randn_like(mu_q)
    kl = 0.5 * (d_mu ** 2 / (2 * log_sig_p).exp() + (2 * d_log_sig).exp() - 1 - 2 * d_log_sig)
    return z, kl.flatten(1).sum(1)
🐍 Exercise 1: precision-weighted merge and residual posteriors
Implement the Ladder VAE merge, NVAE's residual parameterisation and its KL, and check it against the direct KL.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Upper layers going unused is fixed mainly by…
  2. 2. VDVAE handles rare huge gradients by…
  3. 3. Sampling at temperature below 1 means…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why top-down inference helps. The exact posterior of a hierarchy couples the layers: given x, what z_1 should be depends on z_2. A bottom-up or factorised q must guess z_1 without knowing which z_2 it is paired with. Top-down inference samples z_2 first and then forms q(z_1 | z_2, x) from the prior p(z_1 | z_2) and the data message, exactly as the true posterior factorises: p(z_1, z_2 | x) = p(z_2 | x) p(z_1 | z_2, x). When the message is Gaussian and the layer conditional is Gaussian, the precision-weighted merge is the exact conditional posterior.

Proof of the merge. p(z | prior) p(message | z) ∝ exp(−(z − μ_p)²/(2σ_p²) − (z − μ̂)²/(2σ̂²)). Collecting the z² and z terms gives a Gaussian with precision 1/σ_p² + 1/σ̂² and mean equal to the precision-weighted average of μ_p and μ̂.

Why the residual parameterisation is stable. When a group’s prior shifts during training, a posterior parameterised absolutely must chase it, and the KL can spike. Parameterising q relative to p keeps the KL a function of Δμ and Δσ only, measured in units of the prior’s scale.

Why hierarchies are expressive. Integrating out upper layers gives lower layers a continuous-mixture prior: here z_1 is a Gaussian scale mixture, which is heavy-tailed. Child (2021) showed a VAE with enough layers can represent any autoregressive model (each layer can copy one pixel), so very deep VAEs are at least as expressive as autoregressive models while sampling all pixels of a layer in parallel.

When a layer is unnecessary its KL goes to zero: the posterior equals the prior and the layer passes no information. That is correct behaviour, not a failure, and per-layer KL is the way to see it.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The exact posterior of a two-layer chain factorises as…
  2. 2. Integrating out a variance-modulating top layer gives…
  3. 3. Child (2021) showed very deep VAEs can represent…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

What does a second latent layer buy, and how much tighter is top-down inference?

History.

  • C. K. Sønderby, T. Raiko, L. Maaløe, S. K. Sønderby and O. Winther, “Ladder variational autoencoders” (NeurIPS 2016).
  • D. P. Kingma et al., “Improved variational inference with inverse autoregressive flow” (2016), introducing ResNet VAEs with top-down inference and free bits.
  • A. Vahdat and J. Kautz, “NVAE: a deep hierarchical variational autoencoder” (NeurIPS 2020); R. Child, “Very deep VAEs generalize autoregressive models and can outperform them on images” (ICLR 2021).

Exercise 2. A model where every quantity can be computed exactly, to isolate the effects of hierarchy and inference.

  • Model: z_2 ~ N(0, 1), z_1 | z_2 ~ N(0, e^(a z_2)), x | z_1 ~ N(z_1, 0.05), for a = 0 (top layer useless), 1 and 2; 1000 points sampled from it.
  • Compared: the best single Gaussian layer (the most a one-layer linear VAE with an N(0, 1) prior can reach); the exact two-layer log p(x) by quadrature over z_2; the ELBO with top-down inference (Gaussian q(z_2 | x), then the exact precision-weighted q(z_1 | z_2, x)); and the best factorised q(z_1) q(z_2) per point.
  • Per-layer KL under top-down inference shows how much each layer carries.
a kurtosis of x one-layer log-likelihood two-layer log p(x) top-down ELBO gap factorised ELBO gap KL top / bottom
0 2.81 −1.465 −1.465 0 0 0 / 1.543
1 8.87 −1.690 −1.615 0.0024 0.021 0.150 / 1.543
2 48.97 −2.363 −1.869 0.015 0.100 0.352 / 1.597
  • Hierarchy pays off only when the data need it: at a = 2 the two-layer model scored 0.494 nats per point above the best single Gaussian layer, at a = 1 0.075, and at a = 0 nothing.
  • Unneeded layers switch off by themselves: at a = 0 the top layer’s KL was exactly zero.
  • Top-down inference was 7 to 9 times tighter than the factorised posterior, because it lets z_1’s posterior depend on z_2 (a large z_2 means z_1 may be far from 0).
  • Seeds 1 and 2: gains at a = 2 of 0.619 and 0.645 nats; top-down gaps 0.0149 and 0.0146 against factorised 0.098 and 0.100; top-layer KL 0.353 and 0.36.
  • Caveat: these are inference comparisons on the true model, not trained networks; in deep VAEs top-down inference also matters because it makes optimisation of upper layers possible at all.
🐍 Exercise 2 (research): what does a second latent layer buy, and how much tighter is top-down inference?
Compare a single Gaussian layer with a two-layer variance-modulating prior, and top-down against factorised inference, using exact likelihoods.

Hierarchical VAEs in AI, robotics and aerospace

  • Likelihood-based image models: NVAE and VDVAE reach about 2.9 bits per dimension on CIFAR-10 and sample in one network pass per layer.
  • Learned compression: hyperprior image codecs (Ballé et al., 2018) are two-layer VAEs whose top layer predicts the scales of the lower one, exactly the variance modulation of the exercise; they underlie neural codecs studied for satellite imagery.
  • World models: hierarchical latent dynamics separate slow mission-level state from fast sensor-level detail.

References

  1. C. K. Sønderby et al. (2016). “Ladder variational autoencoders.” NeurIPS.
  2. D. P. Kingma et al. (2016). “Improved variational inference with inverse autoregressive flow.” NeurIPS.
  3. J. Ballé, D. Minnen, S. Singh, S. J. Hwang and N. Johnston (2018). “Variational image compression with a scale hyperprior.” ICLR.
  4. L. Maaløe, M. Fraccaro, V. Liévin and O. Winther (2019). “BIVA: a very deep hierarchy of latent variables for generative modeling.” NeurIPS.
  5. A. Vahdat and J. Kautz (2020). “NVAE: a deep hierarchical variational autoencoder.” NeurIPS.
  6. R. Child (2021). “Very deep VAEs generalize autoregressive models and can outperform them on images.” ICLR.
  7. M. J. Wainwright and E. P. Simoncelli (2000). “Scale mixtures of Gaussians and the statistics of natural images.” NeurIPS.