05 · 2013 · Compress, then rebuild

Autoencoder

An autoencoder is trained on a strange task: copy the input to the output, but through a bottleneck. Here a 28×28 image (784 numbers) is squeezed into just two numbers, then rebuilt. To do that well, the network must discover what matters about handwritten digits.

Those two numbers are coordinates on a map. Similar digits end up close together, and the space between them is smooth, so you can walk across the map and watch one digit morph into another. This is a variational autoencoder (Kingma and Welling, 2013).

How it works

  1. Encode. An encoder network reads the image and outputs a point (x, y) on the map.
  2. Decode. A decoder network takes that point and paints a 28×28 image from it.
  3. Train. The loss compares the rebuilt image with the original. A second term keeps the map tidy and filled in (that is the “variational” part), so every location decodes to something sensible.
  4. Explore. After training you can throw away the encoder and use only the decoder as a tiny image generator: choose a point, get a digit.

Try it

runs in your browser
drag across the map

Decoded from this point

x
—
y
—
Dots are real test digits, placed by the encoder. The faint background is what the decoder paints everywhere else.

Encode your own

Draw a digit on the left. The network encodes it to a point on the map, then rebuilds it on the right. Details get lost: that is the price of squeezing 784 numbers into 2.

Things to try

Why compress?

Squeezing data through a bottleneck forces a network to find the underlying factors: here roughly “which digit” and “how slanted or thick”. Those learned codes are useful for search, anomaly detection and compression.

Where they are used

Spotting faulty machines (anything that reconstructs badly is unusual), denoising images, and, at large scale, the compression stage inside image generators such as Stable Diffusion, which paint in a compact latent space and decode to pixels at the end.

This model

Two 128-unit layers each way, trained for 30 passes over 60,000 MNIST digits. The map and your drawings never leave your device.