A model that reconstructs images accurately does not necessarily learn a latent space suitable for generating new images.
Consider generating a realistic human face. Randomly selecting pixel values will almost certainly produce noise. A generative model must learn how facial features relate to one another and how they change together when a person turns their head or changes their expression.
Latent variable models capture these complex dependencies through underlying, unobserved factors. A Variational Autoencoder (VAE) builds on this idea by learning a probabilistic latent space that supports both reconstruction and generation.
1. Generative Models Learn Data Distributions
A face recognition model learns features that help distinguish one person from another. A face generation model must instead learn how facial features occur together.
The probabilistic patterns underlying observed data are described by a data distribution, p(x) , where x represents an observed sample. Generative models learn the characteristics of this distribution to produce new samples with similar properties.
Two broad approaches are:
- Likelihood-based models: Learn using the probability or probability density assigned to observed data. Examples include autoregressive models, VAEs, flow-based models, and diffusion models. Depending on the model, training uses either an exact likelihood or an approximation to the likelihood-based objective.
- Implicit models: Learn a sampling process without directly evaluating the data density. Generative Adversarial Networks (GANs), for example, use a discriminator to distinguish generated samples from real data during training.
For high-dimensional data such as images, directly representing every dependency between individual elements is difficult. Latent variables provide an alternative way to model these relationships.
2. Generating Data from Hidden Factors
When a person turns their head, the positions of their eyes and nose change together with the facial outline. Similarly, smiling changes the appearance of the mouth, cheeks, and surrounding facial features.
Although many pixels change, these changes are driven by shared underlying factors.
A latent variable represents an unobserved factor involved in the data generation process. A face image contains pixel values, but its orientation and degree of smiling are not necessarily recorded as separate variables.
A latent variable model introduces two probability distributions:
- Prior distribution p(z) : Defines how latent variables are selected.
- Conditional distribution p(x∣z) : Defines the distribution of possible observations given a latent variable.
The generation process consists of two sampling steps:
First, sample a latent variable from the prior. Then, sample an observation from the corresponding conditional distribution. Multiple observations can be generated from the same latent variable because the conditional distribution can capture additional variability.
If the latent space has fewer dimensions than the observed data, it can also provide a compact representation. However, dimensionality reduction is not the primary objective.
The central idea is to represent complex dependencies between observed elements through shared latent factors.
3. Marginal Likelihood: The Main Computational Challenge
During training, the model receives observed data x , but the latent variable z responsible for generating it is generally unknown.
A nearly frontal face image, for example, might be generated from several similar latent configurations. Evaluating the likelihood of that image requires accounting for all possible latent variables rather than selecting just one.
Let θ represent the trainable parameters of the conditional distribution. The joint distribution factorizes as:
To obtain the distribution of observed data alone, we marginalize over the latent variables:
For discrete latent variables, marginalization uses a sum instead of an integral.
Viewed as a function of the model parameters θ , this quantity is the marginal likelihood. It measures how well the model explains an observation without requiring knowledge of its underlying latent variable. For continuous observations, it represents a probability density rather than the probability of an individual observation.
The computational difficulty is that this integral can become intractable when the latent space is high-dimensional or the conditional distribution is complex.
Latent Variable Models vs. Autoregressive Models
An autoregressive model represents dependencies directly between observed data elements.
For data containing d elements, the distribution factorizes as:
In text generation, for example, previously generated words provide the context for generating the next word.
| Aspect | Autoregressive model | Latent variable model |
|---|---|---|
| Dependency representation | Earlier elements in the chosen ordering | Unobserved shared factors |
| Distribution structure | Product of conditional distributions | Marginalization over latent variables |
| Generation | Sequential conditional sampling | Sampling latent variables and then observations |
These approaches are not mutually exclusive. They can be combined, but introducing unobserved latent variables adds a marginalization problem.
Three Model Design Decisions
Implementing a latent variable generative model requires addressing three questions:
- Prior design: How should p(z) be defined so that latent variables can be sampled efficiently?
- Conditional distribution design: How should pθ(x∣z) model complex observations?
- Training strategy: How can the model parameters be optimized when the marginal likelihood is difficult to compute exactly?
A VAE addresses these challenges by parameterizing the conditional distribution with a neural network and using variational inference to handle the intractable marginal likelihood.
4. Why Reconstruction Alone Is Not Enough
Imagine training an autoencoder on MNIST handwritten digits, with each image represented by two latent coordinates.
Every image can then be plotted as a point in a two-dimensional latent space. Images with similar characteristics may cluster together, while digits with different characteristics may occupy separate regions.
This structure can be useful for feature extraction and similarity search. However, a latent space that supports accurate reconstruction does not necessarily support reliable generation.
Suppose representations of the digits 1 and 7 occupy separate regions. The area between them may contain latent coordinates that the decoder rarely encountered during training. Decoding a point from such a region may produce a meaningless image.
A latent space suitable for generation should exhibit two important properties:
- Continuity: Nearby latent variables produce similar observations, with gradual changes in latent coordinates leading to gradual changes in generated data.
- Completeness: Regions likely to be sampled from the prior produce meaningful observations.
From Fixed Vectors to Probability Distributions
A conventional autoencoder maps each input to a fixed latent representation. A VAE instead maps each input to a probability distribution over latent variables.
Rather than associating a handwritten digit with a single coordinate, a VAE learns to reconstruct its characteristics using latent variables sampled from the distribution associated with that input. This encourages the decoder to handle small variations in latent coordinates.
Probabilistic encoding alone, however, is insufficient. If the distributions associated with different inputs remain widely separated, the latent space may still contain regions rarely sampled during training.
A VAE addresses this problem by combining reconstruction with regularization toward a shared prior distribution.
5. How the ELBO Shapes the Latent Space
The generative process runs from z to x . During training, however, the model must infer latent variables from observed data.
The true posterior, pθ(z∣x) , can be difficult to compute because it depends on the marginal likelihood.
A VAE introduces an approximate posterior, qϕ(z∣x) , where ϕ represents the trainable encoder parameters.
The encoder typically predicts the mean and variance of a Gaussian distribution associated with the input. A latent variable is sampled from this distribution and passed to the decoder.
Training combines two objectives:
- Reconstruction loss: Measures how well a latent variable sampled from the approximate posterior explains the original input. Training minimizes the negative expected log-likelihood.
- Latent regularization: Uses KL divergence to discourage the approximate posterior from deviating excessively from the shared prior.
These objectives form the Evidence Lower Bound (ELBO), a lower bound on the log marginal likelihood.
The encoder and decoder are trained to maximize this objective. The first term rewards the model for explaining the observed input, while the second penalizes deviations from the prior.
Consider how each objective affects the latent space of MNIST images:
| Training objective | Effect on the latent space |
|---|---|
| Reconstruction only | Preserves input characteristics but may produce irregularly distributed latent representations. |
| KL divergence only | Encourages agreement with the prior but provides no reconstruction-based incentive to preserve distinguishing features. |
| Combined objective | Encourages a balance between input reconstruction and a latent structure suitable for sampling. |
Optimizing both terms helps improve latent space continuity and completeness while encouraging the model to retain information needed for reconstruction.
This regularization does not guarantee meaningful outputs at every latent coordinate. Generation quality also depends on the suitability of the prior, the expressiveness of the latent distributions, and how well the decoder is trained.
6. Choosing a Prior for Generation
To generate new data without an existing input, a VAE needs a distribution from which latent variables can be sampled directly.
A common choice is the standard normal prior:
For a latent vector with m dimensions, this distribution factorizes as:
Here, I is the identity matrix. Each latent dimension has zero mean and unit variance, with zero covariance between different dimensions.
For a multivariate Gaussian distribution, this structure implies statistical independence between dimensions, allowing each coordinate to be sampled independently.
What Happens When You Change a Latent Coordinate?
Changing individual latent coordinates can reveal how internal representations influence generated outputs.
Suppose a model has learned to represent different facial attributes in separate latent dimensions:
- Changing z1 gradually changes facial orientation.
- Changing z2 changes the degree of smiling.
- Changing both produces different combinations of orientation and expression.
This behavior is possible, but it is not guaranteed. Statistical independence between dimensions of the prior does not imply that learned latent dimensions represent independent semantic factors. A single latent coordinate may influence several characteristics simultaneously.
7. Training vs. Generation: Where Does the Latent Vector Come From?
From an implementation perspective, the important distinction between training and generation is the distribution from which the latent variable is sampled.
During Training
The encoder receives an observed input and defines its approximate posterior. A latent variable is sampled from that distribution and passed to the decoder for reconstruction.
Observed input | v Encoder | v Approximate posterior | v Sample latent vector | v Decoder | v Reconstruction The encoder and decoder are optimized jointly through the ELBO.
During Generation
The encoder is not required when generating new samples directly from the prior. Instead, a latent variable is sampled from the standard normal distribution:
The trained decoder defines the conditional distribution from which a new observation is generated:
Standard normal prior | v Sample latent vector | v Trained decoder | v New generated sample Takeaway
A latent variable model captures complex data dependencies through unobserved shared factors. In a VAE, three components make this approach practical:
- The prior defines how latent variables are sampled.
- The encoder approximates posterior inference from observed data.
- The decoder models how observations are generated from latent variables.
A conventional autoencoder primarily optimizes reconstruction. A VAE additionally regularizes its probabilistic latent representations toward a shared prior, encouraging a latent space that supports generation without requiring an existing input.
Originally published at zeromathai.com.
Original article: https://zeromathai.com/en/generative-latent-variable-models-course-en/
