Autoencoders
An autoencoder is a neural network trained to reproduce its own input after squeezing it through a narrow bottleneck, and the only way it can succeed is by learning a compressed representation that captures what actually matters in the data.
Prerequisites: The Multilayer Perceptron, Dimensionality Reduction in Finance
Give a model a task where the "answer key" is the input itself — reproduce this exact vector of numbers you were just shown — and it sounds trivial; the identity function does it perfectly. But force the model to pass that input through a narrow bottleneck first, a layer with far fewer numbers than the original, and the trivial solution is no longer available. It can't just copy; it has to compress, and the only way to compress well is to figure out what's actually important in the data and discard the rest.
That's an autoencoder: a packing exercise. Imagine trying to describe a full outfit — shirt, pants, shoes, jacket, accessories — using only three words, then handing those three words to someone else who has to reconstruct the outfit from scratch. You're forced to identify the three facts that matter most (maybe "formal," "navy," "winter") and let everything else be inferred or lost. A good compression scheme emerges only because the bottleneck makes sloppy compression impossible.
The mechanics, one symbol at a time
An autoencoder has two halves. The encoder maps the input down to a low-dimensional latent code ; the decoder maps back up to a reconstruction :
In words: encode, then decode, then measure how far the reconstruction landed from the original using squared error (add up the squared difference in every dimension). Training just means adjusting the weights of and together to make that loss as small as possible — there are no labels anywhere in this process, only the input compared against its own reconstruction, which is why autoencoders are called unsupervised. The dimension of (the bottleneck's width) is a hyperparameter you choose: make it too wide and the network can cheat toward the identity function without learning anything useful; make it appropriately narrow and it's forced to find real structure.
Worked example 1: compressing two correlated features to one
Two input features that happen to be almost perfectly correlated in the training data: momentum and 3-month trailing return , which in practice move together (correlation near 0.95) because they measure overlapping things. Suppose a trained linear encoder learns (roughly capturing "average trend strength") and the decoder learns , .
For an input : .
Reconstruction: , .
Loss: — small, because one number, , was almost enough to reconstruct both original numbers. The bottleneck forced the network to notice the two features carry mostly the same information and collapse them into a shared summary — the same thing principal component analysis does, but learned by gradient descent rather than by an eigenvalue calculation, and capable of nonlinear compression if the encoder and decoder have hidden layers with nonlinear activations.
Worked example 2: reconstruction error flags an unusual input
Train the same autoencoder only on "normal" days, where momentum and trailing return move together. Now feed it an unusual day where the two features have decoupled: — momentum strongly positive, trailing return strongly negative, a combination the network never saw enough of during training.
Using the same encoder: .
Reconstruction: , .
Loss: — over 250 times larger than the normal-day loss of 0.023. The single learned direction simply cannot represent two features moving in opposite directions, because it never had to during training. A large reconstruction error is a direct, quantitative signal that this input doesn't fit the pattern the network learned — which is exactly the mechanism behind using autoencoders for anomaly and regime-break detection.
Squared reconstruction error grows the way the curve above does: small deviations from what the network has learned to reconstruct well cost very little, but the penalty accelerates fast as an input becomes more unusual, which is what makes reconstruction error such a sensitive anomaly signal.
What this means in practice
Autoencoders are used for dimensionality reduction ahead of another model, denoising (train to reconstruct a clean version from a noisy input, and the bottleneck forces it to learn the underlying signal rather than the noise), and anomaly detection, exactly as in worked example 2 — a spike in reconstruction error on live data flags a regime that doesn't resemble training history. Unlike PCA, an autoencoder's encoder and decoder can be nonlinear, so it can capture curved, more complex structure that a straight-line projection misses, at the cost of needing more data and careful tuning of the bottleneck width to avoid either under-compressing (bottleneck too wide, nothing useful is learned) or over-compressing (bottleneck too narrow, even normal reconstructions are poor).
An autoencoder learns to compress and then reconstruct its own input; because a narrow bottleneck makes exact copying impossible, the compressed code it settles on is forced to capture the real structure of the data, with no labels required.
The classic confusion: assuming low reconstruction error always means "normal" and high error always means "anomalous," without checking what the network actually saw in training. An autoencoder trained on a narrow historical window will flag a perfectly legitimate new regime as anomalous simply because it never saw anything like it — reconstruction error measures similarity to training data, not similarity to truth.
Related concepts
Practice in interviews
Further reading
- Goodfellow, Bengio & Courville, Deep Learning, ch. 14
- Hinton & Salakhutdinov, Reducing the Dimensionality of Data with Neural Networks