An autoencoder is a network trained to reproduce its input through a narrow middle layer, the code. Because the code is smaller than the input (undercomplete), the network must learn a compressed representation, with no labels needed. A linear autoencoder trained on squared error learns exactly the PCA subspace (Baldi and Hornik, 1989). Nonlinear autoencoders can follow curved data, and Hinton and Salakhutdinov (2006) showed that deep ones compress far better than PCA. This page covers encoders, decoders and codes, the link to PCA, and what limits what an autoencoder learns. In the exercises you build the optimal linear autoencoder from PCA, then compare it with a trained nonlinear autoencoder on points along a curved arc, both using a 1-D code. The linear autoencoder matched PCA's error of 0.32, while the nonlinear one reached 0.0009, the noise level.
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.
Squeeze, then rebuild
An autoencoder has two halves. The encoder squeezes each input into a short code; the decoder tries to rebuild the input from that code. Training simply asks the output to match the input. Because the code is too small to copy everything, the network has to keep what matters most about the data and drop the rest. The demo compares a linear code, which can only flatten data onto a line, with a curved code that follows the data.
Autoencoder lab
1-D code. Linear autoencoder (= PCA, red line): error 0.296. Nonlinear code (the angle, green): error 0.003. A linear bottleneck can only flatten curved data onto a subspace.
Why engineers care
Autoencoders compress sensor data and learn features without labels, and their reconstruction error flags unusual inputs: a standard tool for anomaly detection in telemetry and machine monitoring.
📝 Quick check (Level 0)
0/3 answered
1. An autoencoder is trained to output…
2. Undercomplete means the code is…
3. Autoencoders need labels?
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.
By hand
Model: encoder z = f(x) with code dimension k smaller than the input dimension D; decoder x̂ = g(z).
Loss: reconstruction error, usually mean squared error ‖x − x̂‖², or cross-entropy for binary pixels.
Compression ratio D/k: 784/32 = 24.5 for MNIST digits with a 32-number code.
Linear autoencoder:
with linear f and g and squared error, the best possible solution spans the top-k principal components;
its error equals the sum of the discarded eigenvalues of the data covariance;
for eigenvalues 9, 4, 1, 0.25 and k = 2, the error is 1 + 0.25 = 1.25.
Size: a [784, 32, 784] network has 784·32 + 32 + 32·784 + 784 = 50,992 parameters.
📝 Quick check (Level 1)
0/3 answered
1. A linear autoencoder with squared error learns…
2. Its error for eigenvalues 9, 4, 1, 0.25 and k = 1:
3. Compression ratio for 784 inputs and a 32-number code:
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.
Autoencoders in code
Exercise 1.
The optimal linear autoencoder was built from the top principal directions.
Its reconstruction error equalled the sum of the discarded eigenvalues, to 10⁻⁹.
With k equal to the input dimension, reconstruction was exact.
Practice.
In PyTorch, write the encoder and decoder as nn.Sequential stacks and train on F.mse_loss(model(x), x).
Keep the decoder roughly mirroring the encoder. Normalise the inputs.
For anomaly detection, train on normal data only and set a threshold on reconstruction error from a validation set.
enc = nn.Sequential(nn.Linear(784, 128), nn.ReLU(), nn.Linear(128, 32))dec = nn.Sequential(nn.Linear(32, 128), nn.ReLU(), nn.Linear(128, 784))loss = F.mse_loss(dec(enc(x)), x) # no labels: the target is the input
🐍 Exercise 1: the optimal linear autoencoder
Build the linear autoencoder from PCA and check its error against the discarded eigenvalues.
📝 Quick check (Level 2)
0/3 answered
1. For anomaly detection, train on…
2. The training target of an autoencoder is…
3. Binary pixel inputs often use…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.
Theory
Linear autoencoders are PCA.
With linear maps and squared error, every local minimum is a global minimum, and the decoder’s columns span the top-k principal subspace (Baldi and Hornik, 1989).
The code itself need not be the principal components: any invertible mixing of them works equally well.
What nonlinearity adds.
A nonlinear encoder and decoder can learn a curved low-dimensional surface (a manifold) through the data, not just a flat subspace.
Data lying near a curve can then be coded with one number instead of many.
Why the bottleneck matters. With a code as large as the input, an autoencoder can learn the identity and nothing useful. Undercompleteness is one way to force useful structure. Sparse, denoising and contractive autoencoders (next pages) are others that work even with large codes.
📝 Quick check (Level 3)
0/3 answered
1. A linear autoencoder's code equals the principal components…
2. Nonlinear autoencoders can learn…
3. With a code as large as the input, the risk is…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.
Does a nonlinear autoencoder beat PCA on curved data?
History.
Autoencoders trace back to the 1980s (Rumelhart, Hinton and Williams, 1986; Baldi and Hornik, 1989).
G. E. Hinton and R. R. Salakhutdinov, “Reducing the dimensionality of data with neural networks” (Science 2006), trained deep autoencoders that compressed far better than PCA.
Exercise 2.
Data: 400 points on a 270° arc of the unit circle with noise of standard deviation 0.03. The data is intrinsically one-dimensional but curved in 2-D.
Models, both with a 1-D code:
a linear autoencoder [2, 1, 2];
a nonlinear autoencoder [2, 32, 1, 32, 2] with tanh hidden layers.
Training: Adam for 3000 full-batch steps, compared with PCA.
method
mean squared reconstruction error
PCA, 1 component
0.3198
linear autoencoder
0.3198
nonlinear autoencoder
0.0009
noise across the arc (0.03²)
0.0009
The linear autoencoder found exactly PCA’s solution, as the theory says: it can only project the arc onto a line.
The nonlinear autoencoder reconstructs the arc about 350 times better, down to the noise across the arc, which no 1-D code can recover. The noise along the arc is absorbed into the code.
It generalises: on 400 new arc points its error was 0.0011.
Seeds: with seeds 1–3 the nonlinear errors were 0.0010, 0.0030 and 0.0009 (PCA 0.29–0.30). One seed found a slightly worse solution.
🐍 Exercise 2 (research): does a nonlinear autoencoder beat PCA on curved data?
Train linear and nonlinear autoencoders with a 1-D code on an arc and compare them with PCA.
Autoencoders in AI, robotics and aerospace
Anomaly detection: spacecraft telemetry, engine vibration and industrial sensors, where a high reconstruction error flags faults without labelled examples of them.
Compression and features: compact codes of images or states for downstream learning, such as world models in robotics.
Design lesson: check a linear baseline (PCA) first. If it already reaches the noise level, a nonlinear autoencoder adds only complexity.
References
P. Baldi and K. Hornik (1989). “Neural networks and principal component analysis: learning from examples without local minima.” Neural Networks 2.
G. E. Hinton and R. R. Salakhutdinov (2006). “Reducing the dimensionality of data with neural networks.” Science 313.
I. Goodfellow, Y. Bengio and A. Courville (2016). Deep Learning, chapter 14. MIT Press.
On a noisy arc with a 1-D code, the linear autoencoder matched PCA (0.32), while a nonlinear one reached the noise level (0.0009).
Practice
Show:
Level 0
Problem 1Level 0
Compression ratio for 784 inputs and a 32-number code?
24.5.
Problem 2Level 0
Year of Hinton and Salakhutdinov’s Science paper on deep autoencoders?
2006.
Problem 3Level 0
Code dimension used in exercise 2?
1.
Level 1
Problem 4Level 1
Linear autoencoder error for eigenvalues 9, 4, 1, 0.25 with k = 2?
1.25.
Problem 5Level 1
Parameters of a [784, 32, 784] autoencoder?
50992.
Problem 6Level 1
Fraction of variance explained by one component for eigenvalues 9, 4, 1, 0.25 (4 decimals)?
9/14.25 = 0.6316.
Problem 7Level 1
Parameters of a [2, 1, 2] linear autoencoder?
2 + 1 + 2 + 2 = 7.
Level 2
Problem 8Level 2
PCA error with 1 component in exercise 2?
0.3198.
Problem 9Level 2
Nonlinear autoencoder error in exercise 2?
0.0009.
Problem 10Level 2
Ratio of PCA to nonlinear error (0.3198/0.0009, nearest whole number)?
355.
Problem 11Level 2
Linear autoencoder error for eigenvalues 9, 4, 1, 0.25 with k = 1?
5.25.
Problem 12Level 2
Nonlinear autoencoder error on new arc points?
0.0011.
Problem 13Level 2
Mean squared error between (1, 2, 3) and (1, 2, 5) (4 decimals)?
4/3 = 1.3333.
Level 3
Problem 14Level 3
Prove that the best linear rank-k reconstruction error is the sum of the discarded eigenvalues.
A linear autoencoder’s reconstruction x̂ = g(f(x)) has rank at most k, so the best one is the best rank-k approximation of the centred data matrix. By the Eckart–Young theorem this is the truncated SVD, with error equal to the sum of the squared discarded singular values. Divided by n, these are the discarded covariance eigenvalues. ∎
Problem 15Level 3
Explain why the nonlinear autoencoder’s error stopped at 0.0009.
The noise has two parts: along the arc and across it. A 1-D code can store a point’s position along the arc, including the noise in that direction. It cannot also store the offset across the arc, so the decoder outputs a point on the curve, leaving the radial noise variance 0.03² = 0.0009 as error. ∎
Problem 16Level 3
Compare autoencoders with PCA.
PCA is a linear autoencoder solved in closed form: fast, unique up to rotation, interpretable, but limited to flat subspaces. Nonlinear autoencoders can follow curved structure and scale to images, but need training, can get stuck in worse solutions, and their codes are harder to interpret. ∎
Problem 17Level 3
Advise on detecting faults in satellite telemetry with an autoencoder.
Train on telemetry from normal operation, normalised per channel, with a code small enough to prevent copying. Set the alarm threshold on reconstruction error from held-out normal data (for example a high percentile), and check it against any known faults. Compare with PCA first. Watch for slow drift (retrain periodically) and attribute alarms to the channels with the largest errors. ∎
Level 4
Problem 18Level 4
Nonlinear autoencoder error with seed 2?
0.0030.
Problem 19Level 4
Year of Baldi and Hornik’s linear autoencoder result?
1989.
Problem 20Level 4
Hidden width of the nonlinear autoencoder’s tanh layers in exercise 2?
32.
Coding practice
🐍 Code 1 (Level 0): compression ratio
D/k.
🐍 Code 2 (Level 0): parameters
Weights plus biases.
🐍 Code 3 (Level 0): PCA error
Discarded eigenvalues.
🐍 Code 4 (Level 0): MSE
Mean squared error.
🐍 Code 5 (Level 0): explained variance
Top-k share.
Topic test
A timed test drawn at random from this topic's question bank. Each attempt gets new questions. For a longer test across the whole chapter, see the chapter test, or mix topics in the question bag.
Vanilla / undercomplete autoencoder: topic test
10 questions, drawn at random from a bank of 21. Every attempt is different.
⏱ Time limit: 15 minutes. The test submits itself when time runs out.
Levels covered: 0, 1, 2, 3, 4. No hints or solutions until you submit.