Generative models · Representation learning
Probabilistic latent representations with VAEs
A PyTorch implementation exploring variational inference, reconstruction, KL regularization, and latent-variable generation.
1. Why did you choose VAEs?
I chose Variational Autoencoders to understand how generative models can learn probabilistic representations of data in a lower-dimensional latent space.
I was interested in the relationship between representation learning and generative modeling: rather than learning only a deterministic mapping from an input to a compressed representation, a VAE learns a probability distribution over latent variables and uses those variables to reconstruct or generate data.
This introduced me to a different perspective on generative modeling from the diffusion models I was exploring. With a VAE, generation is connected to learning a latent probability distribution and decoding samples from that distribution. With a diffusion model, generation is connected to progressively reversing a noise-corruption process.
I wanted to understand how the VAE objective balances reconstruction quality against regularization, and how that balance influences the structure and usefulness of the learned latent space.
2. What did you implement?
I implemented the core VAE workflow in PyTorch, following its mathematical formulation.
The principal components were:
- Encoder: Maps an input image to the parameters of an approximate posterior distribution over latent variables.
- Latent distribution: Represents the encoder's output using a mean vector and a variance-related parameter, typically expressed through the logarithm of the variance.
- Reparameterization trick: Samples a latent representation while allowing gradients to flow through the encoder.
- Decoder: Maps a sampled latent vector back to the input data space to reconstruct the image.
- Reconstruction objective: Measures how closely the reconstructed image matches the original input.
- KL-divergence regularization: Encourages the learned approximate posterior to remain close to a chosen prior distribution.
- Training loop: Optimizes the model using the combined reconstruction and regularization objective.
- Generation and visualization: Uses latent samples to generate images and examines the resulting representations.
The project involved implementing the essential probabilistic components rather than treating the VAE as an opaque pretrained model.
The precise division between custom-written architecture code and any reused training utilities should be confirmed from the repository before describing individual components as independently implemented.
3. What mathematical ideas did you work through?
The central mathematical idea is that a VAE learns an approximate posterior distribution over latent variables while maximizing a lower bound on the data log-likelihood.
The approximate posterior
Let \(x\) denote an input image and \(z\) its latent representation.
The encoder approximates the posterior distribution \(p(z\mid x)\) with a tractable distribution \(q_\phi(z\mid x)\), commonly a Gaussian:
The encoder predicts the mean and variance parameters that describe this distribution.
The evidence lower bound
The VAE optimizes the evidence lower bound (ELBO):
The first term encourages the decoder to reconstruct the observed data. The second penalizes divergence between the approximate posterior and the latent prior, commonly a standard normal distribution.
In practice, training often minimizes the negative ELBO, expressed as a reconstruction loss plus a KL-divergence term.
This objective creates a trade-off: the model must preserve enough information to reconstruct the input while learning a latent distribution that remains compatible with the prior.
The reparameterization trick
Directly sampling a random latent variable from a distribution parameterized by the encoder would obstruct ordinary backpropagation through that sampling operation.
Instead, the model samples independent Gaussian noise and transforms it using the encoder's predicted parameters:
This separates the random component from the differentiable transformation, allowing gradients to flow through the mean and scale parameters.
Working through these equations helped me understand how probabilistic sampling can coexist with gradient-based neural-network optimization.
4. What architecture did you use?
The model follows the standard encoder–latent-variable–decoder structure.
The encoder transforms the input image into a lower-dimensional representation parameterized by a mean and a variance. The reparameterization step samples a latent vector, and the decoder uses that vector to reconstruct the image.
The architecture must accommodate the input dimensions and output representation of each dataset.
The exact encoder and decoder layer types, number of hidden units or convolutional stages, activation functions, latent dimensions, and normalization choices should be verified against the implementation.
I would not claim that the model used a particular convolutional architecture or that it had a specific latent dimensionality for every dataset without checking the original code.
5. Which datasets and training configurations did you use?
The project involved experiments with two image datasets:
MNIST: Grayscale handwritten digits, providing a relatively simple setting for studying reconstruction and latent representations.
CIFAR-10: Small RGB images spanning ten object categories, introducing more varied visual structure and more challenging reconstruction requirements.
The model was trained using PyTorch, with the encoder and decoder optimized jointly through the VAE objective.
Important configuration details include image normalization, the reconstruction-loss definition, the latent dimensionality, the KL-divergence weighting, batch size, learning rate, optimizer, number of epochs, and hardware.
The precise preprocessing pipeline, training duration, batch size, optimizer settings, and hardware should be recovered from the original training scripts before publication.
These details are important because reconstruction quality and latent-space behavior depend on both the model architecture and the training configuration.
6. What experiments did you conduct?
The project focused on implementing the VAE objective, training the model, and exploring the behavior of its learned latent representations.
Latent dimensionality was identified as an experimental variable in the project description, with configurations ranging from 16 to 256 dimensions. Before presenting this as a controlled comparison, I would verify that these configurations were actually trained and evaluated under comparable conditions.
A rigorous comparison would hold the dataset, architecture family, optimization settings, and training budget fixed while varying the latent dimensionality.
It would then compare reconstruction loss, KL divergence, generated samples, and latent-space behavior.
Other useful extensions would include varying the KL-divergence weight, comparing reconstruction-loss formulations, and examining how regularization changes the information retained by the latent representation.
I would distinguish experiments that were actually completed from those that remain proposed extensions. I do not have verified evidence of a systematic KL-weight sweep or a completed quantitative comparison across all latent dimensions.
7. What results can you demonstrate?
The main result to demonstrate is the end-to-end VAE implementation and its behavior on image data.
Useful evidence would include:
- Original images alongside their reconstructions.
- Samples generated by drawing latent vectors from the prior.
- Latent interpolations between selected examples.
- Training curves showing reconstruction loss and KL divergence separately.
- A comparison of reconstruction quality across verified latent dimensions.
These visualizations would help communicate what the model learns and how the latent representation affects generation.
I do not have verified quantitative reconstruction metrics or a documented controlled benchmark from the project records currently available. I would therefore avoid claiming specific improvements in reconstruction quality or latent-space organization until those results can be recovered.
8. What difficulties did you encounter?
Several conceptual and implementation challenges are central to training VAEs.
Reconstruction versus regularization: Excessive emphasis on the KL term can encourage the approximate posterior to remain close to the prior at the expense of preserving useful information about the input. Too little regularization can weaken the structure needed for meaningful prior sampling.
Reconstruction quality: A lower-dimensional latent representation necessarily constrains the information available to the decoder. This can limit how accurately the model reconstructs complex images.
Gradient flow through sampling: The reparameterization trick must be implemented correctly to allow the encoder to receive gradients through the sampled latent representation.
Loss scaling: Reconstruction loss and KL divergence can have very different numerical scales. The reduction method and relative weighting affect the behavior of the optimization objective.
Dataset complexity: A model that produces reasonable reconstructions of handwritten digits may struggle with the greater visual complexity of natural images.
Posterior collapse is another important failure mode in VAEs, but I would only describe it as an observed problem in this project if the training logs or latent statistics show evidence of it.
9. What did you learn, and what would you explore next?
The project strengthened my understanding of probabilistic latent-variable models and the role of regularization in representation learning.
I learned how the encoder, latent distribution, decoder, and ELBO fit together, and why the reparameterization trick is essential to training a VAE through ordinary gradient-based optimization.
The project also helped me understand that a latent representation is shaped by the objective used to learn it. Reconstruction encourages the preservation of input information, while KL regularization constrains the distribution of latent representations.
If I extended the project, I would investigate:
- How latent dimensionality affects reconstruction quality and the organization of the learned space.
- How KL-divergence weighting changes the reconstruction–regularization trade-off.
- Whether latent interpolations produce smooth and meaningful transitions between examples.
- How convolutional architectures affect image reconstruction on more complex datasets.
- How VAEs compare with diffusion models in sample quality, generation cost, and the kinds of representations they learn.
I would also consider comparing standard VAEs with beta-VAEs and other variants designed to encourage particular latent-space properties, while clearly separating those extensions from the original implementation.
10. Where can people see the work?
GitHub repository: To be confirmed from the original VAE project files.
For the portfolio, I would include the implementation, a concise derivation of the ELBO, reconstruction grids, prior-generated samples, and latent-interpolation visualizations if those artifacts are available.
I would also include separate training curves for reconstruction loss and KL divergence, since they reveal more about the model's behavior than a single combined loss.
The goal is to show how the mathematical formulation translates into a working generative model, while making the implementation and experimental evidence easy to inspect.
Overall, this project demonstrates a foundational exploration of probabilistic generative modeling: implementing a VAE, understanding its optimization objective, and investigating how latent representations support reconstruction and generation.