Can we describe an image with a few numbers, and rebuild it from them?
Slides titled The math give the formulas and proofs; the ideas are complete without them.
Builds on: Day 36: Neural networks.
A 28 \times 28 image is a vector of 784 grey values: one point in \mathbb R^{784}.
Today, start with images of two pixels: a point in the plane, which we can draw.
PCA (unit Principal component analysis): two neighbouring pixels form a cloud along the diagonal. PC1 (blue), the direction of largest spread, is an eigenvector of the covariance matrix S. Keep the score along it; rebuild by going back along the line. Lost (red): the spread along the dropped directions, the sum of their eigenvalues.
Neural networks (unit Neural networks): a layer is a matrix, then a bend (ReLU); without the bend, layers collapse to one matrix. Training walks downhill on an error.
Two pixels, mean subtracted. PC1 is the unit vector \mathbf u = (0.8,\ 0.6). Image \mathbf x = (3,\ 1).
With the mean added back: \text{encode}(\mathbf x) = \mathbf u^\top(\mathbf x - \bar{\mathbf x}), \ \text{decode}(s) = \bar{\mathbf x} + s\,\mathbf u.
With k components: k scores, and \hat{\mathbf x} = \bar{\mathbf x} + s_1\mathbf u_1 + \dots + s_k\mathbf u_k.
encode(x): # image → K numbers
for k = 1 … K: s_k = u_k · (x − mean)
decode(s): # K numbers → image
x̂ = mean + s_1 · u_1 + … + s_K · u_K
error = average over all images of |x − x̂|²
encode and decode become small networks.The PCA unit showed that, for one unit direction \mathbf u, the mean squared reconstruction error of centred data is \operatorname{tr}S - \mathbf u^\top S\mathbf u, smallest at the top eigenvector, and that with k components the error is the sum of the dropped eigenvalues.
A bright spot moves past two pixels. One true number (its position), but the images lie on a curve.
Replace both straight maps by small neural networks (unit Neural networks):
\text{encoder: } 2 \to 16 \to 1, \qquad \text{decoder: } 1 \to 16 \to 2 \quad (\text{ReLU}).
An autoencoder gives every image a place z in a small latent space, and a decoder that turns any z into an image.
| Idea | In one line |
|---|---|
| Image as a point | one axis per pixel; images of one kind fill a thin region |
| Encoder / decoder | image → few numbers z → image |
| PCA | the best linear autoencoder: encode = project, decode = mean + scores × eigen-images |
| Error of PCA | the spread of the images in the directions that were dropped |
| Bottleneck | must be narrow, or the network just copies |
| Autoencoder | encoder and decoder are networks: the decoder can follow a curve |