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.
★☆☆☆☆ 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. A matching network predicts by…
2. The support set is…
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.
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. cos((1, 1), (1, 0)):
2. Examples in a 5-way 1-shot episode with 5 queries per class:
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. Few-shot results should report…
2. A strong few-shot baseline:
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. Matching networks are like…
2. New classes require…
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
O. Vinyals et al. (2016). “Matching networks for one shot learning.” NeurIPS.
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.
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.
One-shot accuracy on unseen classes: episodic 0.865, plain classifier features 0.845, raw 0.381.
Practice
Show:
Level 0
Problem 1Level 0
Year matching networks appeared at NeurIPS?
2016.
Problem 2Level 0
Attention weights for two equally similar supports?
0.5 each.
Problem 3Level 0
Cosine similarity of a vector with itself?
1.
Level 1
Problem 4Level 1
cos((1, 1), (1, 0)) (4 decimals)?
0.7071.
Problem 5Level 1
Examples in a 5-way 1-shot episode with 5 queries per class?
30.
Problem 6Level 1
5-way class sets from 40 classes?
C(40, 5) = 658008.
Problem 7Level 1
Examples in a 20-way 5-shot episode with 15 queries per class?
20·20 = 400.
Level 2
Problem 8Level 2
Matching-network accuracy in exercise 2?
0.865.
Problem 9Level 2
Classifier-feature accuracy?
0.845.
Problem 10Level 2
Raw cosine accuracy?
0.381.
Problem 11Level 2
Accuracy points gained by episodic training over the classifier (0.865 − 0.845)?
2.
Problem 12Level 2
Images in Omniglot (1623 × 20)?
32460.
Problem 13Level 2
Cosine scale used in exercise 2?
10.
Level 3
Problem 14Level 3
Prove that matching attention tends to nearest neighbour as the scale grows.
Let s* maximise the cosine similarity, with margin δ > 0 over the others. Then a(q, s_k)/a(q, s*) = exp(c (cos_k − cos*)) ≤ exp(−cδ) → 0 as c → ∞, so all weight concentrates on s*, and the prediction becomes s*’s label. ∎
Problem 15Level 3
Explain why the plain classifier came so close.
To classify 40 classes, the classifier must learn features that separate classes and ignore nuisance variation, exactly what one-shot matching needs. Since new classes vary along the same latent factors, these features transfer almost as well as features trained episodically for the one-shot task itself. ∎
Problem 16Level 3
Compare matching networks with Siamese networks.
Siamese networks learn pairwise similarity with a contrastive objective and classify by nearest neighbour afterwards. Matching networks train the whole one-shot classification procedure end to end over episodes, with softmax attention across all supports, so the objective directly targets few-shot accuracy and considers all classes in the episode at once. ∎
Problem 17Level 3
Advise on recognising new defect types from five examples each.
Pre-train a strong encoder on all available defect data (as a classifier or with self-supervision), then classify new types by nearest centroid or cosine attention over the five examples. Try episodic fine-tuning only if it improves validation episodes. Report accuracy with confidence intervals, and keep a human in the loop for low-confidence cases. ∎
Level 4
Problem 18Level 4
Matching-network accuracy with seed 1?
0.883.
Problem 19Level 4
Classifier-feature accuracy with seed 2?
0.905.
Problem 20Level 4
Training steps per model?
1500.
Coding practice
🐍 Code 1 (Level 0): cosine
Cosine similarity.
🐍 Code 2 (Level 0): episode size
N(K + Q).
🐍 Code 3 (Level 0): class sets
C(n, N).
🐍 Code 4 (Level 0): attention
Softmax of scaled similarities.
🐍 Code 5 (Level 0): Omniglot
1623 × 20.
Topic test
A timed test drawn at random from this topic's question bank. Each attempt gets new questions. For a longer test across the whole chapter, see the chapter test, or mix topics in the question bag.
Matching networks: topic test
10 questions, drawn at random from a bank of 21. Every attempt is different.
⏱ Time limit: 15 minutes. The test submits itself when time runs out.
Levels covered: 0, 1, 2, 3, 4. No hints or solutions until you submit.