Skip to content
AI360Xpert
Beta

Matching Networks Few-Shot

Matching networks classify a new image by attention over a few labeled examples, weighting each support vote by similarity.

One query attends to three supports with weights 0.62, 0.25, and 0.13 and predicts the top support class
One query attends to three supports with weights 0.62, 0.25, and 0.13 and predicts the top support class

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 gg embeds supports and query into one space, sometimes with a bidirectional LSTM that lets supports condition on each other. Cosine similarity cc between query qq and support sis_i passes through softmax into attention aia_i, 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 1.21.2, 0.30.3, and −0.4-0.4 against the query. Exponentiate: 3.323.32, 1.351.35, 0.670.67, summing to 5.345.34. Attention becomes 0.620.62, 0.250.25, 0.130.13. If supports carry classes cat, dog, cat, cat probability is 0.62+0.13=0.750.62 + 0.13 = 0.75 against dog 0.250.25. 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 0.990.99 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 1.21.2, 0.30.3, −0.4-0.4 give weights 0.620.62, 0.250.25, 0.130.13.
  • Cat wins at 0.750.75 from two supports against one dog.
  • Watch attention entropy or the vote silently becomes 1-nearest-neighbour.