Simple Algorithm
Deep Learning · Emergence · 28 min

Very Deep Hierarchical VAEs

Latent hierarchies, NVAE and why very deep VAEs rival autoregressive models.

A VAE with one layer of latents blurs. Stack dozens of latent layers, let each be generated from the ones above it, and the same objective produces sharp faces at 1024×10241024\times1024 and likelihoods better than PixelCNN's. A short argument shows why depth matters, a handful of stability tricks show what it costs, and diffusion models turn out to be the same idea with the encoder frozen.

What you’ll learn

  • Write the generative and inference models of a hierarchical VAE, and prove that its ELBO splits into one KL term per layer.
  • Explain top-down inference, the residual parameterization of NVAE and the stabilizers deep hierarchies need: spectral regularization, gradient skipping, KL warm-up, KL balancing and free bits.
  • Prove that a VAE with one latent layer per data dimension can represent any autoregressive model, and assess what VDVAE's results show and do not show.
  • Map a diffusion model onto a hierarchical VAE with a fixed encoder, and state what the continuous-time limit implies for the noise schedule.

Hierarchies of latents

DerivationProof
  1. log⁡qϕ(z1:L∣x)−log⁡pθ(z1:L)=∑l=1L[log⁡qϕ(zl∣z>l,x)−log⁡pθ(zl∣z>l)]\log q_{\boldsymbol\phi}(\mathbf z_{1:L}\mid\mathbf x)-\log p_{\boldsymbol\theta}(\mathbf z_{1:L})=\sum_{l=1}^{L}\Big[\log q_{\boldsymbol\phi}(\mathbf z_l\mid\mathbf z_{>l},\mathbf x)-\log p_{\boldsymbol\theta}(\mathbf z_l\mid\mathbf z_{>l})\Big]

    The log-ratio of the two factorizations is a sum over layers:

  2. Eq[log⁡qϕ(zl∣⋅)−log⁡pθ(zl∣⋅)]=Eq(z>l∣x) DKL(qϕ(zl∣z>l,x) ∥ pθ(zl∣z>l))\E_q\big[\log q_{\boldsymbol\phi}(\mathbf z_l\mid\cdot)-\log p_{\boldsymbol\theta}(\mathbf z_l\mid\cdot)\big]=\E_{q(\mathbf z_{>l}\mid\mathbf x)}\,\KL\big(q_{\boldsymbol\phi}(\mathbf z_l\mid\mathbf z_{>l},\mathbf x)\,\|\,p_{\boldsymbol\theta}(\mathbf z_l\mid\mathbf z_{>l})\big)

    Take the expectation under qq. Term ll depends only on z≥l\mathbf z_{\ge l}, so the groups below it integrate out, and sampling z>l\mathbf z_{>l} first and then zl\mathbf z_l turns the inner expectation into a KL:

  3. Summing gives the total KL of the ELBO, DKL(qϕ(z1:L∣x) ∥ pθ(z1:L))\KL(q_{\boldsymbol\phi}(\mathbf z_{1:L}\mid\mathbf x)\,\|\,p_{\boldsymbol\theta}(\mathbf z_{1:L})), layer by layer: the chain rule for KL divergence. Had qq been factored bottom-up (first z1\mathbf z_1 from x\mathbf x, then z2\mathbf z_2 from z1\mathbf z_1, and so on), the orders would disagree and no such layerwise Gaussian KLs would exist; the bound would have to be estimated entirely by sampling.

The top-down order has a second benefit: the posterior over zl\mathbf z_l really does depend on the groups above it, because they define its prior. Top-down inference lets qq reuse the generative model's own computation. A single deterministic bottom-up pass extracts features of x\mathbf x at every resolution; then a top-down pass samples the groups in generative order, and at each layer the encoder combines the prior's state with the bottom-up features of the same resolution. The Ladder VAE (Sønderby et al., 2016) combined them by precision weighting, the product of a Gaussian "likelihood" from the bottom-up pass, N(μ^,σ^2)\mathcal N(\hat{\boldsymbol\mu},\hat{\boldsymbol\sigma}^2), with the prior N(μp,σp2)\mathcal N(\boldsymbol\mu_p,\boldsymbol\sigma_p^2):

σq2=(σ^−2+σp−2)−1,μq=σq2⊙(μ^⊙σ^−2+μp⊙σp−2).\boldsymbol\sigma_q^{2}=\big(\hat{\boldsymbol\sigma}^{-2}+\boldsymbol\sigma_p^{-2}\big)^{-1},\qquad\boldsymbol\mu_q=\boldsymbol\sigma_q^{2}\odot\big(\hat{\boldsymbol\mu}\odot\hat{\boldsymbol\sigma}^{-2}+\boldsymbol\mu_p\odot\boldsymbol\sigma_p^{-2}\big).

The ResNet VAE of Kingma et al. (2016) used the same bidirectional scheme with inverse autoregressive flows on each layer's posterior, and every deep hierarchical VAE since has kept it.

Predict before you look

A hierarchical VAE for 64×6464\times64 color images has four latent groups at each of three resolutions, 8×88\times8, 16×1616\times16 and 32×3232\times32, each group with 20 channels. How many latent dimensions does it have in total?

Show the answer

107520 dimensions

4×20×(64+256+1024)=80×1344=107,5204\times20\times(64+256+1024)=80\times1344=107{,}520, almost nine times the 64⋅64⋅3=12,28864\cdot64\cdot3=12{,}288 numbers in the image. Deep hierarchical VAEs are not bottlenecks: compression comes from the KL terms, which charge for every nat a layer carries, so most dimensions end up close to their priors.

Making depth trainable: NVAE

Deep hierarchies were long unstable: KL terms blew up, groups collapsed onto their priors, and gradients spiked. NVAE (Vahdat & Kautz, 2020) put together a recipe that made dozens of groups train reliably on natural images, and was the first VAE reported to work well at 256×256256\times256. Its central change is to parameterize each posterior relative to its prior:

p(zli∣z>l)=N(μi,σi2),q(zli∣z>l,x)=N(μi+Δμi, σi2Δσi2)⟹DKL=12(Δμi2σi2+Δσi2−log⁡Δσi2−1)p(z^i_l\mid\mathbf z_{>l})=\mathcal N(\mu_i,\sigma_i^2),\quad q(z^i_l\mid\mathbf z_{>l},\mathbf x)=\mathcal N(\mu_i+\Delta\mu_i,\ \sigma_i^2\Delta\sigma_i^2)\quad\Longrightarrow\quad\KL=\tfrac12\Big(\frac{\Delta\mu_i^2}{\sigma_i^2}+\Delta\sigma_i^2-\log\Delta\sigma_i^2-1\Big)
The KL follows from the standard Gaussian formula after shifting by μi\mu_i and scaling by σi\sigma_i, which leave a KL unchanged. When the prior moves during training, the encoder's relative outputs Δμi,Δσi\Delta\mu_i,\Delta\sigma_i need not chase it, and with σi\sigma_i bounded below the KL is controlled by the encoder alone.

The rest of the recipe targets the instabilities one at a time:

  • Spectral regularization, a penalty λ∑is(i)\lambda\sum_is^{(i)} on the largest singular value of every layer (estimated with one power-iteration step per update), limits how sharply the encoder's outputs, and so the KL terms, can change with the input. Vahdat & Kautz found it key to stable training.
  • Batch normalization with care: its running statistics at evaluation time differ from the batch statistics used in training, and deep stacks amplify the mismatch, so NVAE adjusts the momentum and regularizes the norm of the BN scale parameters.
  • Cheap, wide generative cells: depthwise-separable convolutions with an expansion factor of 6, Swish activations and squeeze-and-excitation give large receptive fields at low cost in the top-down path.
  • KL warm-up and balancing: the KL weight ramps from 0 to 1 over the first 30% of training, and during warm-up each group's KL is reweighted in proportion to its size times its average KL, so that no group is starved into collapse.

With these, NVAE reached 2.91 bits per dimension on CIFAR-10, the best reported among non-autoregressive likelihood models at the time, against 2.98 before it.

Why depth: VAEs that contain autoregressive models

DerivationProof
  1. Under qq the latents copy the data, z=x\mathbf z=\mathbf x, so the reconstruction term is log⁡1[x=x]=0\log\mathbb 1[\mathbf x=\mathbf x]=0.

  2. DKL(q ∥ p)=∑i=1NEq[log⁡q(zi∣⋅)−log⁡pAR(zi∣z<i)]=−∑i=1Nlog⁡pAR(xi∣x<i)\KL(q\,\|\,p)=\sum_{i=1}^N\E_q\big[\log q(z_i\mid\cdot)-\log p_{\text{AR}}(z_i\mid z_{<i})\big]=-\sum_{i=1}^N\log p_{\text{AR}}(x_i\mid x_{<i})

    Each posterior is a point mass, so log⁡q(zi∣⋅)=0\log q(z_i\mid\cdot)=0 at the sample, and by the layerwise decomposition each KL is minus the prior's log-probability of the copied value:

  3. Hence L(x)=0+∑ilog⁡pAR(xi∣x<i)=log⁡pAR(x)\mathcal L(\mathbf x)=0+\sum_i\log p_{\text{AR}}(x_i\mid x_{<i})=\log p_{\text{AR}}(\mathbf x). The bound is tight because the point-mass posterior is the true posterior of this model. ■\blacksquare

The construction is degenerate on purpose: the prior does all the work, one pixel per layer, and the decoder only copies. Its lesson is that an autoregressive model is a VAE with a very particular, very long hierarchy. Child's argument is that images should admit far shorter hierarchies (layout, then parts, then texture, with many dimensions generated in parallel at each layer), which a VAE can learn and a pixel-by-pixel model cannot exploit. The theorem guarantees expressivity, not that gradient descent will find a good hierarchy.

VDVAE (Child, 2021) tested this with hierarchies of up to 78 stochastic layers, at resolutions from 1×11\times1 up to the image's, built from bottom-up and top-down residual blocks. Two stabilizers mattered. Gradient skipping discards any update whose gradient norm exceeds a threshold set so that fewer than 0.01% of updates are skipped; without it, rare spikes with norms as large as about 101510^{15} derailed training. And the last convolution of each residual block is initialized scaled by 1/N1/\sqrt N for network depth NN, so the residual stream does not grow with depth. Child reports that with this architecture he needed neither free bits nor KL warm-up.

Bits per dimension (lower is better)CIFAR-10ImageNet 32×3232\times32ImageNet 64×6464\times64
VDVAE (Child, 2021)2.873.803.52
PixelCNN family (PixelCNN++ on CIFAR-10, gated PixelCNN on ImageNet)2.923.833.57
Best autoregressive transformer listed in the paper2.803.773.44
Test negative log-likelihoods as reported by Child (2021); VDVAE's entries are upper bounds computed from its ELBO, the autoregressive models' are exact. VDVAE beat the PixelCNN family while sampling in one pass, but not the best autoregressive transformers. On FFHQ-256 the first few low-resolution layers, under 1% of the latents, largely fixed each face's global structure. These numbers measure likelihood and speed; they say little about perceptual sample quality (see Evaluating Generative Models).
Check your understanding

In the construction that turns any autoregressive model into an NN-layer VAE, which part of the VAE plays the autoregressive model's role?

  1. The decoder p(x∣z)p(\mathbf x\mid\mathbf z)
  2. The posterior q(z∣x)q(\mathbf z\mid\mathbf x)
  3. The prior p(zi∣z<i)p(z_i\mid z_{<i}) over the latent layers
  4. The KL weight β\beta
Show the answer

C. The prior p(zi∣z<i)p(z_i\mid z_{<i}) over the latent layers

The latents copy the data, so the prior over latents must assign them exactly the autoregressive model's probabilities; the ELBO's KL terms then sum to −log⁡pAR(x)-\log p_{\text{AR}}(\mathbf x).

Diffusion models as hierarchical VAEs

A DDPM (Ho, Jain & Abbeel, 2020) is a hierarchical VAE whose encoder is frozen. Its latents are the noisy images x1,…,xT\mathbf x_1,\dots,\mathbf x_T (here t=1,…,Tt=1,\dots,T counts discrete diffusion steps, running from the data x0\mathbf x_0 toward nearly pure noise xT\mathbf x_T), produced by a fixed Markov encoder q(xt∣xt−1)=N(1−βt xt−1,βtI)q(\mathbf x_t\mid\mathbf x_{t-1})=\mathcal N(\sqrt{1-\beta_t}\,\mathbf x_{t-1},\beta_tI), and its generative model is the top-down chain p(xT)∏tpθ(xt−1∣xt)p(\mathbf x_T)\prod_tp_{\boldsymbol\theta}(\mathbf x_{t-1}\mid\mathbf x_t). The encoder runs bottom-up, but Bayes' rule rewrites it in generative order, q(x1:T∣x0)=q(xT∣x0)∏t≥2q(xt−1∣xt,x0)q(\mathbf x_{1:T}\mid\mathbf x_0)=q(\mathbf x_T\mid\mathbf x_0)\prod_{t\ge2}q(\mathbf x_{t-1}\mid\mathbf x_t,\mathbf x_0), and the layerwise ELBO of the first step appears term for term:

log⁡pθ(x0) ≥ Eq[log⁡pθ(x0∣x1)]−DKL(q(xT∣x0) ∥ p(xT))−∑t=2TEq[DKL(q(xt−1∣xt,x0) ∥ pθ(xt−1∣xt))]\log p_{\boldsymbol\theta}(\mathbf x_0)\ \ge\ \E_q\big[\log p_{\boldsymbol\theta}(\mathbf x_0\mid\mathbf x_1)\big]-\KL\big(q(\mathbf x_T\mid\mathbf x_0)\,\|\,p(\mathbf x_T)\big)-\sum_{t=2}^{T}\E_q\Big[\KL\big(q(\mathbf x_{t-1}\mid\mathbf x_t,\mathbf x_0)\,\|\,p_{\boldsymbol\theta}(\mathbf x_{t-1}\mid\mathbf x_t)\big)\Big]
Reconstruction, a prior term with no parameters, and one KL per layer between two Gaussians. The Artificial Intelligence course's Deep Layer lesson on diffusion evaluates each term and shows that it reduces to a weighted noise-prediction loss.
AspectLearned hierarchical VAEDiffusion model
Encoderlearned, top-down, can collapsefixed noise injection: every layer always carries information
Latentstens of groups, multi-resolution, can compressTT copies of data-sized noisy images
Generative layersseparate weights per layerone network shared across steps, told the step tt
Per-layer taskgenerate a whole group of featuresremove a small amount of noise
Samplingone passone pass per step
Freezing the encoder gives up compression and a learned representation, and buys a trivially stable objective with an easy, shared task at every layer.

Variational diffusion models (Kingma, Salimans, Poole & Ho, 2021) took the hierarchy to its continuous-time limit, T→∞T\to\infty. Their latents are zt=αtx+σtϵ\mathbf z_t=\alpha_t\mathbf x+\sigma_t\boldsymbol\epsilon for t∈[0,1]t\in[0,1], and time runs as in DDPM, from nearly clean data at t=0t=0 to nearly pure noise at t=1t=1: the reverse of the flow-matching convention of Continuous Flows & Flow Matching, where noise sits at t=0t=0. With signal-to-noise ratio SNR(t)=αt2/σt2\mathrm{SNR}(t)=\alpha_t^2/\sigma_t^2, decreasing in tt, the diffusion part of the bound becomes

L∞(x)=−12 Eϵ∼N(0,I)∫01SNR′(t) ∥x−x^θ(zt;t)∥2 dt,\loss_\infty(\mathbf x)=-\tfrac12\,\E_{\boldsymbol\epsilon\sim\mathcal N(\mathbf 0,I)}\int_0^1\mathrm{SNR}'(t)\,\norm{\mathbf x-\hat{\mathbf x}_{\boldsymbol\theta}(\mathbf z_t;t)}^2\,dt,

and the change of variables v=SNR(t)v=\mathrm{SNR}(t) shows that it depends on the noise schedule only through its endpoints SNR(0)\mathrm{SNR}(0) and SNR(1)\mathrm{SNR}(1). The schedule in between affects only the variance of the Monte Carlo estimate of the bound, so VDM learned it to minimize that variance, and reported state-of-the-art likelihoods on standard image benchmarks at the time. An infinitely deep hierarchy, then, is not a liability when every layer is a small, shared denoising step.

Key takeaways

  • A hierarchical VAE generates latent groups top-down and infers them in the same order; its ELBO then splits into one closed-form Gaussian KL per group, averaged over the groups above.
  • Top-down inference combines one bottom-up feature pass with the generative pass; NVAE adds posteriors relative to their priors, spectral regularization, careful batch normalization, KL warm-up and KL balancing.
  • Groups that the decoder can do without collapse to their priors; warm-up and free bits, which charge each group jj at least λ\lambda nats, max⁡(λ,KLj)\max(\lambda,\mathrm{KL}_j), keep them in use.
  • A VAE with one latent layer per dimension and copying posteriors contains every autoregressive model (Child, 2021); VDVAE's 78-layer hierarchies beat PixelCNN likelihoods with one-pass sampling, stabilized by gradient skipping and depth-scaled initialization.
  • A diffusion model is a hierarchical VAE with a fixed encoder and a shared denoiser; in continuous time its bound depends on the noise schedule only through the endpoint SNRs (Kingma et al., 2021).

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

In a hierarchical VAE whose prior and posterior are both factored top-down, what does the KL part of the ELBO become?

  1. ∑lDKL(qϕ(zl∣x) ∥ N(0,I))\sum_l\KL\big(q_{\boldsymbol\phi}(\mathbf z_l\mid\mathbf x)\,\|\,\mathcal N(\mathbf 0,I)\big), one standard-normal KL per group
  2. ∑lEq(z>l∣x)DKL(qϕ(zl∣z>l,x) ∥ pθ(zl∣z>l))\sum_l\E_{q(\mathbf z_{>l}\mid\mathbf x)}\KL\big(q_{\boldsymbol\phi}(\mathbf z_l\mid\mathbf z_{>l},\mathbf x)\,\|\,p_{\boldsymbol\theta}(\mathbf z_l\mid\mathbf z_{>l})\big)
  3. Only the KL of the top group, DKL(qϕ(zL∣x) ∥ p(zL))\KL(q_{\boldsymbol\phi}(\mathbf z_L\mid\mathbf x)\,\|\,p(\mathbf z_L))
  4. It has no closed form at any layer and must be estimated by sampling every group
Show the answer

B. ∑lEq(z>l∣x)DKL(qϕ(zl∣z>l,x) ∥ pθ(zl∣z>l))\sum_l\E_{q(\mathbf z_{>l}\mid\mathbf x)}\KL\big(q_{\boldsymbol\phi}(\mathbf z_l\mid\mathbf z_{>l},\mathbf x)\,\|\,p_{\boldsymbol\theta}(\mathbf z_l\mid\mathbf z_{>l})\big)

By the chain rule for KL divergence, the joint KL is a sum of conditional KLs, each averaged over the groups above. Given those groups, each conditional KL is between two Gaussians and has a closed form.

Question 2

NVAE writes a posterior as N(μ+Δμ, σ2Δσ2)\mathcal N(\mu+\Delta\mu,\ \sigma^2\Delta\sigma^2) relative to the prior N(μ,σ2)\mathcal N(\mu,\sigma^2). If the encoder outputs Δμ=0\Delta\mu=0 and Δσ=1\Delta\sigma=1, what is that group's KL?

  1. 12(μ2+σ2−log⁡σ2−1)\tfrac12(\mu^2+\sigma^2-\log\sigma^2-1)
  2. log⁡2\log2
  3. 00: the posterior equals the prior, so the group carries no information about x\mathbf x
  4. It depends on x\mathbf x through μ\mu and σ\sigma
Show the answer

C. 00: the posterior equals the prior, so the group carries no information about x\mathbf x

12(0/σ2+1−log⁡1−1)=0\tfrac12(0/\sigma^2+1-\log1-1)=0. The relative parameterization makes "say nothing about x\mathbf x" the encoder's neutral output, whatever the prior currently is.

Question 3

Viewed as a hierarchical VAE, how does a DDPM differ from NVAE or VDVAE?

  1. Its latents are lower-dimensional than the data, so it compresses more
  2. Its encoder is a fixed noise-adding process, so only the generative steps are learned and no layer can collapse to its prior
  3. It infers its latents bottom-up, so its ELBO has no layerwise KL terms
  4. It uses a separate network for every layer
Show the answer

B. Its encoder is a fixed noise-adding process, so only the generative steps are learned and no layer can collapse to its prior

The forward process has no parameters: every noisy image necessarily carries information about x0\mathbf x_0, and training fits only the reverse steps, with one network shared across tt. Bayes' rule rewrites the encoder in top-down order, so the ELBO still splits into one Gaussian KL per step.

Further reading

  • C. K. Sønderby, T. Raiko, L. Maaløe, S. K. Sønderby & O. Winther, "Ladder variational autoencoders," NeurIPS (2016).
  • D. P. Kingma, T. Salimans, R. Jozefowicz, X. Chen, I. Sutskever & M. Welling, "Improved variational inference with inverse autoregressive flow," NeurIPS (2016).
  • A. Vahdat & 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).
  • D. P. Kingma, T. Salimans, B. Poole & J. Ho, "Variational diffusion models," NeurIPS (2021).