Foundations to Frontier
Tests
E. Deep Learning › E13 Autoencoders

Vanilla / undercomplete autoencoder

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.

Level 0: Foundation

★☆☆☆☆ 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. 1. An autoencoder is trained to output…
  2. 2. Undercomplete means the code is…
  3. 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. 1. A linear autoencoder with squared error learns…
  2. 2. Its error for eigenvalues 9, 4, 1, 0.25 and k = 1:
  3. 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. 1. For anomaly detection, train on…
  2. 2. The training target of an autoencoder is…
  3. 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. 1. A linear autoencoder's code equals the principal components…
  2. 2. Nonlinear autoencoders can learn…
  3. 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

  1. P. Baldi and K. Hornik (1989). “Neural networks and principal component analysis: learning from examples without local minima.” Neural Networks 2.
  2. G. E. Hinton and R. R. Salakhutdinov (2006). “Reducing the dimensionality of data with neural networks.” Science 313.
  3. I. Goodfellow, Y. Bengio and A. Courville (2016). Deep Learning, chapter 14. MIT Press.