Triplet Margin Loss
Sequence Models & Generative Models DS practice problem on Onlearn.
Difficulty: medium.
Topics: Triplet Margin Loss, Euclidean Distance, Margin Hyperparameter, Hard Negative Mining, Anchor-Positive-Negative Triplet, Hinge Loss Formulation, Metric Learning, Optimization Theory, Representation Learning, Deep Learning Foundations, Information Theory, Distance Metrics, Loss Function Design, Embedding Space Geometry, Gradient-Based Learning, Supervised Feature Extraction.
Implement the triplet margin loss function used in metric learning. This loss is commonly used to train embedding networks (e.g., face recognition, image retrieval, sentence embeddings) where the goal is to learn representations such that similar items are close together and dissimilar items are far apart in the embedding space. Given three inputs: anchor : The reference embedding(s) positive : Embedding(s) from the same class as the anchor negative : Embedding(s) from a different class than the anchor margin : A float value that defines the minimum desired gap between positive and negative distances Write a function triplet margin loss(anchor, positive, negative, margin) that computes the mean triplet margin loss over all provided triplets. The function should: 1. Compute the Euclidean distance between anchor and positive embeddings 2. Compute the Euclidean distance between anchor and negative embeddings 3. Compute the per triplet loss incorporating the margin 4. Return the mean loss as a float Your function should handle both single triplets (1D arrays representing individual embeddings) and batches of triplets (2D arrays where each row is an embedding).