The idea: squeeze, then rebuild
An autoencoder is a neural network trained to output its own input. That sounds useless, until you make the middle of the network narrow. The first half, the encoder, must squeeze the input into a few numbers, the code z. The second half, the decoder, must rebuild the input from those numbers alone. The narrow middle is the bottleneck. To pass through it, the network has to learn what the inputs have in common and keep only what makes one input different from another.
On the canvas the data is 24 tiny images of 5 × 7 = 35 pixels: 6 copies each of the digits 0, 1, 4 and 7. The first copy is the clean digit; in the other five one ink pixel has moved. The network is 35 → 12 → 2 → 12 → 35, with tanh in the hidden layers and a sigmoid on the output so every pixel is between 0 (paper) and 1 (ink). It is trained for real in your browser with backpropagation and the Adam optimizer, from fixed random seeds, so every run gives the same numbers.
Reconstruction error
The network is scored by the mean squared error (MSE) between output x̂ and input x, averaged over the 35 pixels:
MSE(x, x̂) = (1/35) · Σ (x̂ᵢ − xᵢ)² // 0 = perfect copy
Untrained, every output pixel is about 0.5, so each pixel is off by about 0.5 and the MSE is about 0.25: 0.253 for the digit 4 (Demo 1: one image through the untrained network). One epoch shows the network all 24 images once, in 4 shuffled mini-batches of 6, with one Adam step (learning rate 0.01) per batch. The chart plots the MSE over all 24 images after every epoch: 0.252 at the start, 0.047 after 50 epochs, 0.013 after 300 (Demo 2: train 300 epochs). The trained network rebuilds the 4 with MSE 0.006; its worst pixel is off by 0.30 (Demo 3).
No labels are used anywhere. The target is the input itself, so this is self-supervised learning: any pile of unlabelled images is training data.
Why the bottleneck matters
If the code were as wide as the input, the network could learn to copy pixel by pixel and would learn nothing about digits. With 2 numbers for 35 pixels, copying is impossible. The cheapest way to rebuild 24 images from 2 numbers each is to learn the four digit shapes, then use the code to say which shape and how it varies. Wider codes rebuild better but summarise less; real autoencoders pick the width as a trade-off, or keep a wide code and add another constraint (noise, sparsity) instead.
The code space: digits cluster
Each image's code is a point in a 2D plane, the latent space. The left plot under the pipeline shows the codes of all 24 images, coloured by digit. At epoch 0 they sit in one heap near the centre. As training goes on they spread apart into four clusters, one per digit: after 300 epochs every image's code is closer to the centre of its own digit's cluster than to any other centre. The network was never told which image is which digit; the clusters appear because images of the same digit need similar codes to be rebuilt well.
The decoder works on any code, not only the codes of training images. Decoding codes on the line from the centre of the 1s to the centre of the 7s gives a 1, then blends of both strokes, then a 7 (Demo 4: decode codes between 1 and 7). Many codes between and around the clusters decode to nothing like a digit, so a plain autoencoder is a poor generator. Making the code space smooth enough to sample from is what the variational autoencoder adds; see VAE vs GAN.
The denoising autoencoder
A denoising autoencoder (DAE) has the same shape but a different task: its input is a corrupted copy x̃ and its target is the clean x. Here each pixel is flipped with probability 0.15, with fresh noise every epoch. Both networks start from the same seeded weights and see the same images in the same order; only the input differs.
The test set is one fixed noisy copy of every image (13.7% of its pixels flipped on average). Fed these noisy copies, the plain autoencoder rebuilds the clean images with MSE 0.072; the denoising autoencoder gets 0.038. On the noisy 4, which has 4 flipped pixels, the plain autoencoder gives MSE 0.063 and the denoising one 0.010 (Demo 5: noisy 4, plain vs denoising AE). The plain network never saw noise, so the stray pixels push its code off course. The denoising network learned to ignore them. The price: on clean images its MSE is 0.038, worse than the plain autoencoder's 0.013, because it was never asked to keep the small differences between the 6 copies of a digit.
Autoencoder vs PCA
PCA also turns each image into 2 numbers: the projections onto the two directions of largest variance, PC1 and PC2, and rebuilds the image as mean + z₁·PC1 + z₂·PC2 (see SVD and PCA). It is a linear encoder and decoder. On these 24 images the two components keep 60% of the pixel variance, and the rebuild has MSE 0.078. The autoencoder, with the same 2 numbers, passes PCA's error at epoch 33 and ends 6× lower at 0.013 (Demo 6: autoencoder vs PCA). In the PCA plot the 1s and 7s sit close together; the autoencoder pulls all four digits apart.
| PCA, 2 components | autoencoder, 2-number code | |
|---|---|---|
| encoder | 2 dot products with fixed patterns | a neural network (35 → 12 → 2) |
| decoder | mean + weighted sum of 2 patterns | a neural network (2 → 12 → 35) |
| found by | an exact formula (eigenvectors / SVD) | gradient descent, from a random start |
| MSE on the 24 images | 0.078 | 0.013 after 300 epochs |
| same answer every time | yes | only with the same seed |
The two are closely related: an autoencoder with no activation functions (all linear) trained on MSE learns the same 2D subspace as PCA. The nonlinear activations are what let it bend the subspace around the data. For more on PCA as a picture of the data, see PCA vs t-SNE; for the network itself, neural networks and backpropagation.
Where autoencoders are used
- Anomaly detection: train on normal data only; an input the network rebuilds badly (high MSE) is unusual.
- Denoising images, audio or sensor readings, as in Demo 5.
- Features and compression: the code is a short summary to feed another model or to search by.
- Latent spaces for generators: image generators such as Stable Diffusion run in the code space of an autoencoder rather than on raw pixels.
See also U-Net Segmentation, a down-and-up network whose skip connections carry detail around the bottleneck.
What the page leaves out
Real autoencoders work on images of thousands of pixels with convolutional encoders and decoders, codes of tens to hundreds of numbers, and millions of training images; here everything is 35 pixels, 24 images and full knowledge of the seeds. The page leaves out sparse and contractive autoencoders, masked autoencoders that hide image patches, vector-quantized codes (VQ-VAE), the variational autoencoder's probabilistic code (covered in VAE vs GAN), binary cross-entropy as the loss, and checking for overfitting on a held-out test set.
On the mind map
References
Goodfellow, Bengio, Courville: Deep Learning, Chapter 14, Autoencoders
Hinton, Salakhutdinov: Reducing the Dimensionality of Data with Neural Networks (Science, 2006)
Vincent et al.: Extracting and Composing Robust Features with Denoising Autoencoders (ICML 2008)
Baldi, Hornik: Neural Networks and Principal Component Analysis (Neural Networks, 1989)