/knowledge/notes/autoencoders-vs-pca
Concept note · PCA & Dimensionality Reduction
Autoencoders vs PCA
Both squeeze each row into fewer numbers and rebuild it. A linear autoencoder lands on PCA's answer exactly, and the non-linearity is what lets it follow a curve.
- Studied
- Multivariate Statistics for Data ScienceMAST90138 · MAST30034
- When
- UniMelb, 2021–2024
- Applied in
- Multivariate Lab · Source Separation Lab
- Read / Refreshed
- ~5 min read2026-10-21
PCA and an autoencoder answer the same request: squeeze each row into fewer numbers, then rebuild the row from those numbers as well as you can. PCA does the squeeze with a straight line or a flat plane, found in closed form. An autoencoder learns the squeeze and the rebuild as two small neural networks. The useful part is knowing where the two agree exactly, and where they part ways.
01
The idea
Both methods have the same shape. An encoder turns a row with numbers into a code with numbers, where is smaller than . A decoder turns the code back into a row . Training means making as close to as the code allows. The narrow middle, the bottleneck, forces the method to keep what matters most and let the rest go.
In PCA, both halves are linear. The encoder projects onto directions and the decoder adds those directions back up. The best directions, in squared error, are the top eigenvectors of the covariance matrix, so there is nothing to train. You get the same answer every time, apart from the sign of each direction.
In an autoencoder, both halves are neural networks. With non-linear activations in the hidden layers, the code can trace a curve or a curved surface, so a single number can describe a point on a bent shape that no straight line fits.
The link worth remembering runs the other way. Take the activations out, so every layer is linear, and train on squared error: the autoencoder ends up spanning exactly the subspace PCA finds. Baldi and Hornik proved in 1989 that, with one hidden layer, this loss has no local minima other than the PCA solution. Stacking more linear layers still computes a single linear map, so it cannot do better. The non-linearity is the whole point.
02
The maths
Centre the data on its mean and form the covariance matrix . Its eigenvectors are the principal directions, and each eigenvalue is the variance along its direction.
Dividing by n rather than n − 1 keeps the error identity below exact. It does not change the directions.
Put the top eigenvectors in the columns of . PCA's reconstruction projects onto them, and its average squared error is the variance it threw away.
The Eckart–Young theorem says no rank-k linear map does better in squared error.
An autoencoder replaces the projection with two learned functions, an encoder and a decoder , and fits them by gradient descent on the same loss.
If f and g are linear, the best g∘f is PCA's projection. If they bend, the reconstruction can follow a curved surface.
The price of bending is everything PCA never asks for: a choice of layer sizes and activations, a learning rate, a random starting point, a stopping rule, and a fitted shape that can wrap itself around noise. PCA has one decision, , and the share of variance kept, , helps you make it.
03
Try it
The widget squeezes 80 points into one number each and rebuilds them. The dashed line is PCA's answer. The red curve is a small autoencoder, 2 to 6 to 1 to 6 to 2 units, trained right here in your browser with 1,200 steps of Adam. Both are scored on the training points and on 80 fresh points from the same curve.
Autoencoder layers
Show errors for
- Training points
- PCA, one component
- Autoencoder, one-number code
The first principal component keeps 78% of the variance.
| Method | Training | Fresh |
|---|---|---|
| PCA | 0.0921 | 0.0911 |
| Autoencoder (tanh) | 0.0024 | 0.0033 |
On fresh points PCA's error is 0.0911 and the tanh autoencoder's is 0.0033. The autoencoder follows the bend that PCA's line cannot.
- Switch the layers to Linear. The red curve straightens onto the dashed line and both error columns match PCA to four decimal places, as Baldi and Hornik predict.
- Back on tanh at curvature 1, the curve follows the bend and its error on fresh points is more than twenty times smaller than PCA's.
- Drag curvature to 0. Now the data really is a line, PCA keeps nearly all the variance, and the autoencoder has nothing to add.
- Keep curvature at 0, set noise to 0.20 and press New data twice to reach seed 3. The tanh autoencoder beats PCA on the training points and loses to it on fresh ones. It has bent itself around noise.
- Show errors for PCA on a curved setting. The error lines are longest at the two ends of the bend, where a straight line strays furthest from the data.
04
Where I used it
05
Easy to get wrong
06
Sources
- Neural networks and principal component analysis: Learning from examples without local minimaBaldi and Hornik, Neural Networks 2(1), 1989, pp. 53–58.The proof behind the first bullet in Try it. A linear autoencoder on squared error has PCA's subspace as its only minimum.
- Nonlinear principal component analysis using autoassociative neural networksKramer, AIChE Journal 37(2), 1991, pp. 233–243.The bottleneck design the widget uses: a mapping layer, a narrow code and a de-mapping layer, presented as non-linear PCA.
- Reducing the dimensionality of data with neural networksHinton and Salakhutdinov, Science 313(5786), 2006, pp. 504–507.The paper that made deep autoencoders practical, with side-by-side pictures of PCA and autoencoder codes.
- Deep Learning, Chapter 14: AutoencodersGoodfellow, Bengio and Courville, MIT Press, 2016.A free textbook chapter covering undercomplete, regularised and denoising autoencoders, with the link to manifold learning.
- Principal component analysis: a review and recent developmentsJolliffe and Cadima, Philosophical Transactions of the Royal Society A 374, 2016.A clear modern review of PCA itself, including scaling choices and how many components to keep.