An autoencoder can reconstruct a handwritten digit from a compressed representation. But what happens when you give its decoder a randomly selected latent vector instead of encoding an existing image?

There is no guarantee that the result will resemble a meaningful handwritten digit. Learning to reconstruct data and learning to generate data are different problems.

A Variational Autoencoder (VAE) addresses this distinction by combining representation learning, probabilistic modeling, and variational inference. Instead of encoding an input as a fixed latent vector, it represents the input as a probability distribution over possible latent codes.

Understanding how these components fit together explains why VAEs use probabilistic encoders and decoders, how their training objective is constructed, and how they generate new data.

From Autoencoders to Generative Models

A conventional encoder–decoder architecture transforms an input into an internal representation and uses that representation to produce an output.

xEncoder​hDecoder​y

An autoencoder specializes this architecture for input reconstruction:

h=fϕ​(x)
x~=gθ​(h)=gθ​(fϕ​(x))≈x

Here, fϕ​ and gθ​ are the encoder and decoder, ϕ and θ are their trainable parameters, and h is the latent representation.

A typical reconstruction objective is mean squared error (MSE):

Lrecon​D1​i=1∑D​(xi​−x~i​)2

Here, D is the dimensionality of the input. By minimizing reconstruction error, the model learns to preserve information needed to reconstruct its inputs.

Because the input itself serves as the reconstruction target, separate labels are unnecessary. The learned representations can also support tasks such as classification and clustering.

However, low reconstruction error does not necessarily indicate that the encoder has learned useful features. A sufficiently flexible model could simply learn to copy its input.

An undercomplete autoencoder uses a latent representation with fewer dimensions than the input, encouraging the encoder to retain important information. An overcomplete autoencoder uses a latent representation with more dimensions than the input, allowing richer representations but potentially requiring additional constraints to prevent simple copying.

Neither architecture inherently defines how to select latent vectors for generating new data. To move from reconstruction to generation, we need a distribution from which latent variables can be sampled and a model that describes how those variables produce observations.

Define a Generative Process Before Learning an Encoder

Consider handwritten images. Changing the thickness or slant of a digit affects multiple pixels simultaneously. A latent variable can represent an underlying factor that influences several observed components.

A probabilistic latent variable model defines generation in two steps:

z∼p(z)
x∼pθ​(x∣z)

The resulting joint distribution is:

pθ​(x,z)=p(z)pθ​(x∣z)

The prior p(z) determines how latent variables are selected. The conditional distribution pθ​(x∣z) determines how observations are generated from them.

This structure allows dependencies between observed components to emerge through shared latent variables, even when those components are modeled as conditionally independent given z .

Marginal likelihood

Different latent variables can produce similar observations. Evaluating how well the model explains an observed input therefore requires considering all possible latent variables:

pθ​(x)∫pθ​(x,z)dz∫p(z)pθ​(x∣z)dz

This operation is called marginalization. The resulting marginal probability or density is the marginal likelihood of the observed data. For discrete latent variables, the integral becomes a sum.

VAEs belong to the likelihood-based family of generative models. Unlike implicit models such as GANs, they define probability distributions and optimize a likelihood-related objective.

Computing the marginal likelihood exactly can become intractable when the latent space is high-dimensional and the conditional distribution is parameterized by a complex neural network. Variational inference and the ELBO provide a way to train the model without evaluating this integral directly.

Variational Inference: Learning the Reverse Direction

Generation proceeds from latent variables to observations. Learning also requires the reverse direction: given an observation, which latent variables could explain it?

Bayes' theorem defines the posterior:

pθ​(z∣x)pθ​(x)p(z)pθ​(x∣z)​

The numerator combines the prior with the conditional likelihood. The denominator is the marginal likelihood, which normalizes the posterior but is difficult to compute directly.

Monte Carlo estimation using samples from the prior is possible. However, it can be inefficient when the latent variables that explain an observation occupy only a narrow region of the latent space.

Variational inference addresses this problem by introducing a computationally manageable approximation to the true posterior.

Given a variational family Q , the objective is:

q∗(z)q∈Qargmin​DKL​(q(z)∣pθ​(z∣x))

KL divergence measures the discrepancy between the approximate and true posterior:

DKL​(q(z)∣pθ​(z∣x))Ez∼q(z)[logpθ(z∣x)q(z)​]

KL divergence is non-negative and becomes zero when the distributions match. It is generally asymmetric, so it is not a distance metric in the usual mathematical sense.

A mean-field approximation simplifies inference by factorizing the approximate distribution:

q(z)=k=1∏K​qk​(zk​)

This independence assumption belongs to the approximation, not necessarily to the true posterior. It improves computational tractability but may prevent the approximation from capturing complex dependencies between latent variables.

Amortized inference

Using a Gaussian approximation allows inference to focus on distribution parameters such as means and variances. Optimizing a separate set of parameters for every observation, however, would require substantial computation.

A VAE instead trains a shared encoder to predict those parameters:

λx​=fϕ​(x)
qϕ​(z∣x)=q(z;fϕ​(x))

Here, λx​ denotes the variational distribution parameters associated with input x .

This approach is called amortized inference. Once trained, the encoder can construct an approximate posterior for a new observation through a forward pass rather than performing a separate iterative optimization.

The encoder is therefore more than a feature extractor: it is a learned inference function.

ELBO: Turning Intractable Inference into a Trainable Objective

Directly minimizing KL divergence from the true posterior still appears to require access to that posterior. Substituting Bayes' theorem into the divergence reveals a more useful formulation:

DKL​(q(z)∣pθ​(z∣x))Eq(z)[logq(z)−logpθ(x,z)]+logpθ​(x)

For fixed x and θ , the log marginal likelihood does not depend on the approximate distribution. Its exact value is therefore unnecessary when optimizing that distribution.

This leads to the Evidence Lower Bound (ELBO):

L(q,θ;x)Eq(z)[logpθ(x,z)−logq(z)]

The relationship between the ELBO and the log marginal likelihood is:

logpθ​(x)L(q,θ;x)+DKL​(q(z)∣pθ​(z∣x))

Because KL divergence is non-negative:

L(q,θ;x)≤logpSource:https://dev.to/zeromathai/variational-autoencoders-explained-from-latent-representations-to-probabilistic-generation-35ei

Related posts