Triplet Loss
Triplet Loss: Triplet loss is a metric-learning objective for training embeddings. It compares an anchor with a positive sample from the same class and a negative sample from a different class. The objective pulls the anchor and positive together while pushing the anchor and negative apart.
In italiano: Triplet LossLegge III · Come una rete impara dai datiThe objective pulls the anchor and positive together while pushing the anchor and negative apart. The margin α requires the negative distance to exceed the positive distance by a fixed amount; once that condition is met, the triplet stops contributing to the loss. Hard negative mining selects negatives that are still close to the anchor, making training informative but potentially less stable.
Formula
L = max(0, ‖a − p‖₂² − ‖a − n‖₂² + α)
Numerical example
import numpy as np
a = np.array([0.0, 1.0])
p = np.array([0.2, 1.0])
n = np.array([2.0, 2.0])
alpha = 0.2
d_ap = np.sum((a - p) ** 2) # 0.04
d_an = np.sum((a - n) ** 2) # 5.00
loss = max(0.0, d_ap - d_an + alpha) # 0.00When to use it
Use triplet loss for face verification, image retrieval and other tasks where semantic distance between embeddings matters more than direct class scores.
Limitations
Random triplets often become uninformative quickly, while aggressive hard-negative mining can amplify mislabeled samples and destabilize training. Pair selection and batch composition are therefore part of the objective design.
Examples
- Face recognition (FaceNet)
- Image retrieval
- Metric learning