Variational Autoencoder (VAE)
A latent-variable generative model trained by maximizing a variational lower bound on the data likelihood, with an amortized encoder and the reparameterization trick.
A variational autoencoder (VAE) is a latent-variable generative model. A latent code is drawn from a fixed prior and decoded into data through a conditional decoder . Exact maximum likelihood is intractable because the marginal has no closed form. The VAE introduces an encoder that approximates the true posterior and trains both networks jointly by variational inference. The encoder and decoder form a single forward path: an input is compressed into a low-dimensional code and then expanded back into a reconstruction, with the whole path trained end to end under one ELBO objective. The construction was introduced by Kingma and Welling in 2013 and remains a standard baseline for deep latent-variable modeling.
Think of a VAE as a compressor with a bottleneck, except the bottleneck holds not a string of fixed bits but a cloud of probability. The encoder glances at a datum and answers “roughly which region of latent space does it fall in” — a mean and a width. The decoder draws one point from that cloud and tries to rebuild . Training forces two things at once: the drawn point must reconstruct (the code has to carry information), yet all the codes stacked together must cover the whole prior (no holes in latent space). The first makes the code useful; the second makes the space sampleable — after training, draw straight from , decode it, and you get a new datum.
From a 2D latent space to generated samples — drag the cursor to decode:
Drag through the latent space and the output morphs smoothly — the essence of a VAE: a continuous low-dimensional code that the decoder maps to data, so nearby z decode to similar samples. (The decoder here is illustrative, not trained.)
The objective is the evidence lower bound (ELBO):
The first term is a reconstruction term: codes sampled from the encoder should decode back to . Here means “average under the code distribution the encoder produces for this ,” and is the log-likelihood the decoder assigns to the true datum — for a Gaussian decoder with fixed variance this reduces to a negative squared reconstruction error. The second is a Kullback–Leibler penalty: measures how far the per-sample posterior strays from the prior , pulling each code cloud toward the unit Gaussian centered at the origin. The gap between and the ELBO is exactly , so tightening the bound also sharpens the posterior approximation.
A concrete number: take a one-dimensional latent and suppose the encoder outputs for some . The KL between a diagonal Gaussian and the unit normal has the closed form , which evaluates to nats — the price paid for moving this code away from the prior. If the encoder retreats to , the KL drops to zero, but the code then carries no information about . Reconstruction and KL tug against each other along this axis.
From evidence to the bound
The bound follows from a single identity. Multiplying and dividing inside the log marginal by and applying Jensen’s inequality to the resulting expectation gives the ELBO directly; equivalently, the log evidence decomposes exactly as
The final term — the KL gap between the approximate posterior and the true posterior — is non-negative and unobservable (the true posterior is itself intractable), which is what makes the ELBO a lower bound rather than the evidence itself. Maximizing the ELBO over shrinks this gap; maximizing it over raises the evidence. Two distinct losses thus arise from one quantity: the encoder is pushed toward the true posterior, and the decoder toward higher likelihood. The identity also explains why maximizing the bound is safe — the bound differs from the truth by a KL we are always shrinking and can never drive negative.
The two terms pull in opposite directions. Reconstruction rewards an encoder that packs distinct inputs into well-separated codes; the KL term rewards an encoder whose outputs blur into the prior. Balancing them yields a latent space that is both informative and smoothly sampleable.
Optimizing the ELBO requires gradients of an expectation over , whose parameters are themselves being trained — and there is no direct way to differentiate through “a sample whose distribution depends on those parameters.” The reparameterization trick removes the sampling from the gradient path: with a Gaussian encoder , a sample is written
so the randomness lives in alone and passes through by the chain rule. Here is the center of the code cloud, its width, an elementwise product, and a noise source independent of the parameters. Once the noise is externalized as , gradients flow through the sampling as through any deterministic operation, training the whole path by ordinary backpropagation.
The encoder is amortized: a single network maps any to its variational parameters, rather than optimizing a separate posterior per data point. This efficiency carries a cost, splittable into two gaps. The amortization gap is the difference between the ELBO achieved by the shared encoder and the best ELBO attainable by optimizing a free posterior for each independently; a network that must generalize across all inputs cannot match per-point optimization everywhere. The approximation gap is the error incurred because the chosen variational family — a diagonal Gaussian, say — may simply be unable to represent the true posterior; even with per-point optimization, a diagonal Gaussian cannot fit a correlated or multimodal posterior. The two compound to set how far the ELBO sits below the true evidence: a more expressive family (e.g. a normalizing-flow posterior) shrinks the approximation gap, while a stronger encoder or test-time posterior refinement shrinks the amortization gap.
Posterior collapse and the -VAE
A failure mode peculiar to the VAE is posterior collapse: for some latent coordinates the encoder reverts to the prior, , so the KL term for those coordinates vanishes and they carry no information about . This occurs when a sufficiently expressive decoder can reconstruct the data without consulting the latent code — an autoregressive decoder is the classic case, filling in from what it has already generated and treating the latent as an optional side channel — leaving the KL penalty as the only active gradient for those dimensions, which then pushes them back to the prior. The reconstruction stays acceptable while the latent representation degrades into noise.
Reweighting the two terms exposes the trade-off directly. The -VAE scales the KL term by a coefficient ,
recovering the standard ELBO at . The coefficient is the weight on the KL term: strengthens the pull toward the prior, relaxes it. Larger tightens the latent toward the factorized prior, which tends to encourage disentanglement — individual coordinates aligning with independent factors of variation — at the expense of reconstruction fidelity. The KL term measures the rate, the average number of nats the code carries about , and the reconstruction term measures the distortion; traces out a rate–distortion curve along which posterior collapse is the extreme of vanishing rate (large presses every coordinate back to the prior), while degenerates to an ordinary autoencoder with no latent constraint.
Where it sits in Cryo-ET reconstruction
The VAE is the entry point for recasting missing-wedge restoration as a generative problem. It supplies three parts that later methods reuse repeatedly: amortized inference of a latent through the encoder, the ELBO that swaps an intractable likelihood for an optimizable lower bound, and reparameterization that makes a stochastic path differentiable. But the VAE’s per-sample KL inflates each posterior toward the prior and tends to blur reconstructions — a real problem when the goal is high-resolution structure. The later routes branch from exactly here: the Wasserstein autoencoder replaces the per-sample KL with a divergence on the aggregated posterior, often producing sharper samples and connecting to the optimal-transport route taken by CryoGEN-II (WAE/OT for a stable single answer); and the need to upgrade a single point reconstruction into a family of posterior reconstructions points to CryoWGEN. Reading the ELBO and its two gaps is what lets you see which constraint each of these methods is loosening.