Autoencoders: The Idea
After this lesson, you will be able to:
- Explain how squeezing a picture through a narrow layer forces a network to keep only what matters
- Name the three parts of an autoencoder (encoder, bottleneck, decoder) and read its reconstruction score
- Explain why a denoising autoencoder cannot just copy its input, and how a linear autoencoder relates to PCA
- Describe the latent space as a map where similar pictures sit close together
- Say why a plain autoencoder cannot make new pictures: its latent space has holes
Before you start
#Compress and Rebuild
You will meet the three parts of an autoencoder, see why a narrow middle layer is the whole point, learn the denoising version, and see why a plain autoencoder still cannot invent new pictures.
A network trained to rebuild its own input after squeezing it through a narrow middle layer.
The half that squeezes the input into a short list of numbers.
The half that rebuilds the input from that short list.
The narrow middle layer. It is too small to copy the input, so the network must keep only what matters.
The short list of numbers inside the bottleneck that stands for one picture. The place where all such lists live is the latent space.
The picture the decoder rebuilds. The reconstruction error is how far it is from the original.
An autoencoder that gets a damaged input and is trained to rebuild the clean original.
#The Bottleneck That Creates Understanding
Force a network to compress its data, and it works out for itself what matters. It needs no labels. The picture itself is the answer key, and the only rule is that the output must match the input after passing through a narrow layer. Learning from the data alone like this is called self-supervised learning.
Try it! Describe a photo of a friend using only 5 words. Now use 10 words. Now 50. More words let you keep more detail. An autoencoder faces the same trade: a wider bottleneck keeps more information but compresses less. The best choice is the smallest bottleneck that still rebuilds the picture well.
#Architecture: Encoder, Bottleneck, Decoder
Picture first. Take a 28 by 28 digit image: 784 numbers. The encoder turns those 784 numbers into 16. The decoder turns the 16 back into 784. The training score is how far the rebuilt picture is from the original. If one pixel was 0.8 and comes back as 0.6, its error is 0.2, and the loss uses the square, 0.04. Add this up over every pixel and every picture.
The loss is the reconstruction error, the squared gap between the input and the rebuilt picture:
If we made the bottleneck as wide as the input (same number of dimensions), what would the autoencoder learn?
#Denoising autoencoders: rebuild the clean picture from a damaged one
- Do: Drag the Bottleneck Size slider from wide to narrow, and drag a point in the latent plane.
- Notice: A wide bottleneck gives a near-perfect copy. A narrow one blurs details, and dragged points decode to digit-like shapes.
- Why: Fewer numbers means less room, so the network keeps only the strongest features. Drawn stand-in, not a trained model.
#Step 1: Input Image
A full-resolution image enters the network — for example, a 28x28 MNIST digit with 784 pixel values. Every pixel is a dimension. The autoencoder must learn to squeeze all 784 values through a much narrower bottleneck.
#Step 2: Encoder Compresses
The encoder — a stack of dense or convolutional layers — progressively reduces dimensionality: 784 to 256 to 64 to 16. Each layer discards redundancy and keeps the features most useful for reconstruction. The network decides on its own what matters.
#Step 3: Bottleneck (Latent Vector z)
At the narrowest point, the data is represented as a compact latent vector z of just 16 dimensions. This is the "compressed description" of the input. For a vanilla autoencoder, z is a single deterministic point.
#Step 4: For VAE — Sample z from N(mu, sigma squared)
In a Variational Autoencoder, the encoder outputs a mean mu and variance sigma squared instead of a single point. The latent code z is sampled from this Gaussian distribution using the reparameterization trick: z = mu + sigma * epsilon. This injects controlled randomness that makes the latent space smooth and generative.
#Step 5: Decoder Reconstructs
The decoder mirrors the encoder in reverse: 16 to 64 to 256 to 784. It takes the latent vector z and expands it back to the original input dimensions, attempting to faithfully reconstruct every pixel of the input image.
#Step 6: Compare with Original (Reconstruction Loss)
The output x-hat is compared pixel-by-pixel with the original input x. The reconstruction loss (MSE or binary cross-entropy) measures how faithfully the decoder reproduced the input. For VAEs, a KL divergence term is added to keep the latent distributions close to a standard Gaussian.
#Step 7: Output
After training, the decoder has learned to produce crisp reconstructions from compact latent codes. For generation, you can skip the encoder entirely — sample z from N(0, I) and pass it through the decoder to create entirely new images that look like the training data.
- Do: Scroll through the seven steps and watch which part of the picture lights up.
- Notice: The data gets narrower on the way in, and wider again on the way out.
- Why: The narrow middle is where the network is forced to choose what to keep. Step 4 previews the next lesson. Drawn stand-in, not a trained model.
#The Latent Space: A Map of Possibilities
Once trained, the encoder maps every training image to a point in the latent space. A point is just the short list of numbers for one image. Similar images land close together, so the latent space becomes a compressed map of the data.
For MNIST digits:
- All "3"s cluster in one region
- All "7"s cluster in another region
- The border between 3 and 7 holds the digits that look like both
- Do: Click two points, then move the slider from one to the other.
- Notice: Points of the same class sit together, and the in-between point changes its label where it crosses a border.
- Why: Similar pictures get nearby codes, so the latent space works as a map. Drawn stand-in, not a trained model: the points are placed by hand.
#The Problem: Autoencoders Are Not Generative
A plain autoencoder can compress and decompress, but it cannot make new pictures. To make one, you would pick a random point in the latent space and decode it. The decoder often returns garbage, because the encoder never sent any real picture to that region.
The latent space has holes: regions that mean nothing. The decoder only learned the specific points the encoder produced, not the gaps between them.
#Key Takeaways
- The bottleneck forces useful representations. By squeezing data through a narrow layer, an autoencoder must keep the most important features and drop noise and repetition
- A linear autoencoder with squared error learns the same subspace as PCA. Nonlinear layers let it follow curved data
- A denoising autoencoder gets a damaged input and is scored against the clean original, so it cannot just copy
- The latent space is a map: similar pictures get nearby codes
- A plain autoencoder has holes in its latent space, so it cannot make new pictures. Fixing that is the job of the variational autoencoder
#Quick Check
What is the purpose of the bottleneck in an autoencoder?