Simple Algorithm
Deep Learning · Activation · 17 min

Learning Representations

Features learned without labels, reused across tasks and opened up for inspection.

Feed a photograph to a trained image classifier and stop it one layer before the answer. Out comes a list of a few thousand numbers. That list, the network's representation of the photo, is where most of the useful work lives: it can be learned without a single label, reused for tasks the network never saw, built for molecules and social networks, and opened up to see what it encodes.

What you’ll learn

  • Explain what a learned representation is, and why a good one makes a task easy: similar things land close together and classes become separable by a straight cut.
  • Describe how self-supervised learning makes its own training signal from unlabeled data, by predicting hidden parts or by contrasting two views, and why it must avoid the collapsed solution.
  • Compare three ways to reuse a pretrained network: a linear probe, full fine-tuning and parameter-efficient methods such as adapters and LoRA.
  • Describe how a graph neural network builds node representations by message passing, and what goes wrong after too many rounds.
  • Explain why features need not line up with single neurons, and what superposition means.

Things as points

Every hidden layer of a network turns its input into a vector of numbers: the network's representation of that input at that layer. The numbers respond to properties of the input, its features, whether or not anyone could name them.

The same trick gives words, photos, songs, products and users their own vectors, called embeddings. Once things are points in a space, "similar" becomes "nearby". The usual measure is the cosine of the angle between two vectors: 11 when they point the same way, 00 when they are unrelated, −1-1 when they are opposite.

cos⁡(a,b)=a⋅b∥a∥ ∥b∥=a1b1+a2b2+⋯+adbd∥a∥ ∥b∥\cos(\mathbf a,\mathbf b)=\frac{\mathbf a\cdot\mathbf b}{\norm{\mathbf a}\,\norm{\mathbf b}}=\frac{a_1b_1+a_2b_2+\dots+a_db_d}{\norm{\mathbf a}\,\norm{\mathbf b}}
Cosine similarity ignores length and compares direction only. In high dimensions two random vectors have a cosine near 00.
Think first In a good word embedding, king−man+woman\text{king}-\text{man}+\text{woman} lands near queen. What does that tell you about the representation, and what does it not tell you?

It tells you that the step from a male word to its female counterpart is roughly the same direction for many pairs, so it can be added anywhere. Embeddings trained only to predict nearby words show this regularity without ever seeing an analogy (Mikolov et al., 2013). It does not mean that one coordinate stores gender: a concept is a direction, usually spread across many coordinates. That difference between directions and coordinates returns at the end of this lesson.

A good representation makes the task easy

Why do representations matter? Because a problem that is hard in one description can be trivial in another. Picture two classes of points in the plane: one class fills a disc around the origin, the other a ring around the disc. No straight line separates them in the coordinates (x1,x2)(x_1,x_2). Now add a single new feature, the squared distance from the center:

r2=x12+x22⟹disc if r2<1,ring if r2>1r^2=x_1^2+x_2^2\qquad\Longrightarrow\qquad\text{disc if }r^2<1,\quad\text{ring if }r^2>1
In the new description one threshold, a single "straight cut", separates the classes. The data did not change; only how they are described.

Classical machine learning hand-designed such features: histograms of edge directions for images, word counts for text. A deep network learns its own. Each layer re-describes the data, and training adjusts every layer at once so that, by the last hidden layer, a simple linear rule is enough. That is representation learning. In trained image networks the early layers respond to edges and color blobs, middle layers to textures and patterns, and later layers to object parts (Zeiler & Fergus, 2014).

Check your understanding

Points with x12+x22<1x_1^2+x_2^2<1 are class A and points with 2<x12+x22<32<x_1^2+x_2^2<3 are class B. You may give a linear classifier one extra feature. Which one makes the classes separable by a single threshold?

  1. x1+x2x_1+x_2
  2. x1x2x_1x_2
  3. x1−x2x_1-x_2
  4. x12+x22x_1^2+x_2^2
Show the answer

D. x12+x22x_1^2+x_2^2

The squared distance from the origin is below 11 for every A point and above 22 for every B point, so the threshold 1.51.5 separates them. A good feature captures what actually distinguishes the classes.

Think first If features matter so much, why not keep designing them by hand?

For pictures, sound and language nobody knows the right features. Hand-designed edge-histogram features were the state of the art in image recognition until deep networks trained on ImageNet overtook them in 2012, and learned features have won in one field after another since. People still choose the architecture and the training task; the features themselves are learned.

Labels from the data itself

Labels are expensive: someone has to look at each photo. Unlabeled photos, text and audio are nearly free. Self-supervised learning invents a training task whose answers are already in the data, a pretext task, and keeps the representation it learns along the way. Two families dominate:

  • Predict what is hidden. Hide some words and predict them (masked language modeling, as in BERT); hide most of an image's patches and redraw them (masked autoencoders); or predict the next word, the training task of GPT-style language models.
  • Contrast two views. Make two altered copies of a photo (crop it, recolor it, flip it). Train the network to give the two copies, a positive pair, nearby representations, while copies of different photos, negative pairs, stay apart. This is contrastive learning.
Check your understanding

A contrastive method trains an encoder so the two views of each photo get similar embeddings. Why does it also need negative pairs?

  1. Without them, mapping every input to the same embedding minimizes the loss perfectly
  2. Negatives only speed up training; the result would be the same without them
  3. Negatives carry the class labels that the method secretly uses
  4. Gradients cannot flow through the augmentations unless negatives are present
Show the answer

A. Without them, mapping every input to the same embedding minimizes the loss perfectly

The pull between positives is satisfied just as well by a constant encoder, which carries no information about the input. Pushing different inputs apart rules that solution out. You saw it in the widget: with the negatives off, everything collapsed.

Pretrain once, reuse everywhere

Given a pretrained network and a new task, you can reuse it in several ways, trading cost against accuracy:

MethodWhat is trainedCost per new taskTypical use
Linear probeonly a new linear output layer; the network is frozentinylittle labeled data; measuring how good a representation is
Full fine-tuningevery weight, starting from the pretrained ones, usually with a small learning ratea full copy of the modelthe most accurate option when data and compute allow
Parameter-efficient fine-tuning: adapters, LoRAa small number of new weights added to a frozen networkoften well under 1% of the weightsadapting very large models cheaply, one small add-on per task
Zero-shot usenothingnonetasks that can be described in words, as with CLIP
Predict before you look

A frozen network turns each image into 512 numbers. You train a linear probe to sort images into 10 classes: one weight for every (feature, class) pair plus one bias per class. How many numbers does the probe learn?

Show the answer

5130 parameters

512×10=5,120512\times10=5{,}120 weights plus 1010 biases: 5,1305{,}130 parameters, while the frozen network may have tens of millions. A probe this small cannot invent new features; it can only read out what the representation already makes linearly available. That is why probe accuracy is the standard yardstick for comparing representations.

Transfer is not free. The further the new data are from the pretraining data (X-rays instead of holiday photos), the smaller the gain, and occasionally pretraining even hurts. And a network fine-tuned on a new task can lose what it knew before, a failure called catastrophic forgetting.

Graphs: learning from connections

Some data are networks: atoms joined by bonds, people joined by friendships, papers joined by citations, road junctions joined by roads. A graph has nodes and edges, and unlike an image it has no grid and no natural order of its nodes.

A graph neural network (GNN) computes a representation for every node by message passing. In each layer every node gathers its neighbors' vectors, combines them (for example by averaging), and updates its own vector. After one layer a node reflects its immediate neighbors; after kk layers, everything within kk hops. To describe a whole graph, say a molecule, a final step pools all the node vectors into one.

Check your understanding

A graph neural network has 3 message-passing layers. Which nodes can influence node vv's final representation?

  1. Every node in the graph
  2. Only vv's direct neighbors
  3. Only nodes within 3 edges of vv
  4. Exactly 3 nodes, chosen by the network
Show the answer

C. Only nodes within 3 edges of vv

Each layer passes information one edge further, so after 3 layers vv has heard from everything within 3 hops and nothing beyond. To reach farther you need more layers, which brings the oversmoothing you just watched.

Looking inside

Interpretability asks what a representation actually encodes. The natural guess is one feature per neuron: a "cat neuron", a "wheel neuron". Sometimes that holds, but often a neuron responds to several unrelated things. In one image classifier, a single neuron responds to cat faces, fronts of cars and cat legs (Olah et al., 2020). Such neurons are called polysemantic (polysemanticity).

One explanation is superposition. A layer can store more features than it has neurons if each feature gets a direction, a particular mix of neurons, instead of a neuron of its own. Directions cannot all be at right angles, so features interfere with each other. But if each feature is rarely active, collisions are rare, and the trade can pay off (Elhage et al., 2022).

Think first If the units of a representation are directions rather than neurons, how could you find them?

Two common routes. A probe is trained to detect a concept from the activations, and its weight vector is a candidate direction for it. A sparse dictionary, learned with a sparse autoencoder, rewrites each activation vector as a combination of a few directions out of many thousands, without saying in advance which concepts to look for. Either way, an experiment that changes the activation along the direction is needed to show the network actually uses it.

In the Representation lessons you'll build the machinery behind each idea:

  • Self-Supervised Learning: pretext tasks, the InfoNCE loss behind contrastive learning, momentum encoders, masked prediction, and how to evaluate what was learned.
  • Fine-Tuning & Transfer: linear probes versus full fine-tuning, which layers transfer, and adapters, prompt tuning and LoRA with their parameter counts.
  • Graph Neural Networks: message passing, graph convolutions, attention on graphs and readout for whole graphs.
  • Interpretability: Looking Inside Networks: saliency maps, probes, superposition, sparse autoencoders and circuits.

The Emergence lessons then go to the research frontier: collapse without negatives, comparing representations across networks, forgetting and model merging, what GNNs cannot tell apart, and dictionary learning at scale.

Key takeaways

  • A representation is the vector of numbers a network computes for an input at some layer. A good one puts similar things close together and makes the task simple, often solvable by a straight cut.
  • Self-supervised learning manufactures labels from unlabeled data, by predicting hidden parts or by pulling two views of one thing together while pushing others apart, and it must avoid the collapsed solution that maps everything to one point.
  • Pretrained representations are reused with linear probes, full fine-tuning, or parameter-efficient add-ons such as adapters and LoRA; models like CLIP can even transfer zero-shot.
  • Graph neural networks build node representations by repeatedly combining each node with its neighbors: kk layers reach kk hops, and too many layers blur everything together.
  • Features need not line up with neurons: with sparse features a layer can hold more features than dimensions (superposition), which makes single neurons polysemantic and sends interpretability looking for directions.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

Which property most clearly marks a representation as good for a classification task?

  1. It has as many numbers as possible
  2. The classes can be separated by a simple rule, such as a linear classifier, on top of it
  3. Every neuron stands for exactly one human concept
  4. It lets you reproduce the input pixels exactly
Show the answer

B. The classes can be separated by a simple rule, such as a linear classifier, on top of it

The point of a representation is to make the task easy. If a linear readout suffices, the hard work has been done by the features. That is also why linear-probe accuracy is the standard test.

Question 2

In masked prediction, such as BERT's pretraining, where does each training label come from?

  1. From human annotators who label each sentence
  2. From a smaller network trained beforehand
  3. From random numbers, since any target will do
  4. From the input itself: the words that were hidden are the answers
Show the answer

D. From the input itself: the words that were hidden are the answers

Self-supervised learning manufactures its targets from unlabeled data. Hide part of the input, train the network to predict it, and the hidden part is the label. No annotation is needed, which is why these methods can use enormous datasets.

Question 3

How does a linear probe differ from full fine-tuning?

  1. A probe trains only a new linear output layer on frozen features; fine-tuning updates all the network's weights
  2. A probe updates all the weights; fine-tuning trains only the last layer
  3. A probe needs no labeled data at all
  4. They train the same weights and differ only in the learning rate
Show the answer

A. A probe trains only a new linear output layer on frozen features; fine-tuning updates all the network's weights

The probe leaves the representation untouched and asks how much a straight cut can read from it. Fine-tuning reshapes the representation for the new task: usually more accurate, but costlier, and it can overwrite what the network knew.

Question 4

You stack 30 averaging message-passing layers on a connected social network. What do you expect the node representations to look like?

  1. Each node keeps mostly its own starting features
  2. Close to one shared vector for every node, because repeated averaging blurs the differences
  3. Sharper than before, with the two communities pushed further apart
  4. Random, because 30 layers are too many to train
Show the answer

B. Close to one shared vector for every node, because repeated averaging blurs the differences

Every layer replaces each node by an average over its neighborhood. Repeating that many times on a connected graph drives every node toward the same vector: oversmoothing. On the two-community graph above, the spread of the colors was down to 11% of its start after 32 layers and 3.8% after 48. Information from far away arrives, but at the price of washing out what made nodes different.

Question 5

A layer has only 2 neurons, yet a trained model reads 5 different features back out of it. What makes this possible?

  1. Each neuron is split into several smaller neurons during training
  2. The features are stored in the biases rather than the neurons
  3. The features are rarely active, so the model gives each one a direction and tolerates a little interference when two are active together
  4. It is impossible: a layer with 2 neurons can carry at most 2 features
Show the answer

C. The features are rarely active, so the model gives each one a direction and tolerates a little interference when two are active together

This is superposition. Five directions cannot all be at right angles in two dimensions, so features interfere; but if each is usually zero, two rarely fire at once and the model comes out ahead. A side effect is that single neurons respond to several unrelated features: polysemanticity.

Go deeper

Further reading

  • Y. Bengio, A. Courville & P. Vincent, "Representation learning: a review and new perspectives," IEEE Transactions on Pattern Analysis and Machine Intelligence 35(8) (2013).
  • S. J. D. Prince, Understanding Deep Learning, ch. 13 "Graph neural networks" and ch. 14 "Unsupervised learning" (MIT Press, 2023), free at udlbook.github.io/udlbook.
  • I. Goodfellow, Y. Bengio & A. Courville, Deep Learning, ch. 15 "Representation Learning" (MIT Press, 2016), free at deeplearningbook.org.