Variational Autoencoders (VAEs) Explained: From Theory to ELBO and the Reparameterization Trick

Introduction

Autoencoders have been a remarkable innovation in machine learning. Their ability to compress data into a lower-dimensional bottleneck enables downstream tasks to run more efficiently, avoiding the heavy computational costs of processing high-dimensional inputs. Beyond compression, autoencoders have proven useful in various computer vision applications, such as image denoising, object removal, and inpainting, as discussed in our earlier article on autoencoders.

If you're new to autoencoders, I recommend watching this introductory video before diving into variational autoencoders (VAEs). While vanilla autoencoders are powerful, they come with notable limitations. To address these, researchers introduced a more advanced variant—the variational autoencoder (VAE)—first proposed by Kingma and Welling in 2013. In this article, we'll explore VAEs in depth, covering their motivation, theoretical foundations, the evidence lower bound (ELBO), and the reparameterization trick.

Motivation: Why Do We Need VAEs?

In our previous discussion, we focused on autoencoders primarily for data compression. However, there are other compelling applications where vanilla autoencoders fall short, particularly in latent space structure and image generation.

Similarity Preservation in Latent Space

When you compute distances between points in the latent space of a vanilla autoencoder, you'll often find that similarity relationships are not preserved as well as in other embedding techniques. This happens because the training objective is reconstruction-focused, not representation-focused; the model learns to reconstruct inputs rather than to organize the latent space semantically. As a result, similar images may land far apart, and dissimilar ones may cluster together. This lack of structure is a significant weakness.

Diagram showing a well-structured latent space where similar objects cluster together, a property vanilla autoencoders often fail to achieve
A desirable latent space preserves similarity—something vanilla autoencoders often fail to achieve.

Image Generation and Interpolation

Since decoders excel at reconstructing images, a natural question arises: can we generate new, high-quality images by sampling points from the latent space? In theory, sampling near an existing encoded point should yield similar images, and interpolating between two points should produce meaningful transitions. For instance, if you have a latent point for a smiling face and another for a frowning face, a midpoint should ideally generate a neutral expression. Unfortunately, vanilla autoencoders often produce poor results in such scenarios because the latent space is not continuous or well-regularized.

Example showing poor interpolation in a vanilla autoencoder's latent space
Vanilla autoencoders typically fail at latent space interpolation, yielding unrealistic or incoherent outputs.

These issues—poor similarity preservation and unstructured latent space—motivate the development of variational autoencoders, which enforce a probabilistic and continuous latent space.

Variational Autoencoders: A Probabilistic Take

Instead of encoding an input to a single point, a VAE encodes it to a probability distribution over the latent space. This forces the latent space to be continuous and regularized, enabling better interpolation and generation. The encoder outputs parameters (mean and variance) of a Gaussian distribution, and the decoder samples from this distribution to reconstruct the input.

The Evidence Lower Bound (ELBO)

The training objective of a VAE is derived from maximizing the likelihood of the data under the model. Since direct maximization is intractable, we optimize a lower bound called the ELBO, which balances two terms:

  • Reconstruction loss: Ensures the decoder accurately reconstructs the input.
  • KL divergence: Regularizes the encoder's distribution to be close to a prior (e.g., standard normal), encouraging smoothness and continuity.

This trade-off is crucial: too much emphasis on reconstruction leads to a latent space similar to vanilla autoencoders, while too much regularization can cause posterior collapse, where the latent variable carries little information.

The Reparameterization Trick

To backpropagate through the stochastic sampling process, we use the reparameterization trick. Instead of sampling directly from the latent distribution, we sample from a standard normal and transform it: z = μ + σ ⊙ ε, where ε ~ N(0, I). This allows gradients to flow deterministically through the network, making training stable and efficient.

Conclusion and Practical Considerations

Variational autoencoders address the key limitations of vanilla autoencoders by constructing a well-structured, continuous latent space. This enables smoother interpolation and more coherent image generation. However, VAEs also have trade-offs, such as generating slightly blurry images compared to GANs. Recent advances, like β-VAEs (which weight the KL term) and VQ-VAEs (which use discrete latent spaces), have further improved their performance and applicability.

In practice, when working with VAEs, you should carefully tune the KL weight and consider the latent dimensionality to balance reconstruction fidelity and generation quality. As of 2026, VAEs remain a fundamental tool in generative modeling, often combined with other architectures in hybrid models for applications like text-to-image synthesis and representation learning.

We hope this deep dive has clarified the theory and practical utility of VAEs. For more, check out our accompanying video and related articles on autoencoders and latent space dynamics.

via Towards Data Science

Related