Chapter 4 · Representation learning · ≈12 min
The Hidden Map
Squeeze every digit through two numbers and back. No labels, no answers — yet a map of meaning appears on its own.
In the last chapter you trained a classifier, and every one of its ten thousand digits arrived with an answer stapled to it: this one is a 4 — be less wrong about that. The label did the pointing, and the network learned whatever the pointing required. But almost nothing you have ever learned from came labeled. The world mostly hands you the thing itself — light, sound, ink — and no answer key. So this chapter takes the labels away entirely and asks what is left to learn from.
Here is the bet the whole chapter rides on: you understand a thing to the degree that you can rebuild it. Not name it — rebuild it, all 784 pixels of it. An autoencoder is that bet built as a machine in two halves. The encoder squeezes each digit down to two numbers. The decoder takes those two numbers and tries to repaint the digit they came from. Between the halves sits the bottleneck — the width-2 waist of an hourglass — and every digit must pass through it. Nothing is bent at the waist, the way it is at every other hidden layer: the point of the place is to be a plain coordinate system, a map you read off directly rather than through a squashing function.
The idea is older than it looks, and it has a linear ancestor. Take every bend out of both halves — leave the encoder and decoder as plain matrices — and the best this machine can do is principal component analysis: the plane along which the data varies most — which is what Pearson went looking for in 1901, under exactly that description: lines and planes of closest fit.1 Not quite PCA, in fact. Training lands the machine on that plane but not on the axes drawn across it — any two directions spanning the plane cost the loss the same, so nothing in the objective prefers the principal ones — and every flat place on the way there that is not the bottom is a saddle.2 Everything a deep autoencoder buys over that, it buys with the bends.3
Training needs nothing this book has not already built. The loss is the distance between each digit and its own reconstruction,
and gradient descent falls downhill on it as always. Read the formula twice and notice what is missing: there is no anywhere. The data grades itself — is both the question and the answer key. Then notice what the bottleneck is for. Allowed 784 numbers in the middle, the network could pass its input straight through and learn nothing. Allowed two, copying is impossible. The only way to reconstruct well is to keep what matters about a digit and discard the rest — and “what matters” is precisely the thing nobody told it.
One clause is still missing, and it is the clause that makes the map worth drawing. As written, the loss has no opinion whatever about where the encoder puts things. It is equally happy with a tidy disc around the origin and with a sprawl that reaches out to a coordinate of forty, continents of dead space in between, growing for as long as you train. So we ask for one more thing. Instead of a point, the encoder proposes a small Gaussian cloud per digit — a centre and a spread — the decoder is handed a sample of it,
and the loss charges rent on how far that proposal drifts from the plain unit Gaussian:
That is a variational autoencoder4 5 , and the two changes buy two different things. The noise means the decoder is never taught a single address, only a neighbourhood, so nearby addresses are forced to decode to similar digits: the map comes out smooth instead of a lookup table full of gaps. The rent means the map stays where you left it — centred on the origin, a couple of units across, no drift and no sprawl — which is why every plate below can frame the whole thing at once and keep it framed while it trains. Each digit is drawn at the centre of its own proposal; the noise is a training-time device. The dial sets how hard the rent is charged, and it is a real trade rather than a tuning detail: turn it up and the map gets tidier and the rebuilds get worse.6
The latent space
While it trains, consider the pinch point. Halfway through the network each digit exists as exactly two numbers, — a 392-fold compression, and the only thing the decoder is ever shown. Whatever survives the squeeze is, by construction, everything the reconstruction needs: loop or no loop, slant, stroke weight, openness. Ten thousand digits become ten thousand points on a flat sheet. That sheet is called the latent space — the network's private map — and because this one happens to be two-dimensional, we can look at the whole of it at once. (Widen the waist to three numbers and the sheet becomes a small globe you can turn.)
Notice also what the rebuilt row looks like when it stops improving: soft. Train it for an hour and it stays soft, and that is not a defect to be tuned away — it is the arithmetic showing its work. Squared error is minimized by an average, so the decoder's best possible answer at a given address is the mean of every digit that lands there, and two numbers cannot keep the difference between one person's 3 and another's. The jitter we just added pushes the same way — an address is trained together with its neighbours, so it answers for all of them. The blur is a receipt: it is exactly what the waist had to throw away. Add a layer to each side, or widen the waist, and the rebuild sharpens — those are the knobs a practitioner reaches for, and they come with the trade this chapter is built to make visible. At eight or sixteen numbers the digits come back nearly intact, and the map stops being something you can look at directly: the plate below falls back to plotting the three directions the cloud varies along most, a shadow of a space too wide to see. Two numbers is a deliberately cruel waist, chosen so the whole map fits on one page.
That blur belongs to the loss rather than to the hourglass, and the distinction is most of what the last decade of generative modelling has been about. Every sharp model since has attacked the clause rather than the network. Adversarial training replaced the pixel-by-pixel score with a second network paid to say whether the result looks real7 ; diffusion models, which is what the image generators you have used are, sidestep the averaging by learning to remove a little noise at a time, so the model commits gradually instead of hedging in one shot.8 Neither threw the hourglass away. A generative adversarial network is its second half working alone, and the image generators most people have used run their diffusion not in pixels at all but inside the latent space of an autoencoder trained exactly like this one — the waist did not go anywhere, it just stopped being the part that gets scored.9
The plate below puts the whole map on one page, with one twist held in reserve. In ink you see the map as the model knows it: anonymous points, arranged purely by reconstruction convenience. In images, each digit is printed at its own latent address, so the map reads like an atlas of handwriting — and the slider decides how many get printed. And the twist, which works over either view: colorize tints everything by its true label, information the network has never seen, not in any gradient, not once. If the bottleneck's two numbers carry nothing about digit-kind, the tints will fall like confetti.
Kind, as a by-product of thrift
They do not fall like confetti. The model was never told what a seven is, and there on the map is the country of sevens — because reconstructing sevens well requires gathering them. Two digits that need the same decoding must sit near each other in the bottleneck, or the decoder cannot serve both; kind emerges as a by-product of thrift. This is representation learning, and it is the thesis of Bending Space wearing new clothes: the network deforms its input space until the task becomes easy, and the deformed coordinates are the real product. There we had to trust that the hidden layers were building something. Here the map is laid flat where you can sweep your hand across it.
The map has geography worth reading. Ones press into a thin peninsula — a stroke with little more than a slant to its name. Fours and nines blur along a shared border, exactly as they do in handwriting. And the interpolation walk crosses country lines on foot: between a 3 and an 8 the decoder produces in-betweens no person ever wrote, rendered with perfect assurance.
There is one more way to put the question, and it is the bluntest. Stop following the data altogether. Lay a uniform grid of addresses over the whole square — no digit chose them, we did — and make the decoder answer for every single one.
Watch a 3 shade into an 8 and the 8 into a 5, one tile at a time. Where the data actually lives you need not take anything on faith: pointing at a tile measures its address against every one of the two thousand held-out digits and reports the closest, in the waist's own units. Work outward and that distance grows — the prior keeps the digits packed near the origin, so the edges and corners of this square are addresses no real digit ever came near. The decoder answers there anyway, sharp-edged and committed, because is a smooth function that must produce 784 pixels for every point of the plane, though it was trained only where the data lives. You are looking at the seed of generative models: pick a latent point, decode it, and you have manufactured a thing that never existed. You are also looking at their oldest defect. Nothing in the training ever gave the decoder a way to say there is nothing here; it was built to produce 784 pixels for any address you hand it, and it does, with the same steadiness everywhere. When a much larger model answers a question about a thing that does not exist, in the same fluent voice it uses for things that do, the machinery is not identical — but the shape of the failure is, and it comes from the same place: a smooth function, confidently extending past the last example it was shown.
From map to embedding
So far we have read the map as a picture. It has a second life as a tool, and this is the one the industry actually runs on. Stop thinking of as a location on a sheet and start thinking of it as a short list of numbers attached to a thing — an embedding. The encoder is now a machine that turns anything of its type into a vector, and once your things are vectors, “which of these is most like that one?” stops being a philosophical question and becomes arithmetic: embed everything once, keep the vectors, and answer a query by finding the nearest ones. That is similarity search, and it is how a photo library finds the other pictures of your dog, how a store recommends the next item, how duplicate documents get caught, and how a language model is handed the right three paragraphs before it answers you.
The plate below hands you the query end of that machine. Draw a digit — your handwriting, not the dataset's — and it is centred, pushed through the encoder, and turned into the same short list of numbers every held-out digit already carries. Then the two thousand of them are sorted by how near they landed and the closest eight are printed. Nothing about the encoder was ever told what a 3 is, so when the row comes back full of 3s, that is the geometry answering, not a lookup.
The second row is a control, and a fair one: the same stroke, the same candidates, the same metric, but distances measured between raw images instead of embeddings. Watch where the two rows disagree. Pixel distance rewards ink that lands in the same places, so it will happily return a fat 1 for a thin 7 and rank a slanted 3 far from an upright one; the map is looking for the same shape drawn any way at all. Draw badly on purpose — a wobbly 8, a 4 with an open top — and the rows separate fastest.
It also introduces the measuring stick that the next chapters use by default. Instead of the straight-line distance between two vectors, compare their directions:
Cosine similarity is 1 when two vectors point the same way, 0 when they are at right angles, −1 when they oppose — and it ignores length entirely. That is usually what you want of an embedding, because in a wide space the direction tends to carry the meaning while the length carries something duller, like how common or how confident the thing is. It is not a law, though, and this chapter is a good place to see the exception: at a two-number waist, throwing away the radius throws away half the map, so plain distance often wins. Try both.
The meters keep score over held-out digits rather than your handwriting, and at a two-number waist they say something you might not expect: the pixel row wins, and not narrowly. That is worth sitting with rather than explaining away. Part of it is arithmetic — you gave the map two numbers and the pixels 784 and then asked for a fair fight. The rest is that MNIST is the kindest dataset raw pixel distance will ever meet. Every digit here, and every stroke you draw, is centred by its ink and scaled into the same box, in white on black with no background, no lighting and no camera. Under those conditions overlap really is a decent stand-in for shape, which is why nearest-neighbour on raw MNIST pixels has been a respectable baseline since the 1990s. Photograph the same digits on a desk instead and that baseline is gone by lunchtime, while an encoder trained on desks would barely notice. Pixel distance is not wrong here. It is being flattered.
So run it the other way. Go back to the first plate, widen the waist to sixteen, and train for a few minutes: the map's meter climbs out of the fifties and settles level with the pixel row, which has nothing to learn and never moves. Level — not past it. That is the result, and it is the more useful one, because the tie is being won with sixteen numbers against 784. A stored embedding is fifty times smaller, and every search is fifty times less arithmetic; at a billion items that is the difference between an index and an impossibility.
The reason the map only ties is in the loss. Nothing in it ever asked for digits of a kind to sit together. The encoder was paid to rebuild pixels, so it kept what rebuilding needs — shape, but also slant, weight, size — and two 7s in different hands can still land apart because they genuinely differ in what the decoder must draw. Kind-ness came out as a side effect, and side effects tie. If you want a space where same kind is near by construction, you have to say so in the loss: show the model two views of the same thing and require it to pull them together while pushing everything else away.10 That is contrastive learning, and it is how modern encoders are actually trained. For images the two views are two crops of one photograph11 ; for the encoder behind every search-your-photos-by-typing feature, the two views are a picture and its caption, which lands images and text in one shared space12 . It is the same geometry you have been reading all chapter, with the arrangement asked for out loud instead of hoped for.
Carry the trick forward, because the rest of the book stands on it. No one labeled anything today: the data was its own target, and the loss came from hiding part of the pipeline and demanding restoration. That maneuver is called self-supervision, and it scales past anything hand-labeling could reach. Here, a digit graded itself. In the next chapter a sentence grades itself — every word is the label for the words before it — and a model that plays that game long enough backs into knowing the language.
Sources
- 1 On Lines and Planes of Closest Fit to Systems of Points in Space Principal component analysis, fourteen pages and no matrices — Pearson works it out in the language of best-fitting lines and planes through a cloud of points, which is still the most useful way to picture what it does.
- 2 From Principal Subspaces to Principal Components with Linear Autoencoders What a linear autoencoder actually finds. Baldi and Hornik proved in 1989 that its loss has a single minimum — the projection onto the principal subspace — and that every other critical point is a saddle; what it does not have is any preference among the axes drawn across that subspace. This note shows how to get the principal ones back out of the weights afterwards.
- 3 Reducing the Dimensionality of Data with Neural Networks The paper that made deep autoencoders work, and the one that shows what the waist buys over its linear ancestor: strip every bend out of an autoencoder and the best it can do is principal component analysis.
- 4 Auto-Encoding Variational Bayes The variational autoencoder, and the reparameterisation that makes it trainable — sample the noise outside the network so the gradient has a path through the sampling step rather than into it.
- 5 Stochastic Backpropagation and Approximate Inference in Deep Generative Models The same idea, arrived at independently and published within months. It happens more often than the textbooks admit.
- 6 β-VAE: Learning Basic Visual Concepts with a Constrained Variational Framework Where the β in this chapter's loss comes from. Turn the rent up and the map gets tidier and the rebuilds get worse; the paper is an argument about what the tidiness is worth.
- 7 Generative Adversarial Networks The first widely used answer to the blur: stop scoring a rebuild against its original pixel by pixel, and train a second network to say whether it looks real.
- 8 Denoising Diffusion Probabilistic Models The answer that won. Learn to remove a little noise at a time and the model never has to average over everything an address could mean — it commits, gradually.
- 9 High-Resolution Image Synthesis with Latent Diffusion Models Where this chapter's hourglass ended up. Diffusion in pixels is ruinously expensive, so run it inside the latent space of an autoencoder instead — which is what the image generators most people have used are doing underneath.
- 10 Representation Learning with Contrastive Predictive Coding The contrastive loss in the form nearly everything since uses it: score the true partner against a crowd of impostors.
- 11 A Simple Framework for Contrastive Learning of Visual Representations Two crops of one photograph should land in the same place; two crops of different photographs should not. That, done carefully enough, is most of what a modern image encoder learns.
- 12 Learning Transferable Visual Models From Natural Language Supervision CLIP — the same contrastive trick with the two views being a picture and its caption, which puts images and text into one shared space and is why you can search a photo library by typing.