Variational Autoencoders (VAEs)

Originally published on Medium on August 16, 2025. Read the original at medium.com/@chindahel1.

Variational Autoencoders (VAEs) are a class of generative models capable of learning complex data distributions and generating synthetic samples. Unlike discriminative models (e.g., classifiers), VAEs focus on modeling the probability distribution of the data.

Assuming the data we observe is generated from some unknown true distribution p∗. We use a parametric probabilistic model pθ to model the data, where θ represents the model’s parameters. The goal is to make pθ as close as possible to p∗.

The true data distribution and the parametric model

For a given data point x, the term pθ(x) represents the likelihood — the probability (or probability density) assigned to x by the model under parameters θ. When we have a dataset D={x1,x2,…,xN}, we adjust θ to maximize the combined likelihood of observing all data points in D. Essentially, it is a case of maximum likelihood estimation.

When a model to learns the distribution from a dataset, it doesn’t just memorize the data points; it tries to understand the underlying process that generated them. We assume that every data point, like an image of a circle, is created by a set of hidden, low-dimensional factors. These factors are called latent variables.

Latent variables as the hidden factors behind each data point

Think of latent variables as a compact “code” or blueprint for each data sample. For a simple dataset of circles, the latent variables could be the circle’s x-y coordinates and its radius. The model learns to map these few variables to a high-dimensional image of a circle. This is far more efficient than storing every single pixel value of every circle in the dataset. This collection of latent variables for a dataset is called the latent space or latent representation. It’s a low-dimensional manifold where data points that are similar in the “real world” (e.g., circles with similar radii) are also close to each other in this latent space.

The latent representation allows bi-directional mapping:

  • Inference: Recovering latent variables from observed data (e.g., estimating [x,y,r] given a circle’s image). This process is known as encoding.
  • Generation: Reconstructing data from latent variables (e.g., drawing a circle from [x,y,r]). This process is known as decoding.
Inference and generation: encoding and decoding through the latent space

We are trying to find a joint distribution pθ(z|x), using the chain rule of probability, this can be represented as the probability of z independent of x and the conditional probability of x given z. Given this factorization, the VAE is encouraged to map the full dat distribution D to a latent distribution pθ(z) then sampling from this will produce realistic synthetic examples in data space, all we have to do now is optimize θ.

Factorization of the joint distribution over data and latent variables

To find the optimal parameters that define this joint distribution, we have to optimize θ, by maximizing marginal likelihood of the data, pθ(x). The expression for the marginal likelihood is an integral over all possible latent variables:

The marginal likelihood as an integral over latent variables

However, computing pθ(x) directly is intractable because it requires integrating over all possible z so we adapt Bayes’ rule.

Bayes' rule applied to the posterior over latent variables

The model evidence is the likelihood that our distribution parameter θ are approximate to the data x that we observe. We ideally want to maximize the model evidence, to do that, we need to find θ for which the right hand side is maximized.

The model evidence and the posterior

Unfortunately, the fact that the marginal likelihood is intractable, the posterior is also intractable. But the fact that they are intractable means that knowing one, we know the other.

The intractable marginal likelihood and posterior

We shift attention from calculating pθ(x) to approximating posterior probability pθ(z|x) and we do this by using variational inference.

Variational Inference

This implies approximating a target distribution to match a given distribution.

Approximating a target distribution with variational inference

The KL divergence is an objective used to represent the expected information loss when we adopt q as our model while the true distribution is p.

The KL divergence between the approximate and true distributions

It represents the divergence of our approximate distribution q from the true distribution p. If p = q, then the KL divergence is 0 because both our approximate distribution and the true distribution are equal or exactly the same.

The KL divergence is zero when the distributions match

Since the KL divergence is always non-negative, the KL divergence is expanded as such:

Expanding the KL divergence

Now the likelihood log pθ (x) is the sum of the KL divergence and the ELBO since KL divergence is non-negative, we know that log pθ (x) ≥ ELBO value; making ELBO the lowest boundary of our approximation distribution. Essentially, the KL divergence is the gap between the ELBO and the log likelihood.

The log likelihood as the KL divergence plus the ELBO

Consequently, maximizing the ELBO maximizes log pθ (x) and minimizes the KL divergence. It minimizes the divergence from the true posterior p to the approximate distribution q. Simultaneously optimizing the generative and inference models. The ELBO is maximized using Stochastic gradient Descent.

Maximizing the ELBO maximizes the log likelihood

The ELBO gradient w.r.t. ϕ is challenging because ϕ influences qϕ(z∣x), which defines the expectation. To move from the mean and standard deviation to a specific latent vector z, we need to sample from this distribution. However, this sampling process is stochastic and not differentiable, which makes it impossible to backpropagate gradients through it during training.

To solve this, VAEs use the reparameterization trick. Instead of sampling directly from the distribution defined by μ and σ, we sample a value ϵ from a simple, fixed distribution (a standard Gaussian with a mean of 0 and a standard deviation of 1). We then calculate our latent vector z as a deterministic function of ϕ and a noise variable ϵ:

The reparameterization trick: z as a deterministic function of phi and epsilon

where :

  • μϕ(x) and σϕ(x) are the mean and standard deviation outputs of the encoder network with parameters ϕ
  • ⊙ represents element-wise multiplication (Hadamard product)
  • ϵ is noise sampled from a standard normal distribution
  • I is the identity matrix

This allows gradients to flow through μϕ and σϕ, enabling the entire model to be trained end-to-end using backpropagation.

VAE Architecture

The encoder maps input x to two outputs μ and σ, we then have stochastic variable ϵ sampled from fixed distribution p(ϵ) and used to sample latent variable z. The decoder, also a deterministic model, maps z to output y. Deep neural networks ensure maximum flexibility in deterministic models.

The VAE architecture: encoder, sampling, and decoder

The VAE is essentially an architecture with an inference/encoder model that reduces data x to latent representation z which is a low dimensional representation of the original data. Then the generative/decoder model attempts to reconstruct the original data x given the latent representation z. And the overall model is penalized according to the reconstruction error between the input and the reconstruction.

The VAE objective: reconstruction error plus the prior penalty

The model is also penalized for the difference between the distribution the encoder learned and a standard, well-behaved prior distribution. This forces the latent space to be continuous and well-structured, preventing the model from creating disconnected clusters.

Thus, our model is an auto encoder, which learns to reconstruct inputs through low dimensional bottleneck that captures all important information in input. Instead of deterministically mapping x to z like an auto encoder, we sample z from a stochastic model on x that has been learned through variational inference, giving us a variational auto encoder.

References

  • Auto-Encoding Variational Bayes. arXiv:1312.6114.
  • Variational Autoencoders — Jeremy Jordan (jeremyjordan.me).
  • The Reparametrization Trick (baeldung.com).
  • Understanding variational Autoencoders (youtube.com).