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

Matching networks

Matching networks (Vinyals and colleagues, NeurIPS 2016) classify a query by attending over a small labelled support set: the prediction is the attention-weighted average of the support labels, with attention from cosine similarity of embeddings. Their key idea is episodic training, "train as you test": every training step is a small few-shot task built from the training classes, so the encoder is optimised for exactly how it will be used. The paper also introduced the miniImageNet benchmark. This page covers attention over supports, episodes, full-context embeddings and how well a plain classifier compares. In the exercises you implement matching attention, then train the same encoder episodically and as a standard 40-way classifier, testing one-shot transfer to 20 unseen classes. Episodic training reached 0.865 and the classifier embedding 0.845 (the same order on three seeds), a small edge.

Level 0: Foundation

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

Compare with every labelled example, weighted by similarity

Given a few labelled examples (the support set) and a new example (the query), a matching network measures how similar the query is to each support example. It turns those similarities into weights that sum to one and blends the support labels with those weights. Training repeats this on many small practice tasks, so the network gets good at exactly this kind of comparison. The demo shows the attention weights over six support examples.

Few-shot lab
Attention over 6 support examples (cosine similarities 0.9, 0.7, 0.2, -0.1, 0.5, -0.4): top weight 0.65. Matching networks predict the attention-weighted average of the support labels.
Why engineers care

When new categories appear with only a handful of examples (a new defect type, a new part, a new terrain class), a matching-style model can use them immediately without retraining.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A matching network predicts by…
  2. 2. The support set is…
  3. 3. Training uses…

Level 1: Beginner

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

By hand

  • Prediction: ŷ = Σ_k a(q, s_k) y_k with a(q, s_k) = softmax_k(c · cos(f(q), g(s_k))), where y_k are one-hot labels.
  • Cosine similarity: cos((1, 1), (1, 0)) = 1/√2 ≈ 0.707.
    • With similarities (0, 0), attention is (0.5, 0.5).
    • A large scale c makes attention nearly one-hot: nearest neighbour.
  • Episodes:
    • N-way K-shot with Q queries per class has N(K + Q) examples (30 for 5-way 1-shot with 5 queries);
    • 40 training classes give C(40, 5) = 658,008 possible 5-way class sets.
  • Full-context embeddings (optional): the support encoder g reads the whole support set (a bidirectional LSTM), and the query encoder f attends to it repeatedly.
  • Benchmarks: Omniglot (1623 characters × 20 drawings) and miniImageNet (100 classes × 600 images), introduced by this paper.
📝 Quick check (Level 1)
0/3 answered
  1. 1. cos((1, 1), (1, 0)):
  2. 2. Examples in a 5-way 1-shot episode with 5 queries per class:
  3. 3. A very large attention scale behaves like…

Level 2: Intermediate

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

Matching networks in code

Exercise 1.

  • Cosine attention matched the softmax of cosine similarities.
  • With scale 10, a query aligned with one support example got more than 99% of the weight.
  • Episodes drew 5 distinct classes.

Practice.

  • Few-shot libraries (learn2learn, torchmeta, easyfsl) provide episodic samplers and benchmarks.
  • Learn or tune the cosine scale (temperature), and evaluate with hundreds of test episodes, reporting 95% confidence intervals.
  • Always include a strong baseline: a classifier pre-trained on all training classes, with nearest-centroid or logistic regression on its features.
for episode in sampler:                                   # N-way K-shot tasks from training classes
    s, ys, q, yq = episode
    S = F.normalize(enc(s), dim=-1); Q = F.normalize(enc(q), dim=-1)
    attn = (scale * Q @ S.T).softmax(-1)                  # cosine attention over the support set
    probs = attn @ F.one_hot(ys, N).float()
    loss = F.nll_loss(probs.clamp_min(1e-9).log(), yq)
🐍 Exercise 1: matching attention
Implement cosine attention over a support set, the matching prediction and episode sampling.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Few-shot results should report…
  2. 2. A strong few-shot baseline:
  3. 3. The cosine scale acts as…

Level 3: Advanced

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

Theory

Train as you test.

  • Episodic training matches the training objective to the test procedure: the encoder is optimised for one-shot classification by attention, over many class combinations.
  • In principle this beats training for a fixed classification task and hoping the features transfer.

A non-parametric classifier.

  • The prediction is a kernel-weighted vote over the support set, like a soft k-nearest-neighbour classifier with a learned kernel.
  • New classes need no new parameters: they enter only through the support set.

The baseline debate.

  • Chen and colleagues (ICLR 2019) and Tian and colleagues (ECCV 2020) found that a network trained as a standard classifier on all training classes, then used as a fixed feature extractor, matches or beats many episodic methods.
  • A good embedding matters more than the episodic training procedure. Episodic training’s advantage, when present, is usually small.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Matching networks are like…
  2. 2. New classes require…
  3. 3. Tian et al. argued…

Level 4: Expert

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

Does episodic training beat a plain classifier?

History.

  • O. Vinyals, C. Blundell, T. Lillicrap, K. Kavukcuoglu and D. Wierstra, “Matching networks for one shot learning” (NeurIPS 2016).
  • W.-Y. Chen et al., “A closer look at few-shot classification” (ICLR 2019).
  • Y. Tian et al., “Rethinking few-shot image classification: a good embedding is all you need?” (ECCV 2020).

Exercise 2.

  • World and encoder: the same nuisance-dominated world as the Siamese page, and the same encoder (32 → 32 → 8).
  • Training, 1500 Adam steps each:
    • matching network: episodic 5-way 1-shot training with cosine attention (scale 10);
    • classifier: a standard 40-way softmax classifier, then its 8-D features used with cosine nearest neighbour.
  • Test: 300 episodes of 5-way one-shot classification on 20 unseen classes.
embedding one-shot accuracy (unseen classes)
raw observations (cosine) 0.381
40-way classifier features 0.845
matching network (episodic) 0.865
  • Both learned embeddings transfer well, more than doubling raw accuracy.
  • Episodic training has a small, consistent edge: 2 points here, with seeds 1 and 2 giving 0.883 vs 0.857 and 0.911 vs 0.905.
  • The plain classifier is a strong baseline, as Chen and Tian found on image benchmarks. Most of the gain comes from learning a good embedding at all, not from the episodic procedure.
🐍 Exercise 2 (research): does episodic training beat a plain classifier?
Train the same encoder episodically (matching network) and as a 40-way classifier, then compare one-shot accuracy on unseen classes.

Matching networks in AI, robotics and aerospace

  • Rapid adaptation: recognising new object or terrain classes for a robot from a few labelled views.
  • Inspection: adding a newly observed defect type with a handful of examples.
  • Design lesson: start with a well-trained embedding and nearest-centroid classification; add episodic training only if it measurably helps.

References

  1. O. Vinyals et al. (2016). “Matching networks for one shot learning.” NeurIPS.
  2. W.-Y. Chen, Y.-C. Liu, Z. Kira, Y.-C. F. Wang and J.-B. Huang (2019). “A closer look at few-shot classification.” ICLR.
  3. Y. Tian, Y. Wang, D. Krishnan, J. B. Tenenbaum and P. Isola (2020). “Rethinking few-shot image classification: a good embedding is all you need?” Proc. ECCV.