What is the difference between contrastive loss and triplet loss?

Quick Overview

Asks how contrastive loss and triplet loss differ for training embedding models. Tests understanding of pairwise versus relative distance constraints, margins, how each shapes the embedding space, and why triplet training depends on hard negative mining.

What is the difference between contrastive loss and triplet loss?

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

What is the difference between contrastive loss and triplet loss? Both losses train embedding models for tasks such as face verification and similarity search. Compare how each is defined, what it asks of distances in the embedding space, and how each is trained in practice. ```hint Fixed target or comparison Write each loss for a single training example. Check whether it pushes a distance toward a fixed value or only compares two distances with each other. ``` ### What a Strong Answer Covers - The training unit and formula for each loss - The kind of constraint each one places on distances, and what that does to the shape of classes in the embedding space - How each matches verification and retrieval goals - Sampling and mining, and why triplet loss depends on them more - How both relate to multi-negative losses such as InfoNCE ### Follow-up Questions - What is semi-hard negative mining, and why can training only on the hardest negatives hurt? - How would you choose the margin, and how does L2-normalizing the embeddings change that choice? - How does InfoNCE relate to triplet loss when it uses many negatives? - Triplet loss drops to near zero early in training, but retrieval quality is poor. What would you check?

Overview: Asks how contrastive loss and triplet loss differ for training embedding models. Tests understanding of pairwise versus relative distance constraints, margins, how each shapes the embedding space, and why triplet training depends on hard negative mining.

General logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

What is the difference between contrastive loss and triplet loss?

Both losses train embedding models for tasks such as face verification and similarity search. Compare how each is defined, what it asks of distances in the embedding space, and how each is trained in practice.

What a Strong Answer Covers Guidance

  • The training unit and formula for each loss
  • The kind of constraint each one places on distances, and what that does to the shape of classes in the embedding space
  • How each matches verification and retrieval goals
  • Sampling and mining, and why triplet loss depends on them more
  • How both relate to multi-negative losses such as InfoNCE

Follow-up Questions Guidance

  • What is semi-hard negative mining, and why can training only on the hardest negatives hurt?
  • How would you choose the margin, and how does L2-normalizing the embeddings change that choice?
  • How does InfoNCE relate to triplet loss when it uses many negatives?
  • Triplet loss drops to near zero early in training, but retrieval quality is poor. What would you check?