4. Ranking and Pairwise Losses
Ranking losses are used when the goal is to learn a relative ordering between items, rather than an absolute prediction. These losses are common in recommender systems, information retrieval, and metric learning. The most common ranking losses are the Pairwise Ranking Loss (also called the Contrastive Loss) and the Triplet Loss, which are designed to learn embeddings where similar items are close together and dissimilar items are far apart.
This figure illustrates the triplet loss mechanism, which is widely used in metric learning and face recognition. The anchor is a reference point (e.g., an image of a person). The positive is another image of the same person, and the negative is an image of a different person. The loss aims to make the distance between the anchor and the positive smaller than the distance between the anchor and the negative by at least a margin. This encourages the model to learn embeddings where similar items are close together and dissimilar items are far apart.
4.1 Contrastive Loss (Pairwise Ranking Loss)
The Contrastive Loss (also called Pairwise Ranking Loss) is used for learning embeddings where similar items are close together and dissimilar items are far apart. For a pair of inputs (x_i, x_j) with label y (1 if similar, 0 if dissimilar):
where is the embedding of sample i, and m is a margin parameter.
Mathematical properties: The contrastive loss is a simple and effective loss for metric learning. It is convex with respect to the embedding distances and has a well-defined gradient. The margin parameter m controls the separation between similar and dissimilar pairs.
Real-World Applications: Face verification, signature verification, and SimCLR self-supervised learning (contrastive loss is used to pretrain encoders on unlabeled data). SimCLR-pretrained YOLOv8 achieves higher mAP than its supervised counterpart (Chen et al., 2020).
4.2 Triplet Loss
The Triplet Loss is a widely used metric learning loss that considers triplets of samples: an anchor, a positive (same class as anchor), and a negative (different class from anchor):
where α is the margin parameter.
Mathematical properties: The triplet loss is one of the most effective losses for learning discriminative embeddings. It is used in FaceNet for face recognition. The triplet loss is more efficient than the contrastive loss because it uses triplets instead of pairs, capturing more relational information.
Real-World Applications: FaceNet face recognition (triplet loss is the core optimization objective of FaceNet), person re-identification (matching individuals across different camera views), and image retrieval (learning embeddings where similar images are close together) (Schroff et al., 2015).
4.3 Quadruplet Loss
The Quadruplet Loss extends the triplet loss by considering four samples: an anchor, a positive, a negative, and a second negative from a different class:
The quadruplet loss enforces that the anchor-positive distance is less than the anchor-negative distance and also less than the negative-negative distance.
Mathematical properties: The quadruplet loss is an extension of the triplet loss that provides additional constraints on the embedding space. It is used in applications where fine-grained class separation is required.
Real-World Applications: Person re-identification (provides more fine-grained class separation) and fine-grained classification (used when subtle differences between classes must be preserved).
4.4 N-Pair Loss
The N-Pair Loss extends the triplet loss by considering multiple negatives for a single anchor-positive pair. It is defined as:
where the embeddings are normalized to have unit norm.
Mathematical properties: The N-Pair loss is a generalization of the triplet loss that considers multiple negatives in a single loss term. This makes it more efficient than the triplet loss, as it can be computed in a single forward pass. The N-Pair loss has been shown to achieve state-of-the-art performance in metric learning tasks.
Real-World Applications: Image retrieval (state-of-the-art metric learning), contrastive learning (the InfoNCE loss used in SimCLR and MoCo is a variant of N-Pair loss), and face recognition.
Applications
- Face recognition (Triplet Loss).
- Image retrieval (Contrastive Loss, N-Pair Loss).
- Recommender systems (Pairwise Ranking Loss).
- Person re-identification (Quadruplet Loss).
Strengths and limitations
| Strengths | Limitations |
|---|---|
| Learn discriminative embeddings. | Require careful sampling of pairs/triplets. |
| Generalize well to unseen classes. | Computationally expensive. |
| Flexible and can be adapted to many tasks. | Sensitive to hyperparameter tuning. |
Table 6: Strengths and limitations of ranking losses. They learn highly discriminative embeddings and generalize well to unseen classes. However, they require careful sampling of pairs or triplets, are computationally intensive, and are sensitive to hyperparameter tuning (margin values, batch composition).