An autoencoder is a neural network trained to compress its input into a small internal representation and then reconstruct the original input from that representation. It pairs an encoder, which squeezes the data down, with a decoder, which rebuilds it. Because the goal is simply to reproduce the input, no labels are needed.
Key takeaways
- Autoencoders learn by trying to output exactly what they receive.
- The narrow middle layer, called the bottleneck or latent space, forces the network to keep only essential features.
- They are a form of unsupervised or self-supervised learning.
- Common uses are anomaly detection, denoising, compression and feature learning.
- Variational autoencoders (VAEs) can generate new data, and a VAE-style autoencoder is part of latent diffusion models.
An autoencoder explained in simple terms
Imagine describing a friend’s face over the phone in exactly ten words so someone else can sketch it. You cannot describe every freckle, so you pick the most telling features. If the sketch looks right, your ten words captured what matters. An autoencoder learns to do this: the encoder picks the “ten words”, the decoder draws the sketch, and training improves both until the sketch closely matches the original.
How an autoencoder works
- Encoder: layers progressively reduce the input, say a 784-pixel image, down to a small code, say 32 numbers.
- Bottleneck: the compact code holds the learned summary.
- Decoder: layers expand the code back to 784 pixels.
- Loss: a loss function such as mean squared error compares the reconstruction with the original.
- Training: backpropagation adjusts both halves to reduce reconstruction error.
A worked example: spotting fraud
Train an autoencoder only on normal card transactions. It becomes good at reconstructing normal patterns, with low reconstruction error. When a fraudulent transaction arrives, with an unusual amount, location and time combination, the autoencoder has never learned to rebuild that pattern, so the error is high. Set a threshold on reconstruction error, and high-error transactions are flagged for review. No labelled fraud examples were needed.
Types of autoencoders
| Type | Key idea | Typical use |
|---|---|---|
| Undercomplete | Small bottleneck forces compression | Feature learning, dimensionality reduction |
| Denoising | Input is corrupted; the model rebuilds the clean version | Removing noise from images or audio |
| Sparse | Only a few units may be active at once | Learning distinct features |
| Variational (VAE) | Learns a smooth probability distribution in latent space | Generating new samples, latent spaces for image generators |
| Convolutional | Uses CNN layers | Image compression, image anomalies |
| Masked | Hides parts of input and reconstructs them | Self-supervised pre-training for vision |
The VAE was introduced by Diederik Kingma and Max Welling in 2013. Because its latent space is smooth and continuous, you can sample new points and decode them into new, plausible data.
Autoencoders in generative AI
Latent diffusion models, the approach behind Stable Diffusion (2022), use an autoencoder to compress images into a smaller latent space. The diffusion process runs in that compact space, which is much cheaper than working with full-size pixels, and the autoencoder’s decoder turns the final result back into an image.
Autoencoder vs PCA
Principal component analysis (PCA) also compresses data, but only with straight-line (linear) combinations. An autoencoder with non-linear activation functions can capture curved, complex structure. A linear autoencoder with squared-error loss learns essentially the same subspace as PCA.
Limitations
- Can simply learn to copy input if the bottleneck is too wide, learning nothing useful.
- Reconstructions are often blurry, especially for plain autoencoders on images.
- Anomaly thresholds need careful tuning to balance false alarms and misses.
- The latent features are usually hard to interpret.
