An autoencoder is a neural network trained to reproduce its input through a narrow intermediate representation. Because it must squeeze the input through a bottleneck, it learns a compact code that captures the data's main structure. Autoencoders are used for dimensionality reduction, denoising, anomaly detection, pretraining, and as the compression stage of modern generative models.
Parent topic: Generative Models. Probabilistic extension: Variational Autoencoders (VAE).
The encoder f maps input x ∈ Rn to a code z = f(x) ∈ Rd with d < n; the decoder g maps back to x̂ = g(z). Both are trained jointly to minimise reconstruction loss, usually mean squared error or binary cross-entropy. No labels are needed — the input is its own target, which makes this self-supervised learning.
A purely linear autoencoder with MSE loss learns the same subspace as PCA. Nonlinear activations and depth let it represent curved manifolds, which is why it can outperform PCA on images.
import torch
from torch import nn
from torchvision import datasets, transforms
class ConvAE(nn.Module):
def __init__(self, d=32):
super().__init__()
self.enc = nn.Sequential(
nn.Conv2d(1, 32, 3, 2, 1), nn.ReLU(), # 28 -> 14
nn.Conv2d(32, 64, 3, 2, 1), nn.ReLU(), # 14 -> 7
nn.Flatten(), nn.Linear(64 * 7 * 7, d))
self.dec = nn.Sequential(
nn.Linear(d, 64 * 7 * 7), nn.ReLU(), nn.Unflatten(1, (64, 7, 7)),
nn.ConvTranspose2d(64, 32, 4, 2, 1), nn.ReLU(), # 7 -> 14
nn.ConvTranspose2d(32, 1, 4, 2, 1), nn.Sigmoid()) # 14 -> 28
def forward(self, x):
z = self.enc(x)
return self.dec(z), z
ds = datasets.MNIST("data", train=True, download=True, transform=transforms.ToTensor())
dl = torch.utils.data.DataLoader(ds, batch_size=128, shuffle=True)
model = ConvAE()
opt = torch.optim.Adam(model.parameters(), lr=1e-3)
for epoch in range(5):
for x, _ in dl: # labels unused
noisy = (x + 0.3 * torch.randn_like(x)).clamp(0, 1) # denoising variant
x_hat, _ = model(noisy)
loss = nn.functional.binary_cross_entropy(x_hat, x)
opt.zero_grad(); loss.backward(); opt.step()
print(epoch, round(loss.item(), 4))
# anomaly score = per-sample reconstruction error
@torch.no_grad()
def score(x):
x_hat, _ = model(x)
return ((x - x_hat) ** 2).flatten(1).mean(1)
Trained only on normal examples (healthy machine sensor data, legitimate transactions, defect-free product images), an autoencoder reconstructs normal inputs well and unusual ones poorly. The reconstruction error becomes an anomaly score. Caveat: a powerful autoencoder can learn to reconstruct anomalies too, so keep the bottleneck tight and validate with known anomalies if any exist.
z gives garbage. Use a VAE to generate.For a dataset of N examples the plain autoencoder minimizes the average reconstruction loss L = (1/N) Σ ℓ(xi, g(f(xi))). With real-valued inputs, ℓ is usually squared error; with inputs scaled into [0, 1], such as normalized pixels, binary cross-entropy against a sigmoid output is common and tends to give sharper gradients near the extremes. Training is ordinary backpropagation with Adam or SGD: no labels, no adversary, no sampling.
The bottleneck is the regularizer. If the code dimension d is at least the input dimension n and nothing else constrains the network, the identity function is a perfect solution and the code learns nothing useful. Every useful autoencoder therefore restricts information somehow: a narrow code (undercomplete), noise on the input (denoising), an L1 or KL sparsity penalty on activations (sparse), a penalty on the Jacobian of the encoder (contractive), or quantization to a finite codebook (VQ-VAE).
Relation to PCA, worked through. Take an encoder z = Wx and decoder x̂ = W'z with no activations and squared error. The best rank-d reconstruction of centered data is the projection onto the top-d principal subspace (the Eckart-Young theorem), so the optimal W' spans that subspace. The learned weights need not be orthogonal or ordered by variance, but the span matches PCA. Adding a nonlinearity such as ReLU between layers is what lets the model represent curved structure, for example the many poses of a handwritten digit that lie on a low-dimensional manifold inside 784-pixel space.
| Choice | Guidance |
|---|---|
| Code size d | Start small (for example 8 to 64 for tabular or MNIST-scale images) and grow until validation reconstruction stops improving. For anomaly detection, smaller is often better. |
| Depth and width | Mirror the encoder in the decoder. Two or three hidden layers are enough for tabular data; images use strided convolutions down and transposed convolutions or upsampling up. |
| Loss | MSE for continuous values, BCE for [0,1] data, perceptual (feature-space) or adversarial losses when visual sharpness matters. |
| Output activation | Linear for standardized inputs, sigmoid for [0,1] inputs. A mismatch here is a frequent cause of poor reconstructions. |
| Regularization | Noise level for denoising (masking 15 to 75 percent of patches works for masked image models), sparsity weight, weight decay, early stopping on validation loss. |
| Tied weights | Using the encoder's transpose as the decoder halves parameters and can stabilize small models. |
A practical scoring pipeline looks like this. Split normal data into train and validation. Train on train. Compute reconstruction error on the validation set and set the threshold at a high percentile, which directly controls the expected false-positive rate on normal traffic. If a few labeled anomalies exist, use them to compare thresholds with precision and recall rather than accuracy, because anomalies are rare. Score per feature as well as per sample: the columns with the largest reconstruction error often explain why a row was flagged, which helps the people who triage alerts. Retrain on a schedule, since the definition of normal drifts. For comparison methods such as isolation forest and one-class SVM see Anomaly Detection.
| Method | Nonlinear | Generates samples | Typical use |
|---|---|---|---|
| PCA | No | No | Fast baseline compression, closed form |
| Autoencoder | Yes | Poorly | Compression, denoising, anomaly scores, pretraining |
| VAE | Yes | Yes, often blurry | Smooth latent spaces, latent diffusion compression |
| GAN | Yes | Yes, sharp | High-fidelity image synthesis, no encoder by default |
| Diffusion | Yes | Yes, high quality | State-of-the-art image and audio generation |
Always benchmark an autoencoder against PCA first. On many tabular problems PCA's reconstruction error is close and it costs nothing to train; the autoencoder earns its place when the structure is clearly nonlinear, as with images, audio spectrograms and multivariate sensor windows.
Further reading. Hinton and Salakhutdinov (2006), "Reducing the Dimensionality of Data with Neural Networks", Science; Vincent et al. (2008), "Extracting and Composing Robust Features with Denoising Autoencoders", ICML; Rifai et al. (2011), contractive autoencoders; van den Oord, Vinyals and Kavukcuoglu (2017), "Neural Discrete Representation Learning" (VQ-VAE); He et al. (2021), "Masked Autoencoders Are Scalable Vision Learners", arXiv:2111.06377; Goodfellow, Bengio and Courville (2016), Deep Learning, chapter 14.