The seemingly stupid idea
Training a network to copy its inputs. Put like that, it seems completely useless. Why train a model to reproduce what you give it?
The trick is that it can't actually copy. You force a bottleneck in the middle of the network: all information must pass through a representation much smaller than the input. Imagine being asked to summarize a 500-page novel on a single index card. You can't copy it word for word: you're forced to capture the essentials, the themes, the key characters, the narrative arc. That's exactly what an autoencoder does.
The result? The autoencoder doesn't produce a perfect copy of the input. It produces a compact representation that preserves important structural information and eliminates noise and superfluous details. And it's precisely this compression constraint that makes the autoencoder useful: the compressed representation (called the latent code) captures the essence of the data.
Basic architecture

The encoder
The encoder is the first half of the network. It takes input x (say an image from MNIST, the standard benchmark set of 70,000 handwritten digits in black and white, at 28x28 pixels, so 784 numbers) and progressively compresses it into a much smaller vector z, say 32 numbers: 784 Γ· 32 β 24, so the input shrinks by a factor of roughly 24.
How to read that line: is the input image, seen as a list of numbers (one per pixel); is the compact code that comes out; is the function the encoder computes. Do not conflate the two kinds of object: holds the learned weights, frozen once training is over; is recomputed for every image you feed in. The weights are the compressor's know-how, is the result it produces on one particular image.
In practice, for images, the encoder uses convolutions (see convolutional networks) with a step of more than one pixel, which reduces the spatial resolution at each layer (e.g., 28x28 then 14x14 then 7x7), followed by dense layers that flatten and further compress. Each layer forces the network to keep only the most useful information for reconstruction.
The bottleneck
The vector z is the narrow neck of the architecture, the point where all information must pass through. With only 32 numbers to represent a 784-pixel image, the network is forced to make choices: which structures, which patterns, which contrasts are essential? This constraint is what forces the learning of meaningful representations. Without it, the network could simply memorize the identity function, which would be useless.
The decoder
The decoder does the inverse work: it takes the compressed code z and attempts to reconstruct the original input:
It uses dense layers to re-expand the dimensions, then transposed convolutions (upsampling) that progressively enlarge the spatial resolution back to the original size. The reconstructed image will never be perfectly identical to the original, but it should capture its essential features.
This is a subtle but important point: the autoencoder doesn't need human-annotated labels. The reconstruction target is the input itself. Each image is both the input and the expected answer. You just need a set of raw images, without any annotation. This is what makes autoencoders so valuable: they learn useful representations from unlabeled data, which is far more abundant and less costly to obtain.
The loss: faithful reconstruction
The loss function measures how closely the reconstruction resembles the original. The choice depends on the data type.
For images with pixels normalized between 0 and 1, you typically use mean squared error, abbreviated MSE:
Symbol by symbol: is the loss, the single number we try to make as small as possible; is the number of pixels in the image; numbers the pixels; is the value of pixel number in the original image; (read "x hat") is the value of the same pixel in the reconstruction, throughout this course, the hat marks a quantity estimated by the model rather than a true value; is the sum over all pixels. In one sentence: each reconstructed pixel is compared to the original pixel, the difference is squared (to penalize large errors more), and you take the average. For binary images (pixels that are 0 or 1), binary cross-entropy, abbreviated BCE, is preferred, as it suits values that read as probabilities (loss functions are covered separately).
The learning signal is clear: "compress into z, but well enough to be able to reconstruct afterward." The network seeks a balance between maximum compression and reconstruction fidelity.

Smart variants
Undercomplete autoencoder
This is the simplest and most intuitive variant: the dimension of z is strictly smaller than that of x. For example, 32 dimensions to represent a 784-pixel image. This size constraint is what prevents the network from simply copying the input and forces it to learn true compression. This is the "standard" case we described above.
Denoising autoencoder, abbreviated DAE
The idea is ingenious: you intentionally add noise to the input before giving it to the network, but the reconstruction target remains the clean input, without noise.
Symbol by symbol: is the clean image; (read "x tilde") is the same image once dirtied; (the Greek letter epsilon) is the noise added; the sign reads "is drawn at random from"; is the normal, or Gaussian, distribution centred on 0 with standard deviation (sigma), that is, unbiased noise whose strength sets; sums the squared differences, the same measure as the MSE above. In one sentence: dirty the image, run it through encoder then decoder, and compare the result to the clean image, never to the dirtied one.
You can use different types of noise: Gaussian noise (adding random values), masking by randomly switching pixels off (dropout), or "salt and pepper" noise (random pixels take the minimum or maximum value).
By learning to remove noise, the network is forced to capture the deep statistical structure of the data. It can no longer just blindly copy: it must understand what is signal and what is noise. The learned representations are therefore significantly more robust and generalizable than those of a standard autoencoder.
Sparse autoencoder
The idea: force the representation z to be sparse, most neurons must stay at zero, with only a few activating to represent each input.
Concretely, you add a sparsity penalty to the reconstruction loss:
(the Greek letter lambda) is a dial set by hand before training: it says how much you care about sparsity relative to reconstruction fidelity. A large gives a very sparse code but poor reconstructions; at zero brings you back to the ordinary autoencoder.
The sparsity penalty can take two forms:
- L1 penalty: you sum the absolute value of each activation (the "1" in L1 refers to that power of one, as opposed to the squares in MSE). The larger an activation, the more it costs. The network thus learns to activate only what's truly necessary.
- Kullback-Leibler divergence, almost always shortened to KL: a measure of how far apart two probability distributions are, covered in its own lesson. Here you compare each neuron's observed average firing rate to a target rate (e.g., "on average, each neuron should activate 5% of the time"). If a neuron activates too often, it's penalized.
The overcomplete-code paradox: unlike standard autoencoders where z is smaller than x, the sparse autoencoder can have a z larger than x. For example, a 784-pixel image with a 2000-dimensional latent space. How doesn't it just copy blindly? Because the sparsity penalty means a given image lights up only a few dozen of those 2000 neurons, the exact count depends on how is set. Each neuron becomes a specialized "pattern detector", somewhat like neurons in the visual cortex, which each respond to a specific type of edge or texture.
Contractive autoencoder
Adds a penalty on the sensitivity of the encoder to small input variations:
Symbol by symbol: is the derivative of the code with respect to the input, i.e., the table of all the answers to "if I nudge this pixel, how much does this coordinate of the code move?" (mathematicians call it the encoder's Jacobian); adds up the squares of every entry in that table (the Frobenius norm); is again the dial that doses the penalty.
In one sentence: if you slightly perturb x, z should barely move. This forces the latent space to be smooth and robust, two similar inputs yield two similar codes. Useful for learning representations insensitive to small transformations.
Practical applications
Dimensionality reduction
If you know PCA (Principal Component Analysis, a classical statistical method that finds the directions along which data varies most), you can think of the autoencoder as a nonlinear PCA. PCA can only capture linear relationships between variables. The autoencoder, thanks to its nonlinear activation functions, can capture much more complex and subtle structures in the data. For example, for face images, PCA could capture global brightness variations, while an autoencoder can learn concepts like "smile," "face orientation," or "wearing glasses."
Anomaly detection
You train the autoencoder only on normal data. It learns to faithfully reconstruct what it has seen. When faced with abnormal data (fraud, a defective part), it doesn't know how to reconstruct it, the reconstruction error spikes. You set a threshold: above it, it's an anomaly.
The key advantage: you don't need examples of anomalies for training. Anomalies are often rare and unpredictable by definition, the autoencoder detects "what it has never seen."
The pipeline is simple: you train the autoencoder on a normal dataset, then measure the reconstruction error on a validation set to set a threshold. In production, each new data point passes through the autoencoder, if the reconstruction error exceeds the threshold, it's flagged as an anomaly. This is used in banking fraud detection, industrial predictive maintenance, and visual quality control.

Pre-training (historically)
Before the deep learning revolution around 2012, deep networks were very difficult to train directly. A popular technique was to pre-train each layer individually as an autoencoder, then stack the pre-trained layers. This smart weight initialization allowed launching supervised learning from a better starting point. With the arrival of powerful GPUs (Graphics Processing Units), large datasets, and better initialization techniques (like Xavier or He), this approach was abandoned. But historically, it was crucial for proving that deep networks could work.
Image compression
Convolutional autoencoders learn an encoding specific to the type of images they're trained on. On a narrow domain (medical images, satellite imagery, faces) that specialization is an edge over general-purpose formats like JPEG or WebP, which know nothing about the expected content. This is the principle behind neural codecs. We quote no gain figure here: published comparisons depend entirely on the image set and the quality measure chosen, and do not carry over from one case to another.
Image denoising
Denoising autoencoders (DAEs) are directly used to clean noisy images. Unlike classical filters like Gaussian blur that act blindly on all pixels, the DAE has learned the real statistical structure of clean images. It knows how to distinguish what is signal (real edges, textures) from what is noise. Result: it preserves important details while eliminating noise, which classical filters cannot do.
Limitations of the classic autoencoder
- Disorganized latent space: two nearby points in z don't necessarily correspond to similar data, making interpolation unpredictable
- No generation possible: you can't sample a random z and expect a realistic output, because the latent space isn't continuous
- Not probabilistic: the model is purely deterministic, it doesn't learn a probability distribution over the data
These three limitations directly motivate the invention of the Variational Autoencoder, abbreviated VAE, which adds a probabilistic structure to the latent space. The VAE bridges classical autoencoders and modern generative models, solving each of these three limitations: see its own lesson.
The "classic" autoencoder described here is mainly of teaching and historical value: it is the simplest setup that shows why constraining a network teaches it something. Two of its descendants, however, are everywhere today. First the variational autoencoder, which acts as the image compressor at the input and output of diffusion models: it is what lets an image generator work on a shrunken version rather than on raw pixels. Second embeddings, the compact vector representations of text or images, which reuse the bottleneck idea but are trained differently (see text embeddings). Layer-by-layer pre-training, by contrast, really has died out.
Summary
The autoencoder relies on a simple but powerful principle: forcing a network to compress its data through a bottleneck, then reconstruct it. This constraint forces the network to learn compact and meaningful data representations. The variants (denoising, sparse, contractive) each add an additional constraint that improves the quality of learned representations. Applications are numerous: dimensionality reduction, anomaly detection, image compression, denoising. However, the classic autoencoder has a fundamental limitation: its latent space is not structured in a way that allows generating new data. That's exactly the problem solved by the variational autoencoder (VAE), which adds a probabilistic structure to the latent space, and which, in that form, remains a live component of today's image generators. Understanding autoencoders is essential before tackling modern generative models.
1. Training a network to copy its own input sounds pointless. What makes the exercise instructive anyway?
2. Once training is over, what happens when you feed a new image to the autoencoder?
3. In a denoising autoencoder, you deliberately corrupt the input image. What is the network's output compared against when computing the loss?
4. A sparse autoencoder can have a 2000-dimensional latent space for a 784-pixel image. Why doesn't it just copy the input?
5. In 2026, which descendant of the autoencoder still sits at the heart of image generators?