Relation networks (Sung and colleagues, CVPR 2018) learn the comparison itself. Instead of a fixed distance such as Euclidean or cosine, a small neural relation module takes a query embedding and a support embedding together and outputs a similarity score between 0 and 1. It is trained end to end with the encoder on few-shot episodes, regressing scores to 1 for matching pairs and 0 otherwise. In principle a learned metric can capture similarities that no fixed distance can. This page covers the relation module, pair scoring and the MSE objective, and when a learned metric helps. In the exercises you implement pair construction and the relation module, then compare it with a fixed squared-distance metric (same encoder, same 1-shot episodes) on unseen classes. The learned module brought no gain: 0.821 against 0.830, and 0.85 vs 0.90 and 0.87 vs 0.90 on two more seeds.
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.
Let a network decide what “similar” means
Prototypical and matching networks compare examples with a fixed ruler: distance or angle. A relation network instead learns the ruler. It feeds a pair of examples, a new one and a labelled one, into a small network that outputs how related they are, from 0 to 1. In principle this can capture notions of similarity that a simple distance would miss. Whether it actually helps is an empirical question. The demo compares support examples with attention; a relation network replaces those similarities with learned scores.
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
Some similarity judgements are not simple distances: two signals may “match” through a learned relationship (a fault signature under different operating conditions). Learned comparison modules can model this, but they need to earn their extra complexity.
📝 Quick check (Level 0)
0/3 answered
1. A relation network learns…
2. Its relation module takes…
3. Scores range…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.
By hand
Score: r(q, s_k) = g([f(q), f(s_k)]), a relation module g (an MLP or small CNN) on the concatenated embeddings, with a sigmoid output.
Pairs: every query is paired with every support (or class prototype): 25 queries × 5 supports = 125 pairs.
Objective:
MSE regression to 1 for matching pairs and 0 otherwise: scores (0.9, 0.2) against targets (1, 0) give (0.01 + 0.04)/2 = 0.025;
only 1/N of the pairs are positive (0.2 for 5-way).
Size: an MLP 2d → h → 1 with d = 8 and h = 16 has 2·8·16 + 16 + 16 + 1 = 289 parameters.
Prediction: the support class with the highest score.
📝 Quick check (Level 1)
0/3 answered
1. Pairs for 25 queries and 5 supports:
2. MSE of scores (0.9, 0.2) vs targets (1, 0):
3. Share of positive pairs in 5-way:
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.
Relation networks in code
Exercise 1.
Pairs were built query-major.
A zero-weight relation module gave scores of 0.5.
Targets marked the matching support for each query.
Practice.
The original model compares convolutional feature maps with a small CNN relation module. For vector embeddings, an MLP works.
Cross-entropy over the per-query scores often trains better than MSE.
Always compare against a fixed-metric baseline (prototypical network) with the same encoder and episodes.
Build query–support pairs, score them with a relation module and form the regression targets.
📝 Quick check (Level 2)
0/3 answered
1. The original relation module was…
2. An alternative to MSE:
3. A fair comparison uses…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.
Theory
Expressiveness.
A fixed metric imposes a geometry: classes must be separable by distance in embedding space.
A learned relation module can implement any (approximately) continuous similarity function of the pair, including asymmetric or non-metric ones.
In principle this is strictly more flexible, since a relation module can emulate Euclidean distance.
Why flexibility may not pay.
The relation module is trained on training classes only. If the encoder can already make classes Euclidean-separable, the extra module mainly adds parameters that must generalise to unseen classes, a new source of overfitting.
With little data, the simpler inductive bias of a fixed metric often generalises better. Later analyses found many few-shot gains came from better encoders and training, not from the comparison function.
When it helps. If the natural similarity in the data really is not distance-like (relations between parts, matches across modalities), a learned comparator can capture what a fixed metric cannot.
📝 Quick check (Level 3)
0/3 answered
1. A learned relation module can emulate…
2. Its risk on unseen classes:
3. Learned comparators help most when similarity is…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.
Does a learned metric beat a fixed one?
History.
F. Sung, Y. Yang, L. Zhang, T. Xiang, P. H. S. Torr and T. M. Hospedales, “Learning to compare: relation network for few-shot learning” (CVPR 2018).
Relation networks for reasoning were introduced by Santoro and colleagues (2017).
Exercise 2.
World and encoder: the same nuisance-dominated world (40 training, 20 unseen classes) and the same encoder (32 → 32 → 8).
Training: 5-way 1-shot episodes for 1500 steps, with either:
a fixed metric: softmax over negative squared distances;
a learned relation module: a 16 → 16 → 1 MLP on concatenated embeddings, trained with MSE.
Test: 300 one-shot episodes on unseen classes. The relation-network gradients were checked against finite differences (maximum error 7 × 10⁻¹¹), so the result is not a bug.
comparison
one-shot accuracy (unseen classes)
fixed squared distance
0.830
learned relation module
0.821
The learned metric brought no gain: it was slightly worse here and on seeds 1 and 2 (0.852 vs 0.897, 0.874 vs 0.901).
The reason: in this world, classes differ in a few latent factors that a good encoder makes distance-separable, so Euclidean distance is already the right comparison. The relation module adds parameters that must generalise to unseen classes.
This is a null result for flexibility, not a refutation: with genuinely non-metric similarity, the outcome could differ.
🐍 Exercise 2 (research): does a learned metric beat a fixed one?
Train the same encoder with a fixed squared-distance metric and with a learned relation module, and compare one-shot accuracy on unseen classes.
Relation networks in AI, robotics and aerospace
Cross-modal matching: comparing an image with a sensor signature or a description, where no shared distance is obvious.
Condition monitoring: learning when two signals indicate the same fault under different operating conditions.
Design lesson: a learned comparator must beat a fixed metric with the same encoder; otherwise prefer the simpler model.
References
F. Sung et al. (2018). “Learning to compare: relation network for few-shot learning.” Proc. CVPR.
A. Santoro et al. (2017). “A simple neural network module for relational reasoning.” NeurIPS.
W.-Y. Chen et al. (2019). “A closer look at few-shot classification.” ICLR.
A relation module with enough capacity can approximate any continuous function of the pair, including a decreasing function of Euclidean distance, so it is at least as expressive as a fixed metric.
With the same encoder and episodes, the learned module gave 0.821 against 0.830 for a fixed squared distance.
Practice
Show:
Level 0
Problem 1Level 0
Year relation networks for few-shot learning appeared at CVPR?
2018.
Problem 2Level 0
sigmoid(0)?
0.5.
Problem 3Level 0
Share of positive pairs in 5-way?
0.2.
Level 1
Problem 4Level 1
Pairs for 25 queries and 5 supports?
125.
Problem 5Level 1
MSE of scores (0.9, 0.2) against targets (1, 0)?
0.025.
Problem 6Level 1
Parameters of a relation MLP 16 → 16 → 1 (d = 8)?
289.
Problem 7Level 1
Input width of the relation module for 8-D embeddings?
16.
Level 2
Problem 8Level 2
Fixed-metric accuracy in exercise 2?
0.83.
Problem 9Level 2
Relation-module accuracy?
0.821.
Problem 10Level 2
Relation-module accuracy with seed 1?
0.852.
Problem 11Level 2
Fixed-metric accuracy with seed 1?
0.897.
Problem 12Level 2
Score of a pair with logit 2.2 (sigmoid, 1 decimal)?
σ(2.2) ≈ 0.9.
Problem 13Level 2
Training steps per model?
1500.
Level 3
Problem 14Level 3
Prove that a relation module can emulate a fixed-distance classifier.
The function h(a, b) = σ(−‖a − b‖² + β) is continuous in the concatenated input [a, b]. By the universal approximation theorem, an MLP with enough hidden units approximates it uniformly on a compact set. Its ranking of supports for a query then matches the squared-distance ranking, reproducing the fixed-metric classifier. ∎
Problem 15Level 3
Explain why the learned module did not help here.
The classes differ along a few latent factors that the encoder learns to expose, so squared distance in embedding space already separates them. The relation module adds 289 parameters, trained only on training classes, that must transfer to unseen classes; the extra flexibility brings estimation error without a modelling gain. ∎
Problem 16Level 3
Compare relation networks with prototypical networks.
Prototypical networks use a fixed squared distance to class means: few parameters, a linear classifier, strong inductive bias. Relation networks learn a comparison module: more flexible (non-metric, asymmetric similarity possible) but more parameters and more prone to overfitting with few training classes. ∎
Problem 17Level 3
Advise on matching acoustic fault signatures across different machine speeds.
Start with an encoder trained to be speed-invariant (augmentation over speeds) and a fixed metric. If matches remain poor because the relation between signatures depends on speed in a non-distance-like way, try a learned comparator conditioned on speed, comparing both on held-out machines and speeds. ∎
Level 4
Problem 18Level 4
Relation-module accuracy with seed 2?
0.874.
Problem 19Level 4
Fixed-metric accuracy with seed 2?
0.901.
Problem 20Level 4
Ways per episode in exercise 2?
5.
Coding practice
🐍 Code 1 (Level 0): pairs
queries × supports.
🐍 Code 2 (Level 0): MSE
Mean squared error.
🐍 Code 3 (Level 0): positive share
1/N.
🐍 Code 4 (Level 0): module size
2dh + h + h + 1.
🐍 Code 5 (Level 0): sigmoid
1/(1 + e^(−x)).
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.
Relation 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.