Variational Autoencoders: Learning a Space of Possible Data Instead of a Single Copy

A Compressor That Can Also Invent

An ordinary autoencoder is a pair of networks trained on a simple task. The encoder squeezes an input, say a 64 by 64 face, into a short vector of numbers, and the decoder tries to rebuild the original face from that vector. If the reconstruction is good, the short vector must hold most of what mattered about the image. It has a strange weakness. Pick a random vector, hand it to the decoder, and the output is usually garbage.

The reason is that nothing in the training asks the codes to be organized. The encoder is free to scatter faces across the code space in isolated clusters, with large empty regions between them that the decoder never saw during training.

The variational autoencoder, or VAE, changes the training so that the code space becomes something you can sample from. It was introduced by Diederik Kingma and Max Welling in «Auto-Encoding Variational Bayes» (posted in December 2013 and presented at ICLR 2014), and in parallel by Danilo Rezende, Shakir Mohamed and Daan Wierstra at ICML 2014. Both papers arrived at the same model from the side of probabilistic inference rather than compression.

Latent Variables and an Intractable Integral

The probabilistic story goes like this. Assume every image x was produced by first drawing a hidden vector z from a simple distribution, usually a standard Gaussian, and then passing z through a decoder network that outputs a distribution over images. Training the model means making the observed images likely. The probability of one image is an average over every z that could have produced it, which is an integral over the whole latent space with a neural network inside it. Nobody can compute that integral exactly.

The other direction is just as hard. Given an image, which z probably generated it? That is the posterior distribution, and by Bayes’ rule it also depends on the same impossible integral. Classical variational inference approximates it with a separate optimization for each data point, which is far too slow for millions of images.

Kingma and Welling’s move was to train a second network, the encoder, that looks at an image and directly outputs the parameters of an approximate posterior: a mean and a variance for each latent dimension. One forward pass replaces a per-image optimization. This is called amortized inference: the cost of learning to infer is paid once and shared across all inputs.

The Evidence Lower Bound

The training objective is the evidence lower bound, usually shortened to ELBO. It is a quantity that sits below the true log probability of the data, so pushing it up pushes the true value up as well. It splits into two terms that are easy to read.

The first is a reconstruction term: sample a z from the encoder’s distribution, decode it, and measure how well the result matches the input. The second is a Kullback-Leibler divergence between the encoder’s distribution and the standard Gaussian prior. This second term penalizes the encoder for placing codes far from the origin or for making them too narrow. Each image gets mapped to a small cloud of codes, and the KL term pushes all of those clouds to overlap with one shared bell curve.

That pressure is what fills the empty regions. Because the clouds overlap, the decoder learns to produce something sensible across the whole neighborhood of the prior, and sampling z from a standard Gaussian gives plausible new images.

The Reparameterization Trick

There is a technical obstacle in the middle of all this. The reconstruction term requires sampling z, and a random sampling step has no gradient. Backpropagation cannot tell the encoder how to change its mean and variance if the only thing between them and the loss is a dice roll.

The fix is short. Instead of sampling z directly from a Gaussian with mean μ and standard deviation σ, sample a noise value ε from a standard Gaussian, which does not depend on any parameter, and compute z = μ + σε. The randomness now enters from the side, and z is a deterministic, differentiable function of μ and σ. Gradients flow through it like through any other layer. The resulting estimator, which the paper named Stochastic Gradient Variational Bayes, can be optimized with the same stochastic gradient methods used for any network.

This one line of algebra made deep latent variable models trainable with standard tools.

Case: Molecules Drawn From a Continuous Space

One of the best known applications came from chemistry. In 2018 Rafael Gómez-Bombarelli and colleagues published «Automatic Chemical Design Using a Data-Driven Continuous Representation of Molecules» in ACS Central Science. They trained VAEs on molecules written as SMILES strings, the text notation chemists use to describe a structure. One model learned from about 108,000 small molecules in the QM9 dataset, the other from 250,000 drug-like molecules taken at random from the ZINC database, with latent spaces of 156 and 196 dimensions.

The attraction was that a discrete search over molecules became a continuous one. The authors trained a property predictor on top of the latent codes and then moved through the space with gradient-based optimization toward codes with better predicted properties, decoding the results back into candidate molecules. They chose a VAE over a plain autoencoder for the reason described above: a plain autoencoder left large dead areas that decoded into invalid strings.

The results also show the limits. Depending on where they sampled, the fraction of decoded strings that were valid SMILES ranged from about 70 percent down to below 1 percent, and every output had to be checked with cheminformatics software such as RDKit. A smooth latent space did not guarantee that the decoder respected the grammar of chemistry, and much of the later work on molecular generation focused on decoders that build graphs or follow grammar rules so that invalid outputs cannot occur.

Posterior Collapse

VAEs have a characteristic failure. If the decoder is powerful enough to model the data on its own, the cheapest way to satisfy the ELBO is to ignore z entirely. The encoder outputs the prior for every input, the KL term drops to zero, and the decoder generates plausible data without using any information from the code. The code becomes useless.

Samuel Bowman and colleagues ran into this when they paired a VAE with a recurrent language model decoder in «Generating Sentences from a Continuous Space» (CoNLL 2016). Without intervention, their model stored very little information in the latent variable. They used two remedies. KL annealing starts training with the KL weight at zero and raises it gradually, so the encoder learns to put information in z before the penalty applies. Word dropout replaces some of the decoder’s input words with an unknown token, which weakens the decoder and forces it to rely on the code. They reported that lowering the keep rate increased the information in z while the overall likelihood got somewhat worse.

Turning the KL Weight Up

If lowering the KL weight helps a VAE use its code, raising it does something else. In β-VAE (Higgins et al., ICLR 2017, from DeepMind), the KL term is multiplied by a factor β greater than one. The stronger pressure toward an independent Gaussian prior encouraged the model to dedicate separate latent dimensions to separate factors of variation. On images of simple shapes, one dimension might come to control position, another scale, another rotation. The price was blurrier reconstructions.

This «disentanglement» line of research grew quickly, and in 2019 Francesco Locatello and colleagues tested it at scale in «Challenging Common Assumptions in the Unsupervised Learning of Disentangled Representations» (ICML 2019). They reimplemented six methods and six disentanglement metrics and trained more than 12,000 models on seven datasets. Their theoretical result was that unsupervised disentanglement is impossible without inductive biases on both the model and the data. Empirically, the methods did enforce the properties their losses encouraged, but well disentangled models could not be identified without some supervision, and random seeds and hyperparameters often mattered more than the choice of method.

Discrete Codes and a Compressor for Diffusion

Two later developments moved VAEs from being the generator to being a component. In «Neural Discrete Representation Learning» (NIPS 2017), Aaron van den Oord, Oriol Vinyals and Koray Kavukcuoglu replaced the continuous Gaussian code with a codebook of learned vectors. The encoder output is snapped to the nearest codebook entry, and gradients skip over the non-differentiable snapping step by being copied straight from the decoder input to the encoder output. The authors noted that this VQ-VAE avoids posterior collapse even with a strong autoregressive decoder. Those discrete codes later became tokens for transformer-based image and audio generators.

The second development is the latent diffusion model of Rombach and colleagues (CVPR 2022), the architecture behind Stable Diffusion. Running diffusion directly on pixels is expensive, so they first train an autoencoder that compresses an image by a factor of 8 in each spatial direction, and the diffusion model works on those compressed codes. One of their variants is regularized with a slight KL penalty toward a standard normal, the same term as in a VAE, but with a weight small enough that its job is to keep the latent space well behaved rather than to allow sampling from the prior.

Closing Thought

The KL term in a VAE is often described as a regularizer, but it is easier to understand as a budget. It measures how many bits about the input the code is allowed to carry beyond what the prior already says. Set the budget near zero and the decoder learns to manage without the code. Raise it and the code becomes cleaner and more compressed while the reconstructions get blurrier. Posterior collapse, β-VAE, KL annealing and the tiny KL weight in latent diffusion are all choices about the same number. A VAE does not decide on its own what its latent space should mean. That decision is mostly made by whoever chooses how much information the code is allowed to carry.

By: Max Johnson B.

Comentarios

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *