Skip to content

6.1 Autoencoders

ready≈ 11.5 hoursNeeds: 3.6, 1.2Unlocks: 6.2
  1. 1 Learn
  2. 2 Bridge & derive
  3. 3 Explore (Autoencoder latent explorer)
  4. 4 Do
  5. 5 Check

Learning without labels. Part 6 is about models that learn the structure of data itself. The simplest is an autoencoder: an encoder maps an image x to a short code z, a decoder maps z back to a reconstruction x̂, and the only loss is how far x̂ is from x (Goodfellow et al., chapter 14). The code is too small to copy the input, so the network has to find what the images have in common.

Linear means PCA. With linear maps and squared error, the best k-number code spans the same subspace as the top k principal components (Goodfellow et al., section 14.1). It finds the subspace, not PCA’s basis: any invertible mixing of the code gives the same reconstructions. The derivation below follows from Eckart–Young (1.2).

Nonlinear means bent. With hidden layers and nonlinearities the code can follow a curved surface through image space. On MNIST a deep autoencoder’s 2D codes separate the digits far better than PCA’s two components, with no labels used (Hinton and Salakhutdinov 2006, figure 3). For pixels in [0, 1] the usual loss is binary cross-entropy on the decoder’s logits: the negative log-likelihood of an independent Bernoulli per pixel (2.4).

Denoising. Corrupt the input, for example by setting a random half of the pixels to zero, and train the network to reconstruct the clean image. It can no longer get away with copying, so it must learn what digits look like (Vincent et al. 2010).

Not yet a generative model. Nothing makes the codes fill the latent space. Between and around the clusters lie codes the decoder never saw in training, and decoding a random code can give a smudge instead of a digit. The variational autoencoder (6.2) fixes this by asking the codes to follow a known distribution that you can sample from (Kingma and Welling 2013).

A linear autoencoder can do no better than PCAstep 1 of 6
  1. L(We,Wd)=1N∥X−XWe⊤Wd⊤∥F2L(W_e, W_d) = \tfrac{1}{N} \lVert X - X W_e^\top W_d^\top \rVert_F^2

Autoencoder latent explorer

Decoding latents into images

Static view on small screens. Open this page on a laptop to interact.

latent space of mode
0123456789
decoded from z = (0.00, 0.00)
4 of the 10 nearest test codes are a 4 distance to the nearest test code: 0.6× the typical gap between neighbouring codes
test reconstruction error, squared error summed over 784 pixels: autoencoder 34.9, PCA 43.6 (both with 2 numbers per image)

Each dot is one test image, placed where the encoder sends it; colour is its label, which the autoencoder never saw. Click or drag anywhere in the plane (or use the arrow keys on the ring) to decode that point. PCA's plane is a linear projection, so its decoder can only add two fixed images to the mean digit; the autoencoder's decoder is a neural network and bends the plane through image space. Neither was trained to make the empty regions decode to anything sensible, so points away from the dots can give smudges that are no digit: a variational autoencoder (6.2) fixes that by making the codes fill a known distribution.

A scatter plot of where 1,500 MNIST test images land in a 2D latent plane, coloured by digit, with buttons to switch between a small trained autoencoder and a 2D PCA of the same images. In the first mode, clicking or dragging in the plane decodes that point into a 28 × 28 image, with the labels of its nearest test codes and how far it is from the data. In the second mode, two handles A and B set a line; one row shows eight images decoded along the line and a second row blends the two end images pixel by pixel. A readout gives each method’s test reconstruction error. Keyboard: Tab to the ring (or to A and B) and use the arrow keys; Shift moves faster.

Autoencoders: edit exercises/stubs/m6_1_autoencoders.py on your own machine. This task needs PyTorch, which the in-page runner does not have.

pip install torch   # CPU build is enough for the tests
mlc check 6.1
Hints · linear_ae_loss_and_grads 0/3 revealed
Hints · subspace_overlap 0/3 revealed
Hints · train_step 0/3 revealed

Then train on MNIST. Open notebooks/6.1-autoencoders.ipynb on Kaggle (a CPU is enough) and paste in your 6.1 file. It checks your linear autoencoder against PCA, plots your 2D codes next to PCA’s, compares codes of 2, 8 and 32 numbers, trains a denoising autoencoder, and decodes interpolations and random codes.

1. A linear autoencoder with a k-number code is trained to convergence with squared error. What has it learned?
2. Why must an autoencoder either have a small code or see corrupted inputs?
3. For pixels in [0, 1], why does the decoder output logits scored with binary cross-entropy?
4. You decode a code that lies far from every training code. What do you get?
  • not yet: All tests pass (0/?)
  • not yet: Checkpoint quiz ≥ 80%
Finish the items above first.
Learner log: time spent and points of confusion

Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.