Autoencoder
An autoencoder learns to compress data into a compact code and rebuild it, with no labels. Covers its structure, variants, anomaly detection, and code.
An autoencoder is a neural network trained to reproduce its own input. An encoder maps the input to a compact code, often called the latent representation, a decoder maps the code back to a reconstruction, and training minimizes the difference between the input and the reconstruction. Because the target is the input itself, autoencoders learn from unlabeled data, a form of unsupervised learning usually classed today as self-supervised learning. What makes the network useful is a constraint that stops it from copying the input straight through, such as a narrow bottleneck, added noise, masked inputs, or a sparsity penalty, which forces it to learn the structure that makes the data compressible. Autoencoders are used for dimensionality reduction, anomaly detection, denoising, pretraining, and as the compression stage of modern image generators.
How Autoencoders Work#
The encoder f maps an input x to a code z = f(x), the decoder g produces a reconstruction x̂ = g(z), and training minimizes a reconstruction loss such as the mean squared error between x and x̂ with backpropagation. In an undercomplete autoencoder, z has far fewer dimensions than x, so the code is a latent space summary that keeps only what the decoder needs. A linear autoencoder trained with squared error learns the same subspace as principal component analysis (PCA), a result Pierre Baldi and Kurt Hornik proved in 1989, and nonlinear activation functions let deeper autoencoders capture curved structure that PCA cannot. Geoffrey Hinton and Ruslan Salakhutdinov showed in a 2006 Science paper that deep autoencoders produced far better low-dimensional codes than PCA for images and documents.
Variants that do not depend on a narrow bottleneck corrupt the input instead and still score the reconstruction against the clean original:
Types of Autoencoders#
- Denoising: Adds noise to the input and trains the network to recover the clean original, as Pascal Vincent and colleagues described in 2008. The learned features are robust to the corruption, and the trained model can clean noisy images or signals.
- Sparse: Uses more code units than inputs but penalizes how many are active at once. Sparse autoencoders trained on the internal activations of large language models (LLMs) split them into many features that each tend to match one interpretable concept.
- Variational (VAE): The encoder outputs a probability distribution over codes, and a KL-divergence penalty keeps it close to a standard normal distribution, so decoding a randomly sampled code generates new data. Diederik Kingma and Max Welling introduced the VAE in 2013.
- Vector-quantized (VQ-VAE): Snaps each code vector to the nearest entry of a learned codebook through vector quantization, turning images or audio into discrete tokens that an autoregressive model can generate.
- Masked: Hides most of the input and reconstructs the missing parts. Masked autoencoders (MAE) hide 75% of an image's patches, and the encoder trained this way becomes a strong backbone for other vision tasks. Meta's Segment Anything Model (SAM), which runs in the Ultralytics package, builds its image encoder on an MAE-pretrained Vision Transformer, and SAM 2 on an MAE-pretrained Hiera encoder, so the features behind every promptable mask were first learned by filling in hidden patches.
Anomaly Detection With Reconstruction Error#
An autoencoder trained only on normal data reconstructs normal inputs well and unfamiliar ones poorly, so reconstruction error works as an anomaly score for manufacturing defects, fraudulent transactions, or failing machines, with no labeled anomalies needed. The PyTorch snippet below trains a small autoencoder on scikit-learn's bundled handwritten digits, leaving out every 7, and compares its error on held-out digits with its error on the 7s:
import torch
from sklearn.datasets import load_digits
from torch import nn
# 8x8 handwritten digits scaled to [0, 1]; every 7 is held out as the unseen anomaly
digits = load_digits()
x = torch.tensor(digits.data / 16, dtype=torch.float32)
normal, sevens = x[digits.target != 7], x[digits.target == 7]
train, test = normal[:1200], normal[1200:]
# Encoder: 64 pixels to an 8-number code; decoder: code back to 64 pixels
torch.manual_seed(0)
encoder = nn.Sequential(nn.Linear(64, 32), nn.ReLU(), nn.Linear(32, 8))
decoder = nn.Sequential(nn.Linear(8, 32), nn.ReLU(), nn.Linear(32, 64), nn.Sigmoid())
model = nn.Sequential(encoder, decoder)
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
for step in range(1000):
optimizer.zero_grad()
nn.functional.mse_loss(model(train), train).backward() # reconstruction error
optimizer.step()
with torch.no_grad():
for name, data in [("Held-out digits", test), ("Unseen 7s", sevens)]:
print(f"{name}: reconstruction error {nn.functional.mse_loss(model(data), data):.4f}")It prints a reconstruction error of 0.0163 for the held-out digits and 0.0547 for the 7s, 3.4 times higher on average, and a threshold on each image's error turns that gap into an anomaly flag. An autoencoder can also generalize too well and rebuild some anomalies accurately, so practical systems tune the bottleneck size and the threshold on validation data that includes known anomalies.
Autoencoders vs. Related Concepts#
- Generative adversarial networks (GANs): A GAN generator maps random noise to data and learns from a discriminator, with no encoder and no reconstruction target. VAEs also generate data, and their likelihood-based objective trains more stably but tends to produce blurrier images.
- U-Net: Shares the encoder-decoder shape but maps the input to a different target, such as a segmentation mask, and passes skip connections around the bottleneck, which in an autoencoder would let the network copy its input.
- Contrastive learning: Learns representations by comparing samples with each other, so its features capture what distinguishes images and ignore pixel-level detail that a reconstruction loss must keep.
- Embeddings: The code an encoder produces is one kind of embedding, and autoencoders are one of several ways to learn them.
Autoencoders in Image Generation#
Latent diffusion models such as Stable Diffusion run their diffusion process inside an autoencoder's latent space and decode the result back to pixels at the end. Stable Diffusion 1.x uses a KL-regularized autoencoder that compresses a 512×512 RGB image into a 64×64×4 latent, 48 times fewer values for the diffusion model to process at every denoising step.










