Triplet Networks
Learns from triples of anchor, positive and negative examples.
The idea
For an anchor, one example of the same class (positive) and one of a different class (negative) are embedded. The triplet loss demands that the anchor is closer to the positive than to the negative by at least a margin. FaceNet used this for face embeddings.
L = max(0, d(a, p) − d(a, n) + margin)
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
- 1Pick an anchor, a positive and a negative.
- 2Embed all three with the shared network.
- 3Compute d(anchor, positive) and d(anchor, negative).
- 4Penalise when the positive is not closer than the negative by the margin.
Where it is used
Face recognition, image retrieval.
Watch out for
Most random triplets are already satisfied, so mining hard ones matters.