An ordinary autoencoder compresses an input down to one latent point and decodes it back — the same input always produces the exact same output. What if the bottleneck held a whole neighborhood of possible points instead of just one, so decoding could generate variations you never actually saw?
The same input, encoded to the same distribution, decoded through a different noise draw each time. One of those draws reproduces the input exactly — the rest land nearby, at points the network never directly trained on.
A variational autoencoder encodes to a mean and log-variance instead of a single point, then samples via the reparameterization trick:
- — the sampled latent vector, passed to the decoder.
- — the mean of the encoded distribution, output by the encoder.
- — the standard deviation of the encoded distribution (derived from ).
- — an external noise term, drawn from a standard normal and independent of the network's parameters.
- Sampling becomes a deterministic function of noise
Writing sampling as a function of , , and an external noise term keeps the whole pipeline differentiable, instead of asking gradients to pass through a random draw directly.
- Gradients still reach the encoder
Gradients flow through and even though itself is random — the randomness is isolated entirely in , which needs no gradient at all.
A second loss term, the KL divergence — short for Kullback–Leibler divergence — to the standard normal, keeps every input's distribution from collapsing to a single point or drifting off to some far corner of latent space:
- — the KL divergence between the encoded distribution and the standard normal target; the regularization term added to the reconstruction loss.
Four different noise draws, one shared input. The mean alone decodes back to exactly the original value — but every other draw lands at a distinct nearby point, none of them ever explicitly trained on. That spread is the entire point: the decoder has learned a neighborhood, not a single memorized answer.
Encoding to , (so ), with a fixed sequence of stand-in noise values :
- No noise reproduces the input exactly
gives , and decoding back through gives exactly — the original input, recovered perfectly when there's no randomness at all.
- Every other draw lands somewhere new
- :
- :
- :
Four genuinely different outputs from one input, one encoder, one decoder.
- KL divergence penalizes distributions that stray from standard normal
Plugging into :
- Encoding : , , so .
- Encoding instead: , , , so .
The second is a smaller KL divergence, closer to , the one distribution where this penalty vanishes entirely.
Find the input, among the three candidates, whose encoded distribution sits closest to the standard normal (lowest KL divergence).
Replacing a single latent point with a distribution — and regularizing that distribution toward the standard normal — turns an autoencoder from a compression scheme into a generative one. Sample a from anywhere reasonable in that latent space, even a point no training input ever mapped to, and the decoder still produces something plausible. The next chapter applies a very different training signal to sequences: masking out pieces of the input and asking a model to reconstruct exactly what's missing.