Search

Search papers and lessons

Few-Shot Learning

Level 3 · 11 min read

Few-shot learning classifies new categories from only a handful of labelled examples each, by reusing what a model learned from many earlier tasks.

On this page

  • Colour = topic mix
  • Discrete math
  • Probability
  • Learning basics
  • Calculus
  • Losses & regularization
  • Abstract algebra
  • ReadReady to readNot yet

TL;DR

Few-shot learning is about teaching a classifier a new category from only a few labelled examples, often one to five per class. A standard supervised model needs thousands of examples per class and overfits badly when given a handful. Few-shot methods get around this by training, in advance, on many small tasks of the same kind, so the model learns a representation that makes new classes easy to separate from a few examples.

Builds on: Supervised Learning, Overfitting · Leads to: Meta-Learning, Episodic Training, Prototypical Networks, MAML

Where it's used

Recognising rare categories. A medical imaging team wants a model to spot a rare skin condition, but has only four confirmed photographs of it. Collecting thousands is impossible. A few-shot model trained on common conditions can use those four photographs as its examples for the rare one, and the team then checks its predictions with a dermatologist.

Personalisation on a phone. A keyboard app wants to recognise a new user's handwriting or a new spoken command after the user has taught it three or four times. The model has to adapt from those few examples, on the device, without a large retraining run.

Benchmarks for learning to learn. Researchers test whether a model can learn new things quickly by using character datasets such as Omniglot, where each character has only a few drawings, and by making the classifier name characters it never saw in training. These benchmarks are the standard way of comparing few-shot methods.

How it works

We use a tiny toy problem. Pretend a model has already turned each example into a point in a plane, so each example is just two numbers. We want to classify new points into two classes, cat and dog, from two labelled points each. This is called a 2-way 2-shot task.

  1. Build the support set. The support set is the labelled examples we are given. Cat: (1, 1) and (3, 1). Dog: (5, 5) and (5, 7). Check: each class has exactly two examples, the K = 2 in the name.
  2. Make one prototype per class. A prototype is the average of a class's support points. Cat: ((1 + 3) / 2, (1 + 1) / 2) = (2, 1). Dog: ((5 + 5) / 2, (5 + 7) / 2) = (5, 6). Check: each prototype lies between its class's points.
  3. Take the query. The query is a new point with no label: (2, 2). Check: the query is not in the support set, so the model has never seen it labelled.
  4. Measure the distance to each prototype. The straight-line distance from (2, 2) to the cat prototype (2, 1) is 1, since only the second coordinate differs by 1. The distance to the dog prototype (5, 6) is 5, because 3² + 4² = 25 and the square root of 25 is 5. Check: 1 is much smaller than 5.
  5. Predict the nearest class. The query is closer to the cat prototype, so the prediction is cat. Check: the answer is one of the two class names.

The model did not learn cats or dogs in this step. It only compared the query with the prototypes. The skill that makes this work was learned earlier, from many other tasks, and it lives in the function that turns raw inputs into points. The next section writes the steps in symbols.

flowchart LR
  S["Support set: K labelled examples per class"] --> P["Average each class into a prototype"]
  Q["Unlabelled query"] --> E["Embed the query as a point"]
  P --> D["Distance from query to each prototype"]
  E --> D
  D --> A["Predict the nearest class"]

The math

Definition: N-way K-shot task
A task with NN classes and KK labelled examples per class. The support set is S={(xn,k,n):n=1,…,N,  k=1,…,K}S = \{(\mathbf{x}_{n,k}, n) : n = 1, \dots, N,\; k = 1, \dots, K\}, where xn,k\mathbf{x}_{n,k} is the kk-th example of class nn. A query x\mathbf{x} is an unlabelled example that must be assigned one of the NN labels.

Here x\mathbf{x} is an input such as an image, nn indexes the classes, and kk indexes the examples within a class. The learned function fϕf_\phi maps an input to a point in a space of fixed size, with parameters ϕ\phi.

Step 2 in symbols: the prototypes

Each class gets the mean of its embedded support examples:

cn=1K∑k=1Kfϕ(xn,k).\mathbf{c}_n = \frac{1}{K} \sum_{k=1}^{K} f_\phi(\mathbf{x}_{n,k}).

In the toy problem, with fϕf_\phi equal to the identity, ccat=12((1,1)+(3,1))=(2,1)\mathbf{c}_{\text{cat}} = \frac{1}{2}\big((1, 1) + (3, 1)\big) = (2, 1).

Steps 3 and 4 in symbols: the distances

The squared Euclidean distance between the embedded query and a prototype is

d(fϕ(x),cn)=∥fϕ(x)−cn∥2.d\big(f_\phi(\mathbf{x}), \mathbf{c}_n\big) = \big\lVert f_\phi(\mathbf{x}) - \mathbf{c}_n \big\rVert^2.

For the query (2,2)(2, 2): dd to the cat prototype is (2−2)2+(2−1)2=1(2-2)^2 + (2-1)^2 = 1, and dd to the dog prototype (5,6)(5, 6) is (2−5)2+(2−6)2=9+16=25(2-5)^2 + (2-6)^2 = 9 + 16 = 25. Squared distances are used so that the numbers stay whole; the ranking of classes is the same as with plain distances.

Step 5 in symbols: the prediction

The probability that the query belongs to class nn is a Softmax over the negative distances:

p(y=n∣x)=exp⁡ ⁣(−d(fϕ(x),cn))∑m=1Nexp⁡ ⁣(−d(fϕ(x),cm)).p(y = n \mid \mathbf{x}) = \frac{\exp\!\big(-d(f_\phi(\mathbf{x}), \mathbf{c}_n)\big)}{\sum_{m=1}^{N} \exp\!\big(-d(f_\phi(\mathbf{x}), \mathbf{c}_m)\big)}.

Here exp⁡\exp is the exponential function and the sum in the denominator runs over all NN classes, so the probabilities add to one. The prediction is the class with the largest probability, which is the nearest prototype. Training uses the loss −log⁡p(y=n∣x)-\log p(y = n \mid \mathbf{x}) for the true class nn, averaged over many queries, as in cross-entropy.

Worked examples

Example
Example 1: the 2-way 2-shot cat and dog task
  1. Support set: cat at (1, 1) and (3, 1); dog at (5, 5) and (5, 7).
  2. Cat prototype: average of the two cat points, which is (2, 1).
  3. Dog prototype: average of the two dog points, which is (5, 6).
  4. Query: (2, 2). Distance to the cat prototype: (2−2)2+(2−1)2=1(2-2)^2 + (2-1)^2 = 1.
  5. Distance to the dog prototype: (2−5)2+(2−6)2=9+16=25(2-5)^2 + (2-6)^2 = 9 + 16 = 25.
  6. The cat distance is smaller, so the query is predicted as cat.

Interpretation: the query sits close to the cat examples, and far from the dog examples, so the nearest-prototype rule makes the obvious call.

Example
Example 2: a 3-way 1-shot task on a line

Each example is one number. The support examples are class A at 0, class B at 4 and class C at 10, so K=1K = 1 and N=3N = 3, and each prototype is just its single example. The query is 6.

  1. Distance from 6 to A: ∣6−0∣=6|6 - 0| = 6.
  2. Distance from 6 to B: ∣6−4∣=2|6 - 4| = 2.
  3. Distance from 6 to C: ∣6−10∣=4|6 - 10| = 4.
  4. The smallest distance is 2, to class B. The prediction is B.
  5. Check with the squared distances used in the math: 36, 4 and 16. The smallest is still 4, for class B.

Interpretation: with one example per class, each prototype is a single point and the rule reduces to "pick the closest example". A query exactly halfway between two prototypes would be a tie, which the model must break somehow.

Example
Example 3: a realistic 5-way 1-shot episode

Suppose a model maps each image to a point in a plane, the way a trained network would. Five classes each have one labelled image, and the five support points are (0, 0), (3, 0), (0, 4), (4, 4) and (6, 1). A new image maps to the query (1, 3).

  1. Squared distance from the query to (0, 0): 12+32=101^2 + 3^2 = 10.
  2. Squared distance to (3, 0): (1−3)2+32=4+9=13(1-3)^2 + 3^2 = 4 + 9 = 13.
  3. Squared distance to (0, 4): 12+(3−4)2=1+1=21^2 + (3-4)^2 = 1 + 1 = 2.
  4. Squared distance to (4, 4): (1−4)2+(3−4)2=9+1=10(1-4)^2 + (3-4)^2 = 9 + 1 = 10.
  5. Squared distance to (6, 1): (1−6)2+(3−1)2=25+4=29(1-6)^2 + (3-1)^2 = 25 + 4 = 29.
  6. The smallest is 2, for the class whose support point is (0, 4). The prediction is that class.
  7. Softmax probability for that class: e−2/(e−10+e−13+e−2+e−10+e−29)e^{-2} / (e^{-10} + e^{-13} + e^{-2} + e^{-10} + e^{-29}). The dominant term is e−2e^{-2}, so the probability is close to one, about 0.999. Check: the other terms are much smaller than e−2e^{-2}.

Interpretation: in a real system the points come from a learned embedding of many dimensions, not a plane, but the arithmetic is the same. Try it: the support prototypes are (0, 0), (3, 0) and (0, 4), and the query is (2, 2). Which class is predicted, and what are the two squared distances that decide it? Answer: the squared distance to (0, 0) is 4 + 4 = 8, to (3, 0) it is 1 + 4 = 5, and to (0, 4) it is 4 + 4 = 8. The prediction is the class at (3, 0), with squared distance 5.

Common mistakes and tradeoffs

It is tempting to think that a few-shot model learns the new class from its few examples the way a person would. In fact the heavy lifting happened earlier: the model learned, across many training tasks, an embedding in which same-class points cluster together. A new class only adds a prototype. The few examples are used to place the prototype, not to learn the features from scratch.

It is tempting to train the model in the usual way, on all examples of all classes at once. In fact a few-shot model is usually trained on episodes that look like the test tasks: each episode samples NN classes and KK examples, and the loss is computed on queries. Training on episodes that match the test setup is what teaches the model to work from a handful of examples.

It is tempting to think that the one or two examples per class are enough to estimate a class mean well. With K=1K = 1, the prototype is one noisy example, and a single unusual example can move the prediction. This is the small-sample version of Overfitting, and it is why accuracy on few-shot benchmarks varies a lot from one random episode to another and is always reported as an average over many episodes.

Few-shot learning suits problems where new classes arrive with very few labels and where a related set of training tasks exists. It is the wrong tool when plenty of labelled data exists, because ordinary supervised training will do better. Alternatives include fine-tuning a pretrained model on the few examples, which is often strong but can overfit, and Zero-Shot Learning, which uses a written description of a class instead of any example.

History

Before the deep learning era, researchers asked whether a model could learn an object category from one image. In 2006, Fei-Fei, Fergus and Perona described learning new object categories from very few training images by building on categories learned earlier, and their work popularised the term one-shot learning.

In 2015, Lake and co-authors showed a Bayesian program-learning model that could learn new handwritten characters from a single example, and they built the Omniglot dataset to test this. In 2016, Vinyals and co-authors proposed matching networks, which trained directly on episodes with the test setup. In 2017, Snell, Swersky and Zemel proposed prototypical networks, the nearest-prototype rule used in this lesson, and Finn, Abbeel and Levine proposed MAML, which learns an initial set of weights that adapt to a new task after a few gradient steps. These papers made episodic training and the benchmark protocol standard.

Recap

  • A few-shot task has NN classes with KK labelled examples each, and a query to classify.
  • Each class is summarised by a prototype, the average of its embedded examples.
  • The query is assigned to the nearest prototype, with probabilities from a softmax over negative distances.
  • The embedding is learned in advance by training on many episodes that look like the test tasks.
  • Read next: Episodic Training for how the training episodes are built, Prototypical Networks for the method used here, Meta-Learning for the wider idea of learning to learn, and MAML for an alternative that adapts weights instead of comparing distances.

Check yourself

QuestionRecall: what are the support set and the query in an N-way K-shot task?Show answer
The support set is the N×KN \times K labelled examples, KK for each of the NN classes. The query is an unlabelled example that the model must assign to one of the NN classes.
QuestionComputation: a 2-way 2-shot task has cat examples (0, 0) and (2, 2), and dog examples (6, 0) and (8, 2). The query is (3, 1). Which class is predicted, using squared distances to the prototypes?Show answer
The cat prototype is (1, 1) and the dog prototype is (7, 1). Squared distance to cat: (3−1)2+(1−1)2=4(3-1)^2 + (1-1)^2 = 4. Squared distance to dog: (3−7)2+(1−1)2=16(3-7)^2 + (1-1)^2 = 16. The smaller distance is to cat, so the prediction is cat.
QuestionWhy: why does a few-shot model need training on episodes, rather than ordinary training on all examples?Show answer
Ordinary training teaches a model to separate the classes it sees, using many examples for each. A few-shot model has to make good decisions from KK examples of a class it has not seen. Episodic training repeatedly presents that situation during training, so the learned embedding is judged by how well it works with few examples, which is what the test tasks require.
QuestionApplied: a clinic wants to recognise a rare condition with five labelled photographs, and it has thousands of photographs of other conditions. Should it use few-shot learning, and what must it check?Show answer
Few-shot learning is a reasonable option, because the rare class has only five examples. The clinic should check that its training tasks resemble the target task, that performance is averaged over many random episodes rather than one lucky draw, and that predictions are reviewed by a specialist. It should also compare against fine-tuning a pretrained model on the five photographs.

Sources