Few-Shot Learning
Classify a new category correctly after seeing only a handful of labeled examples of it, instead of the thousands an ordinary classifier needs per class.
Why Does This Exist?
A manufacturer wants to flag a brand-new type of surface defect on a production line the day it's first spotted, not three months later once enough examples have accumulated to train a normal classifier. An ordinary image classifier needs hundreds or thousands of labeled examples per class to learn reliably. On day one, there might be three photos of the new defect, taken by the line inspector who first noticed it.
Few-shot learning is built for exactly this gap: classify a new category correctly using only a handful — commonly 1 to 10 — labeled examples of it, rather than the volume an ordinary classifier demands.
Think of It Like This
Recognizing a new coworker's handwriting from a sticky note
Ask someone to identify who wrote an unsigned memo, and they'll usually need to have seen many samples of everyone's handwriting first, comparing letter shapes and slant carefully. But show that same person three sticky notes signed by a new coworker, and they can often pick that coworker's handwriting out of a stack almost immediately afterward — not because handwriting recognition suddenly got easy, but because a lifetime of comparing handwriting in general means three examples of something new is enough to form a working sense of it.
Few-shot learning depends on that same prior experience: a model that has already learned what generally distinguishes categories from each other, in some embedding space, needs far fewer examples of a genuinely new category to place it correctly.
How It Actually Works
The support set, the query set, and n-shot k-way
A few-shot task is described by two numbers: k-way is how many classes are being distinguished, and n-shot is how many labeled examples per class are given. A "5-way 3-shot" task means 5 classes, 3 labeled examples each — the support set — and the model has to classify new, unlabeled query examples from those same 5 classes.
Prototypes: the standard approach when an embedding already exists
Given an embedding function that maps any example to a vector — usually one that was pretrained, or meta-learned as covered in meta-learning — few-shot classification often reduces to something almost too simple to call a model: average each class's support examples in embedding space into one prototype vector, then classify a query by whichever prototype is nearest to it.
is the embedding function, is class 's prototype, and is the shot count. All the real work is in — the classification step itself is nothing more than a nearest-prototype lookup. This is why few-shot learning leans so heavily on meta-learning or transfer learning: the quality of the embedding, learned beforehand across many other tasks or a large pretraining corpus, is what determines whether three examples are actually enough.
Why more shots help less than it seems it should
Going from 1-shot to 5-shot generally improves accuracy noticeably, since averaging over more examples produces a more stable prototype. Going from 20-shot to 100-shot typically helps far less — once the prototype has roughly converged to the true class center in the embedding space, additional examples mostly reduce sampling noise that was already small. This is a direct consequence of averaging: the estimate's uncertainty shrinks proportionally to the square root of the sample count, so each additional example helps less than the last one did.
How in-context learning reframed the whole idea
Large language models introduced a version of this that needs no training step at all: put a handful of labeled examples directly into the prompt, and the model conditions its next prediction on them without any parameter update. In-context learning is few-shot learning performed entirely at inference time — the "adaptation" is reading the prompt, not a gradient step or an embedding lookup. It's the same problem this page covers, solved by a different mechanism specific to large pretrained transformers.
Show Me the Code
Prototype classification on a 5-way, 3-shot task, compared against chance.
import numpy as np
rng = np.random.default_rng(3)n_classes, k_shot, dim = 5, 3, 8class_means = rng.normal(0, 1.2, size=(n_classes, dim))
support_x = np.vstack([rng.normal(class_means[c], 1.0, (k_shot, dim)) for c in range(n_classes)])support_y = np.repeat(np.arange(n_classes), k_shot)prototypes = np.array([support_x[support_y == c].mean(axis=0) for c in range(n_classes)])
n_query = 200query_x = np.vstack([rng.normal(class_means[c], 1.0, (n_query // n_classes, dim)) for c in range(n_classes)])query_y = np.repeat(np.arange(n_classes), n_query // n_classes)
dists = ((query_x[:, None, :] - prototypes[None, :, :]) ** 2).sum(axis=2)acc = float((dists.argmin(axis=1) == query_y).mean())print(f"3-shot prototype accuracy over 5 classes: {acc:.3f} (random guess: {1/n_classes:.3f})")# -> 3-shot prototype accuracy over 5 classes: 0.935 (random guess: 0.200)Three labeled examples per class reach 93.5% accuracy against a five-way random-guess floor of 20% — the embedding space, not the three examples themselves, is doing almost all of the work.
Watch Out For
Judging an embedding's few-shot quality from a single random support set
Which three examples happen to land in the support set affects the prototype's position, and a single unlucky draw of support examples can make an otherwise-good embedding look weak. Standard few-shot evaluation averages accuracy over many randomly sampled support sets — for the same reason a single small experiment shouldn't be trusted for any noisy estimate.
Assuming few-shot performance transfers to classes far outside the pretraining distribution
An embedding trained on natural photographs will generally support few-shot classification well for other natural photograph categories, and will typically do far worse on, say, medical scans or satellite imagery, categories structurally different from anything the embedding ever saw. Check whether the target domain resembles the embedding's original training distribution before expecting strong few-shot results.
The Quick Version
- Few-shot learning classifies a new category from a handful of labeled examples — commonly 1 to 10 — rather than the volume an ordinary classifier needs.
- Prototypical networks average each class's support examples into one prototype vector in embedding space, then classify by nearest prototype.
- The embedding function, usually pretrained or meta-learned beforehand, does nearly all the real work; the classification step itself is just a distance comparison.
- Additional shots help less and less past a certain point, since averaging converges the prototype toward the true class center.
- In-context learning is the same underlying problem solved differently: a large language model conditions on labeled examples placed directly in the prompt, with no training step at all.
What to Read Next
- Meta-Learning is the standard way to train the embedding function this page's prototypes rely on.
- Transfer Learning is the simpler alternative source for that embedding, borrowing one trained for a different purpose entirely.
- Semi-Supervised Learning tackles a related label-scarcity problem from a different angle: using unlabeled data instead of a strong pretrained embedding.
- Multi-Task Learning is another way an embedding can pick up the broad, transferable structure that makes few-shot classification work.