Matching Networks Few-Shot
Matching networks classify a new image by attention over a few labeled examples, weighting each support vote by similarity.
Why Does This Exist?
Fine-tuning on one example per class overfits before it learns. Yet a child shown one okapi photo recognises the next. Few-shot methods need to decide from the support set directly, with no gradient steps at test time.
Matching networks do exactly that with attention: embed the query and each support image, score cosine similarities, and vote support labels by softmax weights. The prototype alternative averages first in prototypical networks, the regime map is few-shot vision, and embeddings come from feature extraction.
Think of It Like This
Identifying a song from hummed snippets
You hum a tune and three friends each hum their best match back. You pick whose hum sits closest to yours, leaning mostly on the nearest and a little on the rest.
Supports are the friends, cosine similarity is closeness of humming, and attention weights are how much you trust each. The analogy stops at learning: friends do not retrain their ears nightly, while the encoder trains to make closeness meaningful.
How It Actually Works
An encoder embeds supports and query into one space, sometimes with a bidirectional LSTM that lets supports condition on each other. Cosine similarity between query and support passes through softmax into attention , and the prediction is the attention-weighted sum of support one-hot labels. Training samples random few-shot episodes so the encoder learns similarities that transfer to unseen classes.
Worked example
Three supports with cosine scores , , and against the query. Exponentiate: , , , summing to . Attention becomes , , . If supports carry classes cat, dog, cat, cat probability is against dog . One nearest neighbour dominates while the rest still vote.
Code
Attention vote from raw cosine scores.
import math
def match_vote(scores: list[float], labels: list[int]) -> tuple[list[float], float]: """Softmax attention and cat probability for binary labels 1=cat.""" exps = [math.exp(s) for s in scores] # softmax over similarities total = sum(exps) attn = [e / total for e in exps] cat = sum(a for a, lab in zip(attn, labels) if lab == 1) return attn, cat
print(match_vote([1.2, 0.3, -0.4], [1, 0, 1])) # -> ([0.62, 0.25, 0.13], 0.75)Watch Out For
Letting one support hog every vote
Sharp softmax plus a strong encoder makes the top support take always. The method then degrades to 1-nearest-neighbour with extra steps, brittle to one bad support.
Check attention entropy during eval. If it collapses, temper the softmax or regularise the encoder before blaming the data.
The Quick Version
- Matching networks vote support labels by attention over cosine similarities.
- No test-time gradients: the encoder plus softmax is the classifier.
- Worked numbers: scores , , give weights , , .
- Cat wins at from two supports against one dog.
- Watch attention entropy or the vote silently becomes 1-nearest-neighbour.