Foundations to Frontier
Tests
E. Deep Learning › E15 Metric Learning and Few-Shot Learning

Prototypical networks

Prototypical networks (Snell, Swersky and Zemel, NeurIPS 2017) are one of the simplest and most effective few-shot methods. Each class is represented by its prototype, the mean embedding of its few support examples, and a query is classified by a softmax over negative squared distances to the prototypes. Squared Euclidean distance makes this equivalent to a linear classifier on the embedding, and the mean is the optimal representative for this distance (a Bregman divergence). This page covers prototypes, episodic training, why averaging helps, and the link to nearest-centroid and linear classifiers. In the exercises you implement prototypes and their linear form, then compare prototype and nearest-neighbour classification on unseen classes with 1, 5 and 10 shots. At 1 shot the two are identical (0.857). With 5 and 10 shots, prototypes won: 0.928 vs 0.909 and 0.938 vs 0.915, the same on three seeds.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

One average point per class

A prototypical network summarises each class by a single point: the average of its few examples in embedding space. A new example gets the label of the closest prototype. Averaging smooths out the quirks of individual examples, so more examples per class make the prototype more reliable. The demo shows prototypes (squares) built from K support examples (rings) and how queries are classified.

Few-shot lab
1-shot prototypes (squares) from the support points (rings) classify 84% of 300 queries correctly. More shots give steadier prototypes.
Why engineers care

Prototypes are cheap, interpretable and easy to update: adding a new class or a new example just means recomputing one average. That suits on-device recognition and fast-changing inventories.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A class prototype is…
  2. 2. A query is labelled by…
  3. 3. More shots make prototypes…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Prototype: c_k = (1/|S_k|) Σ_(x ∈ S_k) f(x). The points (0, 0), (2, 0), (1, 3) give (1, 1).
  • Classifier: p(y = k | q) = softmax_k(−‖f(q) − c_k‖²).
    • Squared distance from (1, 1) to (4, 5) is 25.
  • Linear form: −‖x − c‖² = 2cᵀx − ‖c‖² − ‖x‖². The ‖x‖² term is shared by all classes, so this is a linear classifier with w_k = 2c_k and b_k = −‖c_k‖². For c = (1, 2), b = −5.
  • Noise reduction: averaging K examples cuts the prototype’s noise standard deviation by 1/√K, to 0.32 for 10 shots.
  • Episodes: 5-way 5-shot has 25 support examples. Snell and colleagues found training with more ways than testing (for example 20-way) helps.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Prototype of (0, 0), (2, 0), (1, 3):
  2. 2. Linear-form bias for c = (1, 2):
  3. 3. Noise of a 4-shot prototype relative to one example:

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Prototypical networks in code

Exercise 1.

  • Prototypes were the class means.
  • Prototype probabilities summed to 1.
  • The equivalent linear classifier gave the same decision and the same log-probabilities up to a constant.

Practice.

  • Train episodically with squared Euclidean distance, often with more ways than at test time.
  • Pre-training the encoder as a standard classifier, then fine-tuning with the prototypical loss, is a strong recipe.
  • Prototypes can be updated online as new examples arrive, which suits deployment.
emb_s, emb_q = enc(support), enc(query)                     # support: N classes x K shots
protos = emb_s.view(N, K, -1).mean(1)                       # class prototypes
logits = -torch.cdist(emb_q, protos) ** 2                   # negative squared distances
loss = F.cross_entropy(logits, query_labels)
🐍 Exercise 1: prototypes
Compute class prototypes, prototype probabilities and the equivalent linear classifier.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Snell et al. found which distance works better?
  2. 2. Training with more ways than testing…
  3. 3. Adding a new class requires…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why the mean.

  • For any Bregman divergence (squared Euclidean is one), the point minimising the average divergence to a set of points is their mean (Banerjee and colleagues, 2005).
  • Prototypes are therefore the optimal single representatives under the distance being used. Prototypical networks can also be read as mixture density estimation with one component per class.

Prototypes versus nearest neighbour.

  • With one shot, the prototype is the support example, so both rules agree.
  • With several shots, nearest neighbour lets one atypical support example capture nearby queries. The prototype averages it out: its noise falls like 1/√K, which helps whenever classes are roughly unimodal in embedding space.
  • Nearest neighbour can win when classes are multimodal (several clusters), where a single mean is a poor summary.

Linear classifier view. Since the decision rule is linear in f(q), a prototypical network is a linear classifier whose weights are set by the support set rather than learned by gradient descent, which is why it needs so few examples.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The mean minimises average divergence for…
  2. 2. Nearest neighbour can beat prototypes when classes are…
  3. 3. Prototype classification is…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Do prototypes beat nearest neighbour as shots increase?

History.

  • J. Snell, K. Swersky and R. Zemel, “Prototypical networks for few-shot learning” (NeurIPS 2017).
  • A. Banerjee, S. Merugu, I. Dhillon and J. Ghosh, “Clustering with Bregman divergences” (JMLR 2005).
  • Nearest-centroid classifiers go back much further in pattern recognition.

Exercise 2.

  • World and encoder: the same nuisance-dominated world (40 training, 20 unseen classes) and the same encoder (32 → 32 → 8).
  • Training: a prototypical network trained episodically (5-way 5-shot, squared Euclidean, 1500 steps).
  • Test: 5-way episodes on unseen classes with K = 1, 5 or 10 shots, labelling queries by the nearest prototype or the nearest single support example. Prototypes on raw observations are a baseline.
shots K prototype nearest neighbour raw prototype
1 0.857 0.857 0.368
5 0.928 0.909 0.528
10 0.938 0.915 0.581
  • At 1 shot the rules coincide, exactly as they must.
  • With more shots, averaging wins: prototypes beat nearest neighbour by about 2 points at 5 and 10 shots. Seeds 1 and 2 gave 0.945 vs 0.928 and 0.956 vs 0.939 at 5 shots.
  • More shots help both, prototypes more: from 0.857 to 0.938.
  • The embedding matters most: raw-feature prototypes improve with shots too (0.37 → 0.58) but stay far behind.
🐍 Exercise 2 (research): do prototypes beat nearest neighbour as shots increase?
Train a prototypical network and compare prototype and nearest-neighbour classification on unseen classes with 1, 5 and 10 shots.

Prototypical networks in AI, robotics and aerospace

  • On-device recognition: robots and drones register new objects by storing a prototype from a few views.
  • Inventory and inspection: new part or defect classes added by averaging a handful of examples.
  • Design lesson: with several examples per class and unimodal classes, average them; with multimodal classes, keep several prototypes per class.

References

  1. J. Snell, K. Swersky and R. Zemel (2017). “Prototypical networks for few-shot learning.” NeurIPS.
  2. A. Banerjee, S. Merugu, I. S. Dhillon and J. Ghosh (2005). “Clustering with Bregman divergences.” JMLR 6.
  3. W.-Y. Chen et al. (2019). “A closer look at few-shot classification.” ICLR.