Skip to content
mlmentorship

Variational autoencoders: calculate the bound and its gap

Evaluate a complete Gaussian ELBO, improve the approximate posterior, and test why changing the KL weight changes the guarantee.

Published · 8 min read ·Role-specific ·Intermediate
Reading depth

Summary

A variational autoencoder fits a latent-variable model using approximate posterior inference. Its encoder estimates latent uncertainty for observed data; its decoder specifies an observation distribution, not merely a reconstructed mean.

Task: observe under and . Use approximate posterior , where the second argument denotes variance. Compute the training bound and compare it with the exact log-likelihood. This scalar teaching model is solvable; neural models often make the marginal integral difficult.

The model

Generally, and the encoder is . Parameters define the generative model; defines its approximate inference network. One encoder shares inference parameters across observations: amortization does not make each inferred posterior exact.

Read the training lane and generation lane as different computations.

One model, two execution paths

Generation samples the prior without an observed input or encoder

Generation has no observed input to encodeTraining infers z from observed x through the encoder. Generation instead samples z from the prior and decodes it without q(z|x).Generation reaches the decoder fromthe prior, not an encoderTrain: observed x is encoded intoq(z|x)xencodedecodeGenerate: no observed input; sampleprior zpriorzdecode
Derivation
VAE training and generation paths In the upper training panel, observed x enters the encoder, which produces approximate posterior q phi. A reparameterized latent z flows into the decoder and the reconstruction term. Beside that path, the KL term compares q phi with prior p of z. In the lower generation panel, a dashed path samples z directly from the prior, passes it through the decoder, and produces a distribution for a new observation. The encoder and training input do not participate in generation. TRAIN: INFER z FROM THIS x x observed encoder parameters φ qφ(z|x) μφ(x), σφ(x) p(z) prior N(0,I) KL regularizer z = μ + σε ε ∼ N(0,I) compare qφ to p decoder parameters θ reconstruction: log pθ(x|z) target x ELBO = reconstruction - KL GENERATE: SAMPLE z WITHOUT AN INPUT x p(z) sample z decoder same θ pθ(x|z) new x No observed x, encoder, qφ, or KL step.
observed x enters the encoder on the solid training path. A posterior sample reaches the decoder; reconstruction scores the conditional density of that same x. The KL term compares the approximate posterior with the prior. The dashed generation path instead samples the prior and uses the decoder without an observed x. Sources: Kingma and Welling, Rezende et al., and the VAE tutorial.
Check: Why does generation need neither an observed x nor q(z|x), unlike training?
Read it this way: Training infers z from observed x through the encoder. Generation instead samples z from the prior and decodes it without q(z|x).
Which latent source is used when no observed input exists?

The ELBO

For these everywhere-positive Gaussian densities, insert and apply Jensen’s inequality:

Call the final expression , the evidence lower bound, or ELBO. Bayes’ rule also gives the exact gap identity:

The penalty inside compares with the prior; the gap compares with the model posterior. These are different distributions.

For the fixture, . For a Gaussian encoder with mean and variance , the prior KL is .

Quantity, natural-log unitsCalculationValue
Expected reconstruction log density
Prior KL
ELBOReconstruction minus prior KL
Exact log-likelihood,

The exact posterior is . Directly computing its KL from gives , exactly the likelihood-minus-bound gap.

Improve inference variance

New task: change ‘s variance from to the exact posterior variance . Update both reconstruction and prior KL, not just the penalty. Check: reconstruction becomes , prior KL , and ELBO . The posterior gap is zero. Use when: the inference family can better represent the model posterior.

Change the KL weight

New task: keep the original but optimize reconstruction minus times prior KL, with . Check: the objective becomes , exceeding the exact log-likelihood . It is not the ordinary ELBO; its usual lower-bound guarantee cannot be asserted. Use when: deliberately changing a representation or rate-distortion tradeoff rather than claiming the same likelihood objective.

The reparameterization trick

Write , with parameter-independent . For log variance , use , not . The randomness remains; its sampling law no longer depends on . See the pathwise-gradient derivation for conditions and alternatives.

Reconstruct: which observed enters the encoder during unconditional generation?

Answer

None. Sample from the prior, then sample or summarize the decoder distribution . Reconstruction instead conditions on a supplied observation through the encoder.

What VAEs are good and bad at

Continuous latent models can support interpolation and learned compression, including latent representations used by downstream diffusion models. Their usefulness depends on the learned distribution and reconstruction contract, not continuity alone. Gaussian squared error corresponds to a specified decoder variance; it is not a universal reconstruction likelihood. Blur depends on that likelihood, uncertainty, architecture, and optimization, not simply the VAE name. No universal stability or sample-quality ranking follows against GANs or diffusion.

Common pitfalls

Posterior collapse means inferred latents carry little observation-specific information and the decoder can ignore them. An expressive autoregressive decoder can still produce varied outputs; collapse does not imply a constant image. Annealing, free bits, decoder changes, and inference improvements alter different parts of training and offer no universal cure. Not using a pathwise estimator does not uniquely force REINFORCE; other estimators depend on the latent family. Check likelihood sums, minibatch reductions, and KL units before interpreting their relative weight.

Variants

  • Beta-VAE: changes the KL weight; disentanglement is not guaranteed by that coefficient alone.
  • VQ-VAE: introduces discrete codebooks and a different training construction, not Gaussian reparameterization unchanged.
  • IWAE: uses an importance-weighted objective with its own support and sampling conditions.
  • NVAE and hierarchical VAEs: introduce multiple conditional latent levels, retaining a distinct factorization.

The IWAE bound is an expectation, not an exact likelihood correction. A single Monte Carlo realization need not lie below log-likelihood.