Siamese Networks
Two identical networks with shared weights that compare a pair of inputs.
The idea
Both inputs pass through the same network to produce embeddings, and a distance between them says how similar the inputs are. Training with a contrastive loss pulls matching pairs together and pushes non-matching pairs at least a margin apart. Because it learns similarity, it works for classes with only one example.
L = y·d² + (1 − y)·max(0, m − d)²
Watch it run
This is a real computation, not a drawing. Pause, step, change the settings, or try new random data.
- class 1
- class 2
- class 3
Items from three classes are scattered randomly in embedding space. We want the same class close together and different classes far apart.
- step:
- 0
- spread ratio:
- 0.968
Spread ratio = (average distance within a class) ÷ (average distance between classes). Lower is better.
Step by step
- 1Pass input A and input B through the same network.
- 2Compute the distance between the two embeddings.
- 3Apply a contrastive loss using the label (same or different).
- 4At test time compare a new input with stored examples.
Where it is used
Signature and face verification, one-shot recognition.
Watch out for
Needs many well-chosen pairs.