Variational Autoencoders
Before delving into variational autoencoders, let's do a brief review of autoencoders.
Autoencoders
Autoencoders are a family of neural networks whose goal is to learn a simplified representation of the input data.
Their operation is based on compressing and reconstructing data. They generally consist of two networks:
- An Encoder that compresses the input data into a lower-dimensional space, known as the latent space, .
- A Decoder that reconstructs the data from the latent space, .
The parameters of each network are optimized jointly so that the reconstructions are as similar as possible to the original data, .

The idea originated in the 1980s and was taken up again, among other works, by Hinton and Salakhutdinov[hinton2006reducing] in 2006. Today it has direct applications in tasks such as anomaly detection, manifold learning, compression, or data generation.
The loss function of a conventional autoencoder measures how well the model can reconstruct the original input once the data has been compressed into the latent space. Therefore, the goal is for the output to be as similar as possible to the input . The choice of error function depends mainly on the nature of the input data.
-
Mean Squared Error (MSE)
This is the most commonly used loss function for continuous data (for example, images or time series).
The goal is to minimize the squared difference between the original input and the reconstructed output :
-
Binary Cross Entropy
This is used when the input data is normalized in the range [0, 1] and interpreted as probabilities, or when the data is binary. It is very common in autoencoders where the output layer has a sigmoid activation function.
There are many variants of autoencoders, such as Denoising Autoencoders[vincent2008extracting], Sparse Autoencoders[makhzani2013k], or Contractive Autoencoders[rifai2011contractive]. From here on we will focus on Variational AutoEncoders (VAE)[kingma2013auto] for their generative properties.
VAE
Conventional autoencoders map inputs to a fixed set of vectors. This means that if you sample a point in the latent space where no representation exists, the output will be unrealistic because the decoder doesn't know how to reconstruct an input from an empty region.
The novelty that VAEs introduce is that instead of mapping data to fixed vectors, they are mapped to a distribution.
Together with regularization towards a common prior, this favors a latent space that is continuous and structured. Therefore, you can choose a point that doesn't correspond exactly to the projection of any training data, pass it through the decoder, and obtain a new, coherent sample . That is, generating data in the form .
This is precisely a generative model of latent variables.

The relationship between the input data and the latent vector is given by:
- Prior:
- Likelihood:
- Posterior:
The distribution of the data can then be modeled as:
Unfortunately, is not easy to compute, since it is very costly to check and sum over all possible values of (it is usually called 'intractable'). To solve this problem, VAEs resort to variational inference. Precisely, variational inference is one of the most widely used methods in Bayesian inference and is used to approximate intractable integrals. In other words, it is a technique used to approximate complex distributions.
The complex distribution we want to approximate is the posterior . To do this, a family of simple distributions is assumed, and the approximation is posed as an optimization problem in which we search within for the distribution that best approximates the target distribution. denotes the parameters of the chosen family of distributions.
In this way, we introduce:
- An encoder that approximates the posterior .
- A decoder that models the conditional probability .

The solution to this problem is the reparametrization trick. Instead of having the network directly generate the random value , the process is split into two parts:
- The predictable part (the network): The encoder learns the mean and standard deviation in a deterministic and differentiable way.
- The random part (): An external noise source independent of the network's weights is introduced, following a standard Gaussian distribution .
Once the network and the randomness are separated, they are simply combined with:
In this way, the randomness stays confined exclusively to , and since it is an external variable fixed during the computation, the network doesn't need to learn how to differentiate it.
Loss function: ELBO
The distribution estimated by the encoder, , must approximate the true posterior . To quantify the distance between these two distributions, the Kullback–Leibler divergence is used, , which measures how much information is lost if distribution is used to represent .
In this case we want to minimize with respect to .
Using Bayes' theorem, this can be rewritten as:
- The first term is the log-likelihood of the data, which we want to maximize.
- The second term is the expected log-likelihood of the data under the posterior approximated by the encoder.
- The third term is the KL divergence between the approximated posterior and the prior.
Combining these terms, we can define the loss function of a VAE as:
known as the Evidence Lower Bound (ELBO).
- The first term is the reconstruction of , which tends to make the encoding-decoding scheme as efficient as possible by maximizing the log-likelihood with sampling from (encoder).
- The second term regularizes the latent variables by minimizing the KL divergence between the variational approximation (encoder) and the prior distribution of . Normally, the family of simple distributions we choose is a diagonal Gaussian. Therefore, when you pass an input through the encoder, it returns the values of and for that specific input.
The ELBO is maximized; when we talk about the loss function we minimize its negative, .
Applications
The applications of VAEs are countless. Some examples beyond image generation are text modeling[yang2017improved], anomaly detection, remaining useful life estimation of industrial systems[costa2022variational], arrhythmia classification[costa2021semi], or molecular design[de2023population].
Posterior collapse
VAEs are known to be susceptible to posterior collapse. This is a problem that refers to the situation in which the posterior approximated by the encoder collapses onto the prior. That is, the encoder learns for any input . This happens because the KL the second term of the ELBO, is reduced to zero and the latent variables stop encoding information about the input data.
As a consequence, the decoder learns to ignore entirely and generates reconstructions based solely on global statistics of the data, nullifying the usefulness of the latent space.

Training tricks
There are several approaches to solving this problem, including:
- KL Divergence Annealing: gradually increases the weight of the KL divergence term in the loss function during training, allowing the model to first learn to reconstruct the data before imposing strong regularization on the latent space.
- Free Bits: sets a minimum threshold for the KL divergence in each dimension of the latent variable, ensuring that each one contributes at least a "minimum amount of information" and preventing them from collapsing to trivial values.
- Minimum Desired Rate: similar to Free Bits, but applies a penalty to maintain a minimum amount of information in the latent space, forcing the compression not to lose relevance.
- Limiting the decoder's capacity or adding connections from : reduces the possibility of the decoder explaining the data without resorting to the latent variable.
VAE Variants
VAEs trained with a pixel-by-pixel reconstruction loss tend to produce blurry images. Why does this happen?

Typically, metrics such as RMSE or BCE are used to quantify the quality of the reconstructions, as we mentioned earlier. Therefore, the network's goal is to minimize these metrics.
The problem arises when, for the same latent representation, several plausible reconstructions exist. In that case, to minimize a pixel-by-pixel loss, the network can make a kind of cheat: predicting intermediate values between those alternatives. In an image, that conditional average between possibilities tends to produce blurry edges and details.
β-VAE
One way to control the balance between reconstruction quality and latent space regularization is to introduce a hyperparameter that adjusts the weight of the KL term:
This is what the β-VAE[higgins2017beta] consists of.
If is set, the objective function is the same one used in standard VAEs; if it is set to 0, the KL regularization disappears and the objective is reduced to the reconstruction term, bringing the behavior closer to that of a conventional autoencoder.
If is used, more bits are stored about each input (that is, more importance is given to the data) and, therefore, images can be reconstructed in a less blurry way. On the other hand, an advantage of using is that it encourages learning a "disentangled" latent representation. Intuitively, this means that each latent dimension represents a different factor/feature of the input data. For example, a model trained on photos of human faces might capture smoothness, skin color, hair color or length, emotion, whether glasses are worn, and many other relatively independent factors in separate dimensions.
In summary: designing a VAE is almost always a balancing act between "I want the reconstruction to look good" and "I want the latent space to make sense."
InfoVAE
To solve the problem of placing too much emphasis on the second term (regularization), causing latent representations that don't capture the structure of the data well and generated samples that lack diversity, InfoVAE[zhao2017infovae] modifies the loss function by adding a mutual information maximization term between the latent variables and the observed data . The InfoVAE loss function can be written as:
Where the first term is the reconstruction error, the second is the KL divergence between the approximated posterior distribution and the prior, and the third is the KL divergence between the marginal distribution of the latent variables and the prior distribution. The distribution is known as the aggregated posterior and describes how the encodings of the entire dataset are distributed in the latent space.
and are hyperparameters that control the relative importance of each term. In this way, the objective favors retaining information about while still approximating the aggregated posterior to the prior.
Multimodal VAEs
Multimodal VAEs[wu2018multimodal], [shi2019variational] are an extension of the standard VAE model to handle and learn joint representations of data coming from different modalities. These modalities can include text, images, audio, video, etc. The main idea is to capture the dependencies and correlations between different types of data, learning a shared latent representation that can be used for tasks such as data generation or imputation.
Several approaches have been proposed so far for multimodal VAE learning. This review gathers some of the most influential works.
Hierarchical VAEs
One limitation of standard VAEs is that they usually use a simple prior, such as a standard Gaussian. One way to increase their flexibility is to introduce a hierarchy of latent variables. By making both the inference model (encoder) and the generative model (decoder) hierarchical, more complex distributions can be represented.

The distribution we want to model then is:
Therefore, the ELBO would be expressed as follows:
Many hierarchical models have been published, among which VD-VAE (Very Deep VAE)[child2020very] stands out for image generation, and Bit-Swap[kingma2019bit] for data compression.
The VD-VAE architecture is a convolutional VAE. For each group of latent variables, the prior and posterior are diagonal Gaussians. In the decoder, it uses nearest-neighbor resampling instead of transposed convolutions and adds several design decisions to stabilize training. Together, this makes it possible to train a deep hierarchy using the original ELBO, without modifying the objective function.
Extra: Compression
In Bit-swap[kingma2019bit], a hierarchical VAE is presented, demonstrating that the bits-back compression scheme (BB-ANS) can be used with latent variable models. That is, this type of generative model can be used as efficient compression schemes. Let's take a look, but first: a bit of information theory.
Kolmogorov complexity
In information theory, the Kolmogorov complexity of an object , denoted , refers to the length of the shortest possible description of that object on a 'universal computer'. It represents the inherent complexity of a piece of information as a function of the length of the most concise binary program capable of generating it.
A shorter binary program implies a higher level of regularity or predictability in the data.
A longer binary program suggests a more complex, less compressible structure.
Similarly, the Kolmogorov complexity of given , , corresponds to the length of the binary program that, receiving as input, produces . Based on this idea, Bennett et al. [bennett1998information] derived the information distance metric, :
This metric essentially evaluates the existence of a simple program capable of transforming one object into another and, therefore, provides a measure of the relationship or amount of information shared between them. The simpler the conversion program, the more similar the objects are considered to be. However, Kolmogorov complexity is not theoretically computable but can only be approximated.
Compression
Compression techniques approximate Kolmogorov complexity, since they offer a practical means of representing data more concisely without losing essential information. The main goal of a lossless compressor is to minimize the number of bits needed to represent the data, subsequently allowing its exact decompression.
Compression methods achieve this goal by identifying and exploiting redundancies or patterns present in the data. To achieve the shortest possible compression length, symbols with higher probability receive shorter codes. According to Shannon's Source Coding Theorem [shannon1948mathematical], the length of the encoded bits is directly related to the entropy of the information source:
What compression algorithms seek is to effectively model the "real" distribution of the data :
-
A common strategy consists of dictionary-based methods, where patterns or sequences of data are identified and replaced with shorter codes. Among these are the Lempel-Ziv algorithms, which dynamically build a dictionary during compression [kosaraju2000compression].
-
Context-based models, such as adaptive arithmetic coding, adjust the encoding probabilities based on the context of previously encoded symbols [moffat1998arithmetic].
-
Entropy coding methods, such as Huffman coding, assign shorter prefix codes to the most probable symbols [moffat2019huffman].
Recent advances have incorporated Machine Learning techniques into compression algorithms. What models can we use to approximate the real distribution of the data?
Exactly, VAEs!!
Generative models as compressors
In a typical compression pipeline, data is represented as a sequence of symbols and encoded into a set of bits . Afterwards, the inverse process is applied to recover the original data.
-
Compression:
-
Decompression:
Lossless compression methods seek to minimize the number of bits without sacrificing information. For a symbol with probability , the ideal length is bits.
The better the compression, the better the approximation of . In a probabilistic context, can be expressed as the probability of observing the data under a given model:
where is the probability of observing the data given a specific set of parameters, is the prior distribution, and represents the model's parameters.
In the context of generative modeling, this expression is usually formulated in terms of the latent variable :
Sounds familiar, right?
Using this notation, the process of sending information in a compression scheme () proceeds as follows:
- The latent variable is encoded using .
- The sample is encoded using .
- Both and are transmitted.
On the other hand, receiving the information () follows these steps:
- is decoded using .
- is decoded using .
The number of bits involved initially corresponds to . The bits-back method allows recovering approximately bits used to select [frey1997efficient]. This idea connects variational inference with probabilistic compression.
The compression process described above can be interpreted according to the BB-ANS scheme [townsend2019practical]. Some extra bits are used to generate a sample via the encoder ; afterwards, is used to encode through , while the variable itself is encoded using the prior distribution . The reverse process allows recovering via , reconstructing via , and recovering the initial extra bits using .

The final length of the bit stream for a single data point is given by:
Notice that the costs and are transmitted, but the bits-back mechanism allows recovering approximately the extra bits used to select . What matters here is that coincides with the negative ELBO (negative Evidence Lower Bound, nELBO), expressed in bits:
Dividing by converts nats into bits. This cost corresponds to the loss function we minimize in a VAE.
-
The first term corresponds to the reconstruction of and favors an efficient encoding and decoding scheme by maximizing with samples from .
-
The second term is the regularization of the latent space by minimizing the KL divergence between the variational approximation and the prior distribution .
As a result, minimizing the negative ELBO maximizes a lower bound of and brings closer to the model's posterior .
This equivalence demonstrates that a properly optimized latent variable model, such as a VAE, is directly applicable to compression tasks, since it minimizes the achievable code length in the context of bits-back coding.
Jiang et al., in NPC (Non-Parametric learning by Compression)[jiang2022few], proposed a method based on this premise for image classification, outperforming supervised and semi-supervised methods.
Similarly, in Few-shot generative compression approach for system health monitoring [costa2025few] I proposed a method based on compression and VAEs to diagnose cases where monitoring data is abundant, but there are very few labeled instances.