Triplet Loss is a loss function that uses sets of three examples (triplets) to learn embeddings in which "the distance between examples of the same kind" is shorter than "the distance to examples of a different kind" by a margin. It is used for tasks such as face recognition, where there are a huge number of classes and people unseen during training must also be distinguished.
A triplet consists of the following three examples.
- Anchor : the reference example (a face photo of some person)
- Positive : an example of the same kind as the anchor (another photo of the same person)
- Negative : an example of a different kind from the anchor (a photo of another person)
With denoting the embedding output by the network, Triplet Loss is as follows.
, and is the margin. As in the figure at the top, the goal is a state in which the anchor–positive distance plus still falls short of the anchor–negative distance.
This loss was popularized by FaceNet, the face recognition system of Schroff et al. at Google (2015). FaceNet embeds face photos into 128-dimensional vectors and learns so that the distance between vectors directly reflects how similar the faces are. Once this space exists, deciding whether two faces are the same person is a distance threshold, identifying someone is finding the closest enrolled face, and grouping people is clustering by distance. The paper says it took the idea of this loss from metric learning for nearest-neighbor classification (classifying with the label of the closest example) by Weinberger et al. (2005).

