What is a variational autoencoder (VAE), and how does it differ from a plain autoencoder?
Answer
A variational autoencoder is a latent-variable generative model trained by amortized variational inference, not a compression network with a smaller bottleneck. A plain autoencoder learns a deterministic map and minimizes reconstruction error alone, so nothing constrains where codes land: the latent space is an arbitrary point cloud with holes, and decoding a random vector usually produces garbage. A VAE instead makes the encoder output the parameters of a distribution
, samples
from it, and optimizes the evidence lower bound (ELBO), which adds a KL term pulling every posterior toward a fixed prior
. That single extra term is what turns an encoder-decoder pair into a generator: after training you can draw
and decode it, because the aggregate posterior now covers the prior. The sampling step is made differentiable by the reparameterization trick,
with
, which keeps the stochastic node out of the gradient path.
(1) Probabilistic Encoder: the encoder emits a mean and a log-variance per latent dimension rather than a point, so each input maps to a small blob of latent space instead of a single coordinate.
(2) Two-Term Objective: the loss is reconstruction plus a KL regularizer against the prior; a plain autoencoder has only the first term, which is exactly why it is not generative.
(3) Reparameterization Trick: sampling is rewritten as a deterministic function of the parameters and an external noise draw, giving a low-variance pathwise gradient instead of a high-variance score-function estimator.
(4) Sampling From The Prior: because the KL term forces the posteriors to overlap and fill the prior, ancestral sampling (, then decode) yields plausible data, and interpolation between two codes stays on the data manifold.
(5) Explicit Likelihood Bound: the ELBO is a lower bound on , so a VAE gives a comparable density estimate, whereas an autoencoder’s reconstruction error has no probabilistic meaning.

Figure 1: The structural difference is one node: the plain autoencoder passes a single deterministic code to the decoder, while the VAE passes a sample from a learned Gaussian that the KL term keeps anchored to .
Two practical details dominate real training runs. The first is the balance between the two loss terms: with a strong decoder or a large KL weight, the cheapest solution is to set and ignore the latent entirely, a failure called posterior collapse that shows up as a KL term decaying to near zero while reconstruction stalls. Standard mitigations are KL annealing (ramp the weight from 0 to 1 over the first epochs) and free bits (do not penalize a dimension until its per-dimension KL exceeds a floor of roughly 0.05 to 0.5 nats). The second is the choice of likelihood: a diagonal Gaussian decoder is equivalent to an MSE reconstruction loss, which averages over plausible outputs and is the direct cause of the blurry samples VAEs are known for; discretized logistic or categorical likelihoods sharpen results noticeably. Modern image systems exploit this honestly, using a KL-regularized autoencoder as the perceptual compressor and putting the generative burden on a diffusion model in that latent space, as in Stable Diffusion.
Mathematical Formulation:
Where:
is the ELBO, a lower bound on the marginal log-likelihood
; the gap between them equals
, so maximizing the bound both fits the data and sharpens the approximate posterior.
is the observation and
the latent code, with
in the usual bottleneck setting.
is the encoder (recognition model) with parameters
, and
is the decoder (likelihood) with parameters
.
is the fixed prior, and
indexes latent dimensions in the closed-form KL, where
and
are the encoder outputs for example
.
is elementwise multiplication and
is the external noise draw that makes the sample differentiable in
and
.
is the KL weight;
recovers the exact ELBO,
gives the beta-VAE disentanglement regime, and
degenerates to a plain autoencoder. Networks are typically initialized so
, which starts training near the prior.

Figure 2: Encoded training data in a 2D latent space. The plain autoencoder is free to scatter codes anywhere, so a draw from lands in a hole; the KL term compresses the VAE’s aggregate posterior onto the prior, so the same draw hits populated territory.
| Property | Plain Autoencoder | VAE | VQ-VAE |
|---|---|---|---|
| Encoder output | One deterministic code vector per input | Distribution parameters, mean and log-variance | Continuous vector snapped to the nearest codebook entry |
| Training objective | Reconstruction error only | ELBO: reconstruction minus beta times KL to N(0, I) | Reconstruction plus codebook and commitment losses, no KL |
| Latent geometry | Arbitrary scale, holes and gaps between clusters | Smooth and prior-matched, interpolation stays on-manifold | Discrete grid of K entries, no notion of interpolation |
| Generating new data | Not supported, a random code decodes to noise | Draw z from the prior and decode, one forward pass | Needs a learned prior over codes, such as a transformer |
| Typical failure | Memorizes an identity map when the bottleneck is wide | Posterior collapse, blurry Gaussian-likelihood samples | Codebook collapse with most entries unused |
| Common use | Denoising, compression, anomaly detection | Generative modeling and the latent space of latent diffusion | Discrete tokens for autoregressive image and audio models |
Leave a Reply