TL;DR
Matching Networks, from Vinyals and colleagues in 2016, classify a new example by comparing it with a small labelled set given at the same time. The network is not retrained for new classes, so a class seen once can be recognised. The authors also train the network on small tasks that look like the test situation, and this training is what makes one-shot accuracy high.
Builds on: Attention Mechanism, Cosine Similarity, Episodic Training, Long Short-Term Memory · Leads to: Prototypical Networks for Few-shot Learning, Few-Shot Learning
Why it mattered
Most deep learning systems learn a fixed list of classes and must be retrained to add one. This paper offered a trainable recipe for the opposite: the labelled examples are inputs to the network, and the network's job is to match a query against them. The episodic framing it popularised is common in later few-shot work. The paper's own numbers show the effect: on Omniglot, 5-way one-shot accuracy reached 98.1 percent, against 80.0 percent for a strong baseline classifier.
The problem
A standard classifier maps an image to one of the classes it was trained on. Adding a class means collecting many examples and running gradient updates. The authors call this a parametric approach: each training example has to be slowly written into the weights. Non-parametric methods such as K-Nearest Neighbors avoid this, because a new example is added just by storing it, but their accuracy depends heavily on the distance measure.
The paper asks for both properties at once: quick absorption of a new labelled example, and strong generalisation from common examples. The task is N-way k-shot classification. A support set holds k labelled examples from each of N classes not used in training, and a batch of unlabelled queries must be assigned to those N classes. Chance is 1/N.
How it works
The model is an attention lookup over the support set. Here is one tiny instance, with two-dimensional vectors so the numbers can be checked by hand.
- Start with a support set of two labelled examples. Their embeddings are with label A, and with label B. The labels are one-hot vectors, so A is and B is .
- A query arrives. Its embedding is . The query points mostly toward the first support example, so it should look like class A.
- Compare the query with each support embedding using cosine similarity, which measures the angle between two vectors. The cosine with the first is 2 divided by the square root of 5, which is 0.894. The cosine with the second is 1 divided by the square root of 5, which is 0.447.
- Turn the two similarities into weights that add to 1, using a softmax. Exponentiate each: e to the 0.894 is 2.446, and e to the 0.447 is 1.564. Their sum is 4.010. The weights are 2.446 / 4.010 = 0.610 and 1.564 / 4.010 = 0.390.
- Output the weighted sum of the labels: 0.610 times (1, 0) plus 0.390 times (0, 1) gives (0.610, 0.390). The first entry is the probability of class A, so the prediction is A, which agrees with the check in step 2.
The paper calls the weighted sum a classifier, , built from the support set . Changing S changes the classifier with no change to the network's weights.
The figure shows how one support example's weight moves as its cosine similarity to the query changes, with the competitor fixed at cosine 0.
- weight on the example
The paper adds two training ideas on top of this lookup. The first is the episode: the network is trained on tasks shaped like the test, with a few classes and a few examples per class. The second is full context embeddings, where the support set itself changes how each embedding is computed. Both are formalised in the math section.
flowchart LR S[Support set with k examples per class] --> G[Embed each support example with g] Q[Query x hat] --> F[Embed the query with f] F --> C[Cosine similarity to each support embedding] G --> C C --> A[Softmax gives attention weights a] A --> Y[Weighted sum of one-hot labels] Y --> P[Predicted class is the largest entry]
Worked examples
- Support set: with label A, and with label B.
- Query: .
- Cosine with the first support: . Cosine with the second: . They are equal.
- Equal scores give equal weights: .
- Output: .
- The two probabilities tie, so the output does not pick a label. Equal similarity means no evidence either way.
- Support set: with label A, with label B, and with label C. This is 3-way 1-shot.
- Query: .
- Cosines: with it is . With it is . With it is .
- Exponentials: , , . Their sum is .
- Weights: , , . They add to 1.
- Output: . The largest entry is for C.
- Interpretation: the query is closest to , but the other two keep some weight. A rule that used only the single nearest neighbour would ignore them.
The math
Definition
A one-shot classifier of this kind takes a support set of labelled examples and a query , and returns a probability over labels. Each is a one-hot vector, so the output is a probability vector over the labels in .
Step 1 in symbols: the attention weights
Two embedding networks and map inputs to vectors. The cosine similarity between vectors and is . The attention weight for support example is
The weights are positive and add to 1 over . Cosine values lie between minus 1 and 1, so the largest weight can be at most times the smallest.
Step 2 in symbols: the prediction
The output is
Because each is one-hot, the entry of for a label is the sum of the attention weights on the support examples carrying . The predicted class is the label with the largest entry, written in the paper.
Step 3 in symbols: episodes and the training objective
A task is a distribution over label sets . An episode draws a label set , then draws a support set and a batch from . Training maximises the log probability of the batch labels, averaged over episodes:
Here holds the weights of and . The paper calls this a form of meta-learning: each episode teaches the network how to learn from a support set.
Step 4 in symbols: full context embeddings
Plain and embed each example alone. The paper lets the support set change the embeddings. The query embedding becomes , where is the number of reading steps. The appendix writes the reading steps as
Here is a read from the support set, fed back into the recurrent cell. The support embeddings are also made context-aware: is a bidirectional LSTM run over the support set, plus a skip connection .
Results
Omniglot (1,623 characters from 50 alphabets, 20 drawings each). Table 1 of the paper reports Matching Networks with cosine matching and no fine-tuning at 98.1 percent for 5-way 1-shot, 98.9 percent for 5-way 5-shot, 93.8 percent for 20-way 1-shot and 98.5 percent for 20-way 5-shot. The Baseline Classifier, which matches features from a classifier trained on other characters, scores 80.0, 95.0, 69.5 and 89.1 percent on the same four settings. The Convolutional Siamese Net scores 96.7 and 88.0 percent in the 5-way 1-shot and 20-way 1-shot settings.
miniImageNet (60,000 images, 100 classes, 84 by 84 pixels, 80 training and 20 test classes). Table 2 reports 5-way accuracy. Matching Networks with full context embeddings and fine-tuning reach 46.6 percent in 1-shot and 60.0 percent in 5-shot, against 38.4 and 51.2 percent for the softmax Baseline Classifier with fine-tuning. Full context embeddings help on this harder set, adding 3.0 points in 1-shot without fine-tuning.
Full ImageNet, rand split. Table 3 gives 5-way 1-shot accuracy of 93.2 percent for Matching Networks against 87.6 percent for the Inception Classifier. The abstract's figures for ImageNet (87.6 to 93.2 percent) and Omniglot (88.0 to 93.8 percent) are the 1-shot results compared with the competing approaches.
Full ImageNet, dogs split. Here the model is 1 point below the Inception baseline on the held-out dog classes (58.8 against 59.8 percent). The authors attribute this to training episodes that sample classes uniformly from the whole class tree, whereas the test set is fine-grained.
Language. On the Penn Treebank one-shot sentence task, chance is 20 percent. Matching Networks reach 32.4, 36.1 and 38.2 percent with support sets of 1, 2 and 3 examples per class. An LSTM language model trained on all words reaches 72.8 percent, but it has seen far more data, so it is an upper bound and not a fair rival.
Limits. The cost of each gradient update grows with the support set, which the conclusion names as the main drawback. Performance also drops when the label distribution is fine-grained, as on the dogs split. And the model needs a per-query computation over the support set at test time, so it is not free to run at scale. A standard classifier is simpler when the classes are fixed, large and known in advance.
Reading guide
Read the paper in this order, with the lessons it needs.
- Section 2.1 (model architecture) uses Attention Mechanism and the softmax in Softmax, with Cosine Similarity as the score. Equation 1 is the weighted sum of step 2 above.
- Section 2.1.2 (full context embeddings) uses Long Short-Term Memory for the attended read and the bidirectional encoder.
- Section 2.2 (training strategy) uses Episodic Training and the framing of Meta-Learning.
- Section 3 (related work) compares to Siamese Network models and to kernel-based nearest-neighbour methods, which equation 1 generalises.
- Section 4 (experiments) uses Convolutional Neural Network feature extractors and Batch Normalization in the Omniglot embedding, and Language Model ideas in the language task.
Read next: Prototypical Networks for Few-shot Learning, which simplifies the same episodic setting by comparing queries with class averages.