Autoencoders and VAEs

Networks that learn by compressing and rebuilding: bottlenecks, the link to PCA, latent spaces, denoising, anomaly detection, and variational autoencoders.

Intermediate lesson, about 40 minutes, with interactive demos and a quiz.

What you will learn

Compress, then rebuild

When Stable Diffusion draws a picture, it never works on pixels until the very last step. It works on a compressed version of the image about 48 times smaller, and a network called an autoencoder turns that back into pixels. The same idea flags faulty parts on production lines, cleans up noisy scans, and lets researchers look inside language models.

What is it?

An autoencoder is a neural network trained to output its own input. That sounds pointless, until you force the data through a narrow middle layer, the bottleneck. To rebuild 64 pixels from 2 numbers, the network has to discover what actually varies between its inputs and throw the rest away.

Why does it matter?

  • Compression that bends. Hinton and Salakhutdinov showed that a deep autoencoder with a 30-number code rebuilt handwritten digits and faces far more faithfully than PCA with 30 components.
  • Spotting the unusual. Trained only on normal data, an autoencoder rebuilds anything unusual badly, and that error is an alarm that needs no examples of faults.
  • Generating. The variational autoencoder (VAE) turns the bottleneck into a space you can sample from, and its descendants are the compressors inside today’s image and video generators.

How does it work?

This lesson builds up in steps: the architecture and its loss, the exact link to PCA, what the compressed codes look like, two practical uses (denoising and anomaly detection), why a plain autoencoder is a poor generator, and how the VAE fixes it. Every demo trains a real network in your browser, from scratch, in a second or two.

New to neural networks? Start with layers, activations and training there.

Key takeaways

  • An autoencoder is trained to rebuild its input after squeezing it through a narrow bottleneck.
  • The bottleneck forces it to keep what varies in the data and discard the rest.
  • Autoencoders compress, denoise, detect anomalies and, as VAEs, generate.

Encoder, bottleneck, decoder

Two networks glued back to back, trained together on one number: how far the output is from the input.

What is it?

The encoder f maps an input x to a short code z = f(x), also called the latent vector. The decoder g maps the code back to a reconstruction x̂ = g(z). The code’s length is the width of the bottleneck.

How does it work?

The reconstruction loss measures how far x̂ is from x, and backpropagation pushes its gradient through the decoder, through the bottleneck, and on through the encoder. No labels are needed: the input is its own target, which makes this self-supervised learning.

L(x) = ‖x − g(f(x))‖² or L(x) = −Σᵢ [ xᵢ log x̂ᵢ + (1 − xᵢ) log(1 − x̂ᵢ) ]

Squared error suits real-valued data such as points or sensor readings. For pixels between 0 and 1, the decoder usually ends in a sigmoid and the loss is binary cross-entropy summed over pixels, which is what the glyph demos below use.

The bottleneck is what makes it interesting. If the code were as wide as the input, the network could learn to copy every pixel straight through and learn nothing about the data. Narrowing the code is one way to stop that; later in the lesson you will meet two others, adding noise to the input and adding a penalty on the code.

Why does it matter?

Once trained, the halves are useful separately. The encoder alone is a feature extractor: its codes can feed a classifier or a search index. The decoder alone is a renderer: give it a code and it draws. Everything else in this lesson is a variation on which half you use, and what you ask of the code.

How the gradient flows back through both halves is ordinary backpropagation.

Key takeaways

  • The encoder maps x to a short code z; the decoder maps z back to a reconstruction x̂.
  • Both are trained together to minimise reconstruction loss, with no labels: the input is the target.
  • Something must stop the network copying its input straight through; a narrow bottleneck is the simplest way.

The latent space

Every input gets a code. Put all the codes on one plot and you have a map of the data that the network drew for itself.

What is it?

The latent space is the space of all possible codes. With a 2-number bottleneck it is a plane, and you can plot it: each training input becomes a dot. The word latent means hidden: these are variables nobody measured, which the network invented to describe the data.

How does it work?

The demo uses 8 × 8 glyphs from five families (vertical bars, horizontal bars, rising and falling strokes, boxes), each drawn at a random position, size or slant. An autoencoder (64 → 32 → 2 → 32 → 64) trains on 250 of them. It is never told the families. The colours are added afterwards, only so you can see where each family went.

Moving along a strand changes one thing smoothly, such as where the bar sits, because that is what the network needed to encode. Two numbers are enough to rebuild the training glyphs with an average squared error of about 0.027 per pixel, and new ones at about 0.04. But look at the axes: the codes stretch over tens of units in a lopsided shape, and between the strands are regions no training glyph was mapped to. What the decoder draws there was never checked by the loss.

Why does it matter?

A good latent space puts similar things near each other, which is what search, recommendation and clustering need. This is the same idea as word and sentence embeddings, learned in a different way. The gaps between strands are the first hint of a problem for generation, which the last part of this lesson takes up.

The Embedding Explorer shows another learned space where distance means similarity.

Key takeaways

  • The latent space is the space of codes; with two numbers you can plot it and decode any point in it.
  • Without labels, similar inputs end up near each other, because that makes them easier to rebuild.
  • A plain autoencoder’s latent space has an arbitrary scale and shape, with gaps the loss never checked.

Denoising autoencoders

Damage the input on purpose, keep the clean version as the target, and the network has to learn what clean data looks like.

What is it?

A denoising autoencoder is trained to rebuild a clean input from a corrupted copy of it: some pixels switched off or set at random, or noise added. Vincent and colleagues introduced it as a better way to learn features: a network cannot undo damage by copying, so it must capture how the parts of the input depend on each other.

How does it work?

Only the training input changes. Each step, each glyph gets a fresh dose of damage (here, 20% of its pixels set to black or white at random), is encoded and decoded, and the output is compared with the clean original. The architecture and loss are unchanged.

The denoising autoencoder keeps its advantage even at 40% damage (0.055 against 0.110 for the plain one), more than it ever saw in training. It has learned what glyphs look like, not just how to compress them. At 0% the order flips: a network trained to distrust its input is a little worse at reproducing clean ones exactly.

Why does it matter?

Denoising is useful in its own right, for scans, audio and medical images. It is also the core of a much bigger idea: diffusion models generate images by learning to remove noise, a little at a time, starting from pure static.

Key takeaways

  • A denoising autoencoder learns to rebuild clean inputs from deliberately corrupted ones (Vincent et al., 2008).
  • Copying cannot undo damage, so it must learn the structure of the data; it also generalises to heavier noise.
  • Learning to remove noise is the central step of diffusion models.

Spotting the unusual

Faults are rare and every one is different. Instead of learning what faults look like, learn what normal looks like and watch for misfits.

What is it?

Anomaly detection by reconstruction error: train an autoencoder only on normal data, then score each new input by how badly the autoencoder rebuilds it. Inputs like the training data come back almost unchanged; anything else is rebuilt as the nearest normal thing the decoder knows, and the difference is large.

How does it work?

Choose a threshold on the error, often from errors on held-out normal data, and flag everything above it. Moving the threshold trades missed anomalies against false alarms, the familiar precision and recall trade-off. The demo uses a 4-number code, trained on the five glyph families, and tests it on 50 new normal glyphs and 20 odd ones of five kinds.

At the starting threshold (0.079) it flags 19 of the 20 odd glyphs with 2 false alarms out of 50. Raise it to about 0.13 and the false alarms vanish, but only 16 odd glyphs are caught. The hardest cases are the ones that resemble a family: a plus is half a vertical bar and half a horizontal one, and a solid block is not far from a thick box. Look at the reconstructions: the decoder turns every odd glyph into the nearest thing it knows.

Why does it matter?

This pattern is used for machine sensor data, network traffic, card transactions and visual inspection, where faults are too rare and varied to collect. It is the nonlinear version of the PCA trick at the end of the PCA lesson.

Choosing the threshold is a precision and recall decision; Model Evaluation covers how to make it.

Key takeaways

  • Train only on normal data; a high reconstruction error marks an input as unusual.
  • The threshold trades missed anomalies against false alarms.
  • Anomalies that resemble normal data are the hardest, and an autoencoder that rebuilds everything detects nothing.

Why a plain autoencoder is a poor generator

You have a decoder that turns two numbers into a glyph. Pick two numbers at random and you should get a new glyph. Usually you do not.

What is it?

To generate, you need to know where in the latent space to pick codes from. A plain autoencoder never says. Its loss only cares that each training input is rebuilt, so the codes can sit at any scale, in any lopsided shape, with gaps between them, and nothing checks what the decoder draws in the gaps.

How does it work?

The natural way to sample is to draw each code number from a standard normal, N(0, 1): mean 0, spread 1. The demo retrains the same 2-number autoencoder and decodes random codes, either from N(0, 1) or uniformly from the box around the training codes, then measures how far each drawing is from the closest real glyph.

Over 80 draws from N(0, 1), the decoded glyphs are on average 0.11 away from the nearest real glyph, four times the reconstruction error of real ones, and nearly half are more than three times worse. The training codes spread over roughly −4 to 20 on one axis, so a standard normal only visits one corner of the map. Sampling from the box does better (0.05 on average), but you only know the box because you encoded the training set, and about 1 draw in 9 still lands in a gap.

Why does it matter?

The fix needs two things: a known distribution for the codes, so you know where to sample, and a latent space with no gaps, so that every sample decodes to something sensible. That is what the variational autoencoder adds.

Key takeaways

  • Generating means decoding new codes, so you need to know which codes are valid.
  • A plain autoencoder’s codes have no set scale or shape, and the gaps between them were never trained.
  • Random codes from a standard normal often decode to smudges: in this demo, nearly half did badly.

Variational autoencoders

Instead of one exact code per input, encode a small cloud of likely codes, and keep every cloud near the same standard normal.

What is it?

A variational autoencoder (Kingma and Welling’s VAE) changes two things. Its encoder outputs a mean μ and a spread σ for each code number, a small Gaussian cloud of plausible codes rather than one point. And its loss adds a penalty that keeps each cloud close to the standard normal N(0, 1). To generate, draw z from N(0, 1) and decode it.

How does it work?

The reparameterisation trick. Training needs a gradient through the sampling step, and a random draw has none. So the VAE draws ε from N(0, 1) separately and computes z = μ + σ·ε. The randomness is now an input; z depends smoothly on μ and σ, and backpropagation works as usual.

The two terms of the loss. The reconstruction term asks the decoder to rebuild x from the noisy code. Because the code is jittered, nearby codes must decode to similar things, which fills in the space around each training point. The KL term measures how far each cloud is from N(0, 1) and penalises it, which pulls all the clouds towards the same region at the same scale.

KL = ½ Σⱼ ( μⱼ² + σⱼ² − 1 − log σⱼ² )

Summed over the code dimensions. The KL term is zero only when μ = 0 and σ = 1, that is, when the code carries no information about x. The two terms pull against each other.

The ELBO. The sum of the two terms is not arbitrary. Kingma and Welling showed that the reconstruction log-likelihood minus the KL is a lower bound on log p(x), the log-probability the model gives to the data: the evidence lower bound. Minimising reconstruction loss plus KL is maximising that bound, so a VAE is trained as a proper probability model of its data.

log p(x) ≥ E_q(z|x)[ log p(x | z) ] − KL( q(z | x) ‖ p(z) )

q(z | x) is the encoder’s Gaussian cloud, p(x | z) the decoder, p(z) = N(0, 1) the prior.

Why does it matter?

Plain autoencoderOne code per input. Codes anywhere, any scale, gaps between them. Good compressor, poor sampler.VAEA Gaussian cloud per input, pulled towards N(0, 1). The space is filled in, so random draws decode sensibly. Outputs tend to be blurrier.

The VAE was one of the first deep generative models that could be trained stably and could both encode and sample. The Generative AI lesson puts it next to GANs, autoregressive models and diffusion.

Key takeaways

  • A VAE’s encoder outputs a mean and a spread; the code is sampled as z = μ + σ·ε so gradients can flow (the reparameterisation trick).
  • The loss is reconstruction plus a KL term that pulls every code cloud towards N(0, 1); together they form the negative ELBO.
  • Because the space is filled in and centred, codes drawn from N(0, 1) decode to plausible new samples.

The β trade-off and blurry samples

Reconstruction wants each code to say as much as possible about its input. The KL term wants each code to say nothing. One dial sets the balance.

What is it?

A β-VAE multiplies the KL term by a weight β. With β = 1 it is the standard VAE. Higgins and colleagues showed that β above 1 can make each code dimension capture a separate factor of variation, at a cost in reconstruction quality. β = 0 drops the KL term entirely.

loss = reconstruction + β × KL

How does it work?

Small β lets codes spread out and stay precise: sharp reconstructions, but a lumpy space. Large β squeezes every code towards N(0, 1), so the code carries less about each input and the decoder hedges. When a decoder trained with squared or cross-entropy error is unsure between several sharp answers, its best guess is their average, and averages of images are blurry.

At β = 0 the means spread to about ±18 and the KL is over 70 nats; samples from N(0, 1) miss most of the codes, and the first eight are on average 0.096 from any real glyph. At β = 0.5 to 1 the means sit mostly within the circles, the decoded grid shows every family blending smoothly into the next, and the samples improve to about 0.03. At β = 4 the KL falls to about half a nat and the reconstructions blur (squared error 0.085 against 0.049 at β = 1): the code barely depends on the input any more, which is called posterior collapse when it happens in full.

Key takeaways

  • β weights the KL term: low β gives sharp reconstructions and a lumpy space; high β gives a smooth space and blur.
  • Blur comes from hedging: an uncertain decoder trained on per-pixel loss outputs an average.
  • β = 1 is the standard VAE and the true ELBO; other values trade one goal for the other on purpose.

Where autoencoders work now

VAEs are rarely used on their own to generate images any more. Their encoders and decoders are inside almost everything that does.

What is it?

  • The compressor in latent diffusion. Rombach and colleagues trained an autoencoder with only a light KL penalty (or a codebook), then ran diffusion in its latent space. In Stable Diffusion a 512 × 512 × 3 image becomes a 64 × 64 × 4 latent, about 48 times fewer numbers. The diffusion model generates a latent; one decoder pass turns it into pixels.
  • Image tokenisers. The VQ-VAE snaps each encoder output to the nearest entry in a learned codebook, turning an image into a grid of discrete tokens. VQGAN sharpened such tokenisers with perceptual and adversarial losses. A transformer can then generate images token by token, just as a language model generates text.
  • Looking inside language models. Sparse autoencoders, trained on a model’s internal activations with a penalty that keeps most code numbers at zero, pull out features that often match human concepts.

How does it work?

In each case the autoencoder is trained once, separately, and then frozen. The expensive model (a diffusion network or a transformer) works in its compact latent space. The autoencoder’s job is to make that space small, and its decoder’s job is to make the result look sharp, which is why these autoencoders are trained with extra perceptual and adversarial losses rather than per-pixel error alone.

Why does it matter?

Running a generator on 16,384 numbers instead of 786,432 is a large part of what made high-resolution image and video generation affordable. The trade-off from the β demo still applies: compress too hard and fine detail, like small text and faces, is lost before the generator ever sees it.

The Diffusion Lab shows the denoising process that runs inside these latent spaces.Interpretability explains sparse autoencoders and the features they find.

Key takeaways

  • Latent diffusion generates in an autoencoder’s compressed space, about 48 times smaller for Stable Diffusion, and decodes once at the end.
  • VQ-VAE and VQGAN turn images into discrete tokens that a transformer can generate.
  • Sparse autoencoders are a key tool for finding human-readable features inside language models.

Check your understanding

Six situations where autoencoders help, or need care. Each asks you to apply an idea, not recall it.

Question 1 of 6

A factory trains an autoencoder on 50,000 photos of good circuit boards. On the line, a board with a scratch it has never seen comes past. How can the autoencoder flag it, with no photos of scratches to learn from?

Next, see how VAEs fit among other generators in Generative AI, or watch denoising build an image in the Diffusion Lab.

References

Papers behind the methods and claims in this lesson.

References

  1. [1]

    Reducing the dimensionality of data with neural networks(opens in a new tab)

    Geoffrey E. Hinton & Ruslan R. Salakhutdinov, 2006

    Science 313(5786), 504-507. Deep autoencoders, pretrained layer by layer, compress images and documents far better than PCA.

  2. [2]

    Neural networks and principal component analysis: learning from examples without local minima(opens in a new tab)

    Pierre Baldi & Kurt Hornik, 1989

    Neural Networks 2(1), 53-58. Proves that a linear autoencoder trained on squared error has one minimum (up to equivalent solutions): projection onto the subspace of the top principal components.

  3. [3]

    Extracting and composing robust features with denoising autoencoders(opens in a new tab)

    Pascal Vincent, Hugo Larochelle, Yoshua Bengio & Pierre-Antoine Manzagol, 2008

    ICML 2008. Train an autoencoder to rebuild clean inputs from deliberately damaged ones, and it learns more useful features.

  4. [4]

    Auto-encoding variational Bayes(opens in a new tab)

    Diederik P. Kingma & Max Welling, 2013

    arXiv (ICLR 2014). The variational autoencoder, the reparameterisation trick and the evidence lower bound used to train it.

  5. [5]

    β-VAE: learning basic visual concepts with a constrained variational framework(opens in a new tab)

    Irina Higgins, Loic Matthey, Arka Pal et al., 2017

    ICLR 2017. Weights the KL term by β; with β above 1 the latent dimensions tend to capture separate factors, at a cost in reconstruction.

  6. [6]

    High-resolution image synthesis with latent diffusion models(opens in a new tab)

    Robin Rombach, Andreas Blattmann, Dominik Lorenz, Patrick Esser & Björn Ommer, 2022

    CVPR 2022. Runs diffusion in the latent space of a pretrained autoencoder (lightly KL- or VQ-regularised), the design behind Stable Diffusion.

  7. [7]

    Neural discrete representation learning(opens in a new tab)

    Aaron van den Oord, Oriol Vinyals & Koray Kavukcuoglu, 2017

    NeurIPS 2017. VQ-VAE: the encoder’s output is snapped to the nearest entry in a learned codebook, turning an image into a grid of discrete tokens.

  8. [8]

    Taming transformers for high-resolution image synthesis(opens in a new tab)

    Patrick Esser, Robin Rombach & Björn Ommer, 2021

    CVPR 2021. VQGAN: a codebook autoencoder trained with perceptual and adversarial losses, whose image tokens a transformer then models.

Related