Search

Search papers and lessons

Representation Learning

Level 3 · 11 min read

Representation learning trains a model to find its own useful intermediate features, so the data is rewritten in a form where the task becomes easier.

On this page

  • Colour = topic mix
  • Calculus
  • Linear algebra
  • Probability
  • Neural network basics
  • Losses & regularization
  • Optimizers
  • Sign in to see your progress

TL;DR

A representation is a new way of describing an input, as a list of numbers the model computes for itself. Representation learning means training a model so that these numbers are useful, usually because they make the final task easy: a simple classifier on top can then do the job. Instead of hand-designing features, you let gradient descent shape them.

Where you'll see it: the hidden layers of image classifiers, the word vectors inside language models, and the pretrained encoders that get reused for search, recommendation and transfer to new tasks.

Intuition

Rewriting the data until the answer is obvious

Imagine trying to sort red and blue marbles that are scattered on a table in a tangled pattern. You cannot separate them with one straight cut. Now imagine someone gives you a new map of the table where the marbles are already in two clean clusters. The same marbles, in a different description, and the sorting task is trivial.

That is the whole idea. The raw input (pixels, letters, sensor readings) is hard to act on directly. A representation is a transformed description of the same input, and a good one makes the next step easy. Representation learning is the search for that transformation, done automatically from data.

Why not just design the features?

For a long time, practitioners wrote feature extractors by hand. An edge detector, a histogram of colours, a count of word pairs. This works, but a person has to guess which features matter, and the guesses rarely transfer well to a new problem.

A neural network can learn the features as part of training. Each hidden layer produces a new vector, and the layers closer to the output tend to capture more abstract properties. In an image model the first layers respond to edges and textures, the middle ones to parts such as eyes or wheels, and the last ones to whole objects. Nobody programmed those stages. They emerged because they reduce the training loss.

−1−0.500.511.522.5300.511.52xfeature value
  • h1 = ReLU(x)
  • h2 = ReLU(x - 1)
Two hidden units bend the input line. The new coordinates h1 and h2 are a representation: a different description of x that a straight line can use.

Two views of one goal

There are two ways to say what a representation is for. The first is compression: keep the information in the input that matters and throw away the rest. The second is usefulness: keep whatever helps the next task, even if it throws away a lot of detail. Modern systems mostly chase the second, and the best representations are the ones that transfer, meaning they still help on tasks the model was never directly trained on.

The math

Definition: Representation and encoder

Let x\mathbf{x} be an input vector. An encoder is a function fθf_\theta with parameters θ\theta that maps the input to a representation h=fθ(x)\mathbf{h} = f_\theta(\mathbf{x}). The representation h\mathbf{h} is a vector of length dd, where dd is usually smaller than the input length but not always. The encoder is learned, so θ\theta is set by minimising a loss.

A common choice is to make the encoder a stack of layers, as in a feedforward network. Write the encoder as a composition fθ=g(L)∘⋯∘g(1)f_\theta = g^{(L)} \circ \cdots \circ g^{(1)}, where each g(ℓ)g^{(\ell)} is one layer. Then the representation at depth ℓ\ell is

h(ℓ)=g(ℓ) ⁣(h(ℓ−1)),h(0)=x.\mathbf{h}^{(\ell)} = g^{(\ell)}\!\left(\mathbf{h}^{(\ell-1)}\right), \qquad \mathbf{h}^{(0)} = \mathbf{x}.

Every intermediate h(ℓ)\mathbf{h}^{(\ell)} is a representation. A task head y^=qϕ(h)\hat{y} = q_\phi(\mathbf{h}) sits on top of the last one.

The training objective

Training minimises a loss L\mathcal{L} over all parameters, the encoder and the head together:

min⁡θ,ϕ  1N∑i=1Nℓ ⁣(qϕ(fθ(xi)),yi)\min_{\theta, \phi} \; \frac{1}{N} \sum_{i=1}^{N} \ell\!\left(q_\phi(f_\theta(\mathbf{x}_i)), y_i\right)

Here NN is the number of training examples, xi\mathbf{x}_i and yiy_i are input and label of example ii, and ℓ\ell is the per-example loss. Gradient descent updates θ\theta using the gradient of this loss, which Backpropagation computes layer by layer. The representation is never given a target directly. It is shaped only by the gradient flowing back from the final task.

Linear probes

A standard way to measure a representation is the linear probe. Freeze fθf_\theta, then fit only a linear head y^=Wh+b\hat{y} = \mathbf{W}\mathbf{h} + \mathbf{b}. If a simple linear model reaches high accuracy on h\mathbf{h}, the information needed for the task is present and easy to read. If it does not, either the information is missing or it is tangled.

Self-supervised objectives

Often there are no labels. A common trick is to invent a task from the data itself, for example predicting a masked word from its neighbours, or deciding whether two augmented views came from the same image. The loss still has the form above, but the targets come from the data. This is the core of self-supervised learning, and it is one of the main reasons representation learning scales: unlabelled data is cheap.

For contrastive methods, one popular form is the InfoNCE loss. For a positive pair (hi,hi+)(\mathbf{h}_i, \mathbf{h}_i^+) and a set of negatives hj−\mathbf{h}_j^-, with similarity s(u,v)=u⊤v/∥u∥ ∥v∥s(\mathbf{u}, \mathbf{v}) = \mathbf{u}^\top \mathbf{v} / \|\mathbf{u}\| \, \|\mathbf{v}\| (the cosine similarity, a normalised dot product) and temperature τ>0\tau \gt 0,

Li=−log⁡exp⁡ ⁣(s(hi,hi+)/τ)exp⁡ ⁣(s(hi,hi+)/τ)+∑jexp⁡ ⁣(s(hi,hj−)/τ).\mathcal{L}_i = -\log \frac{\exp\!\left(s(\mathbf{h}_i, \mathbf{h}_i^+)/\tau\right)}{\exp\!\left(s(\mathbf{h}_i, \mathbf{h}_i^+)/\tau\right) + \sum_j \exp\!\left(s(\mathbf{h}_i, \mathbf{h}_j^-)/\tau\right)}.

The loss is small when the positive pair is more similar than every negative. Pulling positives together and pushing negatives apart is what gives the representation its structure.

Worked examples

Example
A hidden layer makes XOR linearly separable

Take the four binary inputs of XOR. The label is 1 when the two bits differ: (0,0)→0(0,0) \to 0, (0,1)→1(0,1) \to 1, (1,0)→1(1,0) \to 1, (1,1)→0(1,1) \to 0. No straight line in the raw input separates the 1s from the 0s.

Use a hidden layer of two ReLU units, h1=ReLU(x1+x2)h_1 = \text{ReLU}(x_1 + x_2) and h2=ReLU(x1+x2−1)h_2 = \text{ReLU}(x_1 + x_2 - 1).

  1. Compute the representation of each input.
    • (0,0)(0,0): h1=0h_1 = 0, h2=0h_2 = 0, so h=(0,0)\mathbf{h} = (0, 0).
    • (0,1)(0,1) and (1,0)(1,0): h1=1h_1 = 1, h2=0h_2 = 0, so h=(1,0)\mathbf{h} = (1, 0) for both.
    • (1,1)(1,1): h1=2h_1 = 2, h2=1h_2 = 1, so h=(2,1)\mathbf{h} = (2, 1).
  2. The two inputs with label 1, (0,1)(0,1) and (1,0)(1,0), now share one point, and the label-0 points (0,0)(0,0) and (2,1)(2,1) sit on either side of it along h1h_1.
  3. Look for a linear score y=w1h1+w2h2+by = w_1 h_1 + w_2 h_2 + b that works in the new space, predicting label 1 when y>0.5y \gt 0.5. Try w=(1,−2)w = (1, -2) and b=0b = 0.
    • (0,0)(0,0): 0−0=00 - 0 = 0. Below 0.5, label 0, correct.
    • (1,0)(1,0): 1−0=11 - 0 = 1. Above 0.5, label 1, correct.
    • (2,1)(2,1): 2−2=02 - 2 = 0. Below 0.5, label 0, correct.
  4. Interpretation: the input was not separable in its own coordinates, but after one learned layer a single linear rule classifies all four points correctly. The representation did the hard work, and the classifier on top is trivial. This is the mechanism the rest of the lesson relies on.
Example
Similar words get similar vectors

Suppose an embedding has learned three-dimensional vectors for three words. The numbers are invented for the example:

king=(0.8,0.6,0.1),queen=(0.7,0.7,0.2),apple=(0.1,0.2,0.9)\mathbf{king} = (0.8, 0.6, 0.1), \quad \mathbf{queen} = (0.7, 0.7, 0.2), \quad \mathbf{apple} = (0.1, 0.2, 0.9)

Measure similarity with cosine similarity, cos⁡(u,v)=u⊤v/(∥u∥∥v∥)\cos(\mathbf{u}, \mathbf{v}) = \mathbf{u}^\top \mathbf{v} / (\|\mathbf{u}\| \|\mathbf{v}\|).

  1. Dot product king and queen: 0.8⋅0.7+0.6⋅0.7+0.1⋅0.2=0.56+0.42+0.02=1.000.8 \cdot 0.7 + 0.6 \cdot 0.7 + 0.1 \cdot 0.2 = 0.56 + 0.42 + 0.02 = 1.00.
  2. Norms: ∥king∥=0.64+0.36+0.01=1.01≈1.005\|\mathbf{king}\| = \sqrt{0.64 + 0.36 + 0.01} = \sqrt{1.01} \approx 1.005, and ∥queen∥=0.49+0.49+0.04=1.02≈1.010\|\mathbf{queen}\| = \sqrt{0.49 + 0.49 + 0.04} = \sqrt{1.02} \approx 1.010.
  3. Cosine of king and queen: 1.00/(1.005⋅1.010)≈0.9851.00 / (1.005 \cdot 1.010) \approx 0.985.
  4. Dot product king and apple: 0.08+0.12+0.09=0.290.08 + 0.12 + 0.09 = 0.29. Norm of apple: 0.01+0.04+0.81=0.86≈0.927\sqrt{0.01 + 0.04 + 0.81} = \sqrt{0.86} \approx 0.927.
  5. Cosine of king and apple: 0.29/(1.005⋅0.927)≈0.3110.29 / (1.005 \cdot 0.927) \approx 0.311.
  6. Interpretation: king and queen score 0.985 and king and apple score 0.311. The learned space puts related words near each other, and the distance is a usable signal for search, clustering or a downstream classifier, without anyone having written a rule about royalty or fruit.

Tradeoffs and failure modes

Representation learning is the default in most modern systems, but it has costs that the features-by-hand approach did not.

SituationProblemBetter choice
Very little labelled dataThe encoder overfits, or the head memorises noiseStart from a pretrained encoder and fine-tune, or use a linear probe on frozen features
Task is simple and well understoodA learned encoder is costly and opaqueHand-built features with a linear model
Need to explain each decisionLearned directions have no fixed meaningSimpler models, or probing and attribution tools after the fact
Representation collapsesAll inputs map to nearly the same vector, and the head still learns somethingContrastive negatives, a predictor head, or stop-gradient tricks in self-supervised training
Representation learns a shortcutIt uses background or texture instead of the objectMore varied data, augmentation, or checks on held-out groups
Representation does not transferIt is tuned so tightly to one task that nearby tasks failEvaluate with several probes, not only the training task

The main practical failure is treating a good training score as proof of a good representation. Freeze the encoder and run a linear probe on a different dataset. That test catches most of the problems in the table.

History

The idea that a network's hidden units should learn useful features is older than deep learning. Early connectionist work in the 1980s, including the popularisation of backpropagation by Rumelhart, Hinton and Williams in 1986, showed that hidden layers could learn internal codes for the task.

In the mid-2000s, researchers including Geoffrey Hinton and Yoshua Bengio reported that deep networks could be trained more effectively by pretraining one layer at a time on unlabelled data. This revived interest in learned representations as a separate goal. Bengio, Courville and Vincent's 2013 review gave the field a shared vocabulary and argued that good representations should disentangle the factors that explain the data.

What made the idea dominant was scale. Convolutional networks trained on large labelled image sets in the early 2010s produced features that transferred to many other vision tasks. Word2vec, published by Mikolov and colleagues in 2013, showed that simple prediction tasks on large text could give word vectors with useful geometry. Later, large self-supervised language and vision models made the pretrain-then-adapt pattern standard. Today most applied systems reuse a learned encoder rather than training one from scratch.

Check yourself

QuestionIn one sentence, what does a representation do for the classifier that sits on top of it?Show answer

It rewrites the input as a new vector in which the task is easier, so a simple head, often a linear one, can do the final job. The representation carries the difficulty, and the head only reads the answer off it.

QuestionA hidden layer maps the four XOR inputs to the points (0,0), (1,0), (1,0) and (2,1). Compute the output of the linear rule w = (1, -2), b = 0 for each point, and say whether the classifier is correct.Show answer

For (0,0)(0,0) the output is 00. For (1,0)(1,0) it is 11. For (2,1)(2,1) it is 2−2=02 - 2 = 0. The label for (1,0)(1,0) is 1, and the labels for (0,0)(0,0) and (2,1)(2,1) are both 0, so with the threshold 0.5 all three values match. The fourth input (0,1)(0,1) maps to (1,0)(1,0) as well, so it gets output 1, which is correct for XOR. The classifier is correct on every input.

QuestionYou have 200 labelled images and a large pretrained encoder. You want a classifier. Should you train the encoder from scratch, freeze it and fit a linear probe, or fine-tune it? Explain the choice.Show answer

Training from scratch on 200 images almost certainly overfits, because the encoder has far more parameters than the data can support. A linear probe on frozen features is the safest first step: it has few parameters and tests whether the pretrained representation already separates the classes. If the probe is not good enough, fine-tune with a small learning rate, and check against a validation split, since fine-tuning can destroy useful structure if it moves too far.

QuestionA self-supervised model reaches very low training loss, but every image in a held-out set maps to nearly the same vector. What went wrong, and what would you change?Show answer

The representation has collapsed. The objective can be minimised without the encoder keeping any information about the input, so it found a trivial solution. The fix is to add a mechanism that prevents this, such as negative examples in a contrastive loss, a predictor head with stop-gradient, or a variance regularisation term. After any change, check the spread of the representations on held-out data, not only the training loss.

Sources