An autoencoder can reconstruct its inputs successfully and still fail to generate meaningful data from a randomly selected latent vector. The problem is not necessarily the decoder. It may be the structure of the latent space itself.
A Variational Autoencoder (VAE) addresses this problem by representing inputs as probability distributions rather than fixed latent vectors. It combines probabilistic encoding with prior regularization to encourage a latent space that supports both reconstruction and generation.
Understanding how these distributions interact is the key to understanding how a VAE generates new data.
Why Reconstruction Alone Is Not Enough
Consider a 28 × 28 MNIST handwritten digit image containing 784 pixels. An autoencoder can compress this image into a much smaller representation that captures features such as stroke direction, thickness, and orientation.
If the encoder produces the vector , the image becomes a point in a two-dimensional latent space.
Similar images may form clusters, while images with different characteristics may occupy separate regions. This structure can be useful for classification and similarity search, but it does not necessarily support reliable generation.
Suppose handwritten digits 1 and 7 occupy separate regions. The space between them may contain latent vectors that the decoder has not learned to interpret properly. Sampling from these regions can produce meaningless images.
A latent space suitable for generation should exhibit two important properties:
- Latent space continuity: Nearby latent vectors produce similar outputs, and gradual changes in latent vectors lead to gradual changes in generated data.
- Latent space completeness: Regions with a high probability of being sampled should reliably produce meaningful data.
VAEs encourage these properties through probabilistic encoding and prior regularization.
From Fixed Coordinates to Probability Distributions
A conventional autoencoder maps an input to a fixed latent vector. The same input consistently produces the same representation. A VAE instead maps an input to a probability distribution over latent variables.
For example, consider a one-dimensional Gaussian distribution with a mean of 2.0 and a standard deviation of 0.3. The same input could produce latent samples such as 1.8, 2.1, or 2.3.
The decoder learns to explain the original input using different samples from this distribution rather than relying on a single coordinate.
A common VAE encoder defines a multivariate Gaussian distribution with diagonal covariance:
Here:
- is the observed input.
- is the latent variable.
- represents the trainable encoder parameters.
- is the latent mean vector.
- is the latent variance vector.
The diagonal covariance structure assumes that latent dimensions are conditionally independent given the input .
From an implementation perspective, the encoder produces the parameters of a distribution. The latent vector passed to the decoder is sampled from that distribution.
Why probabilistic encoding needs regularization
Probabilistic encoding alone does not ensure a well-structured latent space. Input-specific distributions can still occupy distant regions, leaving poorly learned areas between them.
A VAE addresses this by encouraging each approximate posterior distribution to remain close to a shared prior distribution .
Its training objective balances two competing requirements:
| Objective | Purpose |
|---|---|
| Reconstruction loss | Preserve information needed to explain the input. |
| KL divergence | Limit the discrepancy between the approximate posterior and the prior. |
Optimizing reconstruction alone may preserve input features without producing a latent space suitable for generation. Conversely, minimizing KL divergence alone may make input-specific distributions identical, removing information needed to distinguish different inputs.
Joint optimization encourages informative latent representations and compatibility with prior-based sampling. However, this regularization does not guarantee meaningful outputs at every latent coordinate.
The Probabilistic Generative Model
Complex data contains dependencies between its components. An autoregressive model represents these dependencies by conditioning each component on previously generated components:
A latent-variable model introduces latent variables that represent underlying factors shared across multiple observed components. For a facial image, these factors might include facial orientation or expression.
The basic VAE generative process consists of two sampling steps:
The corresponding joint distribution is:
Here, is the latent prior, is the conditional output distribution, and represents the trainable decoder parameters.
During training, only is observed. The latent variable responsible for generating it is unknown, so computing the marginal likelihood requires integrating over all possible latent variables:
Training aims to increase the log marginal likelihood of observed data. Implementing this objective introduces three fundamental design problems:
- Define a prior distribution from which latent variables can be sampled efficiently.
- Parameterize a conditional distribution that generates data from latent variables.
- Optimize the model without directly evaluating the difficult latent-variable integral.
Autoregressive models and latent-variable models are not mutually exclusive. A VAE can also use an autoregressive decoder to parameterize its conditional output distribution.
Designing the Latent Prior
The prior determines where latent variables come from when generating data without an original input. A common choice is the multivariate standard normal distribution:
For a -dimensional latent vector, this distribution factorizes into independent standard normal distributions:
Each latent dimension has unit variance, and the covariance between different dimensions is zero. Under this Gaussian prior, the dimensions are statistically independent and can be sampled individually.
For a two-dimensional latent space, independently sampling two standard normal variables might produce:
The trained decoder transforms this vector into the parameters of an output distribution.
Changing individual latent components and observing the generated outputs can help reveal what the latent space has learned. In facial image generation, changing a latent component might affect facial orientation or expression.
However, statistical independence in the prior does not guarantee semantic independence in the learned representation. A single latent dimension may influence several characteristics simultaneously.
The Decoder Predicts a Distribution, Not Just an Output
A VAE decoder defines a probability distribution over possible outputs rather than mapping a latent variable directly to a single fixed observation.
This distinction matters because even data generated from the same underlying factors can exhibit different observed values.
The general decoder formulation is:
The neural network maps a latent variable to the parameters of an output distribution. Although the network parameters remain fixed after training, different latent inputs produce different distribution parameters.
This is known as probability parameterization. The appropriate output distribution depends on the type of data being modeled.
Gaussian Decoder: Continuous Data
For continuous observations such as temperatures or sensor measurements, the decoder can define a Gaussian distribution:
The mean vector determines the center of the output distribution. The covariance matrix describes variability and relationships between output components.
Implementations may simplify this distribution by using fixed or diagonal covariance.
Bernoulli Decoder: Binary Data
For binary images containing pixel values of either 0 or 1, a Bernoulli decoder predicts the probability that each pixel equals 1.
Assuming conditional independence between the output components:
Here, is the probability that the -th component equals 1. If the observed value is 1, its probability is . If the observed value is 0, its probability is .
A sigmoid function is commonly used to convert neural network outputs into valid Bernoulli probabilities.
Categorical Decoder: Discrete Categories
For observations representing one of several categories, the decoder can define a categorical distribution:
Here, is the number of categories, is the probability of category , and is a one-hot vector. Only the probability of the selected category contributes to the resulting likelihood.
A softmax function is commonly used to produce category probabilities that sum to 1.
| Decoder | Data type | Distribution parameters |
|---|---|---|
| Gaussian | Continuous | Mean and covariance |
| Bernoulli | Binary | Probability of each component being 1 |
| Categorical | Discrete categories | Category probability vector |
All three decoders follow the same principle: map latent variables to probability distribution parameters. What changes is the distribution used to model the observed data.
Distribution Means vs. Actual Samples
Once the decoder defines an output distribution, there are two ways to obtain an output.
The first is to use the expected value of the distribution:
The second is to sample from the distribution:
For a fixed latent variable, the first approach produces a deterministic representative value, while the second produces a stochastic sample.
The distinction becomes particularly clear when comparing different output distributions:
| Distribution | Expected value | Possible sampled output |
|---|---|---|
| Gaussian | 10 | 9.8 or 10.3 |
| Bernoulli | 0.8 | 0 or 1 |
| Categorical | [0.1, 0.7, 0.2] | [0, 1, 0] |
The Gaussian example assumes a mean of 10 and a standard deviation of 0.5. In the Bernoulli example, the output is 1 with an 80% probability and 0 with a 20% probability.
This explains why displaying the expected values of a Bernoulli image decoder can produce a grayscale-looking image, even though actual samples contain only binary pixel values.
Deterministic decoding does not necessarily make the entire reconstruction process deterministic. If the encoder samples a different latent variable on each pass, using the decoder's expected value can still produce different reconstruction results.
Conditional independence does not imply marginal independence
A simple conditional output distribution can still represent dependencies through shared latent variables.
Consider a Gaussian decoder with two output components:
For a fixed , the two output components are conditionally independent. However, changing can change both means and variances together.
After marginalizing over the latent variable, dependencies can emerge between the observed components. Shared latent variables therefore allow a model with a relatively simple conditional output distribution to represent more complex data relationships.
What the decoder computes during training
During VAE training, there is no need to sample a new observation from the decoder's output distribution. Instead, the model directly evaluates the log likelihood of the actual observed input under that distribution.
Sampling from the decoder is primarily relevant when generating new observations.
Why the VAE Needs a Variational Encoder
Generating data from a trained VAE is straightforward: sample a latent variable from the prior and pass it through the decoder.
Training presents a different problem. Given an observed input, the model must infer which latent variables are likely to explain it.
Monte Carlo estimation from the prior
The marginal likelihood can be expressed as an expectation under the prior:
Given independent latent samples:
Monte Carlo estimation approximates the marginal likelihood by averaging their conditional likelihoods:
The calculation is simple, but most prior samples may not explain the current observation well.
For example, when evaluating the marginal likelihood of a handwritten digit 7, latent variables that generate digits 0 or 3 may contribute very little to the estimate.
If the latent space is high-dimensional and the region that explains a particular observation is narrow, obtaining enough useful samples becomes difficult. Naive prior sampling can therefore be computationally expensive and produce estimates with high variance.
Approximating the posterior
Rather than sampling latent variables without considering the input, we want a distribution that identifies latent variables likely to explain the observation.
Bayes' theorem defines the true posterior:
The numerator combines the prior with the conditional likelihood. The denominator is the marginal likelihood, which contains the difficult integral over latent variables. Computing the exact posterior is therefore also difficult.
A VAE uses variational inference to approximate this posterior with a tractable distribution:
The variational encoder is not simply a mechanism for injecting randomness into a compressed representation. Its purpose is to approximate the posterior so that latent variables relevant to the current observation can be sampled efficiently.
Connecting Inference and Generation Through the ELBO
Sampling from the approximate posterior and simply averaging conditional likelihoods does not generally recover the original marginal likelihood. The original marginal likelihood is an expectation under the prior, whereas the approximate posterior depends on the observed input.
Because evaluating the log marginal likelihood requires a computationally difficult integral, a VAE instead maximizes a tractable lower-bound objective called the Evidence Lower Bound (ELBO):
The two terms serve different purposes:
- Expected log likelihood: Measures how well latent samples from the approximate posterior explain the observed input.
- KL divergence: Penalizes discrepancies between the approximate posterior and the shared prior.
The reconstruction and regularization objectives introduced earlier are therefore two components of the same probabilistic training objective. Maximizing the ELBO allows both networks to be optimized jointly.
Amortized Inference: One Encoder for Many Inputs
Traditional variational inference can optimize separate variational parameters for each observation. For a dataset containing tens of thousands of images, repeatedly performing this optimization can be computationally expensive.
A VAE replaces input-specific optimization with a shared inference network.
In traditional variational inference, each input has separately optimized parameters :
In amortized variational inference, the shared parameters belong to a neural network that predicts the approximate posterior parameters for a given input:
The encoder implements this mapping through a neural network :
For a one-dimensional Gaussian latent variable, the resulting distribution is:
Notice that the encoder outputs two values: a mean and a variance. However, the latent variable itself is still one-dimensional.
These two values parameterize a distribution over a single latent variable. The decoder receives a sample from that distribution, not the two distribution parameters as separate latent dimensions.
This approach is called amortized inference because the cost of learning how to infer latent distributions is shared across observations. For a new input, a forward pass through the trained encoder replaces a separate variational optimization procedure.
Training vs. Generation: What Actually Runs?
The VAE architecture revolves around three probability distributions:
| Distribution | Role |
|---|---|
| Prior used to sample latent variables without an input. | |
| Approximate posterior used to infer latent variables from observed data. | |
| Conditional distribution used to model or generate observations. |
The computational paths differ depending on whether the model is being trained or used to generate new data.
Training: Joint Optimization
During training, an observed input is available:
The training procedure is:
- Pass the observed input through the encoder to obtain the approximate posterior parameters.
- Sample a latent variable from the approximate posterior.
- Pass the latent sample through the decoder to obtain the output distribution parameters.
- Evaluate the observed data's log likelihood and the KL divergence to estimate the ELBO.
- Update both networks to maximize the ELBO.
A typical Gaussian VAE uses the reparameterization trick to allow gradients to propagate through the latent sampling operation to the encoder.
Generation: Sampling Without an Original Input
When generating new data without an original input, the encoder is unnecessary:
The generation procedure is:
- Sample a new latent vector from the standard normal prior.
- Pass the latent vector through the trained decoder.
- Sample a new observation from the output distribution or use a representative value, depending on the application.
The crucial difference is where the latent variable comes from. Training uses input-dependent samples from the approximate posterior, while unconditional generation uses samples drawn directly from the prior.
Prior regularization connects these two computational paths by encouraging the approximate posterior distributions to remain compatible with the prior.
Takeaway
Inference starts with observed data, while unconditional generation starts with the latent prior. By learning input-dependent approximate posteriors and regularizing them against a shared prior, a VAE extends an autoencoder's reconstruction capabilities into a probabilistic framework for generating new data without an original input.
Originally published at zeromathai.com.
Original article: https://zeromathai.com/en/vae-probabilistic-generation-course-en/
Top comments (0)