Course contentsShow
← Machine Learning and Deep Learning
Lesson 622 of 3,53815. Neural Network FoundationsFree lesson

Contrastive and Triplet Losses

Learning metric learning losses that pull similar examples together and push dissimilar examples apart in embedding space.

Contrastive and Triplet Losses

What You'll Learn

You'll learn how specialized loss functions can train neural networks to create meaningful embedding spaces where similar items cluster together and dissimilar items stay apart.

The Core Idea

Unlike classification losses (cross-entropy) or regression losses (MSE) that predict specific labels or values, contrastive and triplet losses teach networks to measure similarity. They create embedding spaces—high-dimensional representations where the distance between points reflects how similar the original inputs are.

Think of organizing books in a library: instead of assigning each book to a single category, you create a map where similar books naturally end up near each other—mysteries cluster together, cookbooks group up, and they're far from each other.

Contrastive Loss

Contrastive loss works with pairs of examples:

  • For similar pairs (same person's faces, matching items), it pulls their embeddings closer together
  • For dissimilar pairs, it pushes them apart by a margin

The loss penalizes the network when similar items are too far apart OR when dissimilar items are too close.

Triplet Loss

Triplet loss operates on three examples at once:

  • An anchor (reference point)
  • A positive example (similar to anchor)
  • A negative example (different from anchor)

The goal: ensure the anchor-positive distance is smaller than the anchor-negative distance by at least some margin. It's like saying "this cat photo should be closer to other cat photos than to any dog photo."

Why This Matters

These losses excel when you need:

  • Face recognition (match face to identity)
  • Image similarity search
  • Learning representations without fixed classes
  • Few-shot learning scenarios

Key Takeaway: Contrastive and triplet losses train networks to learn meaningful distances in embedding space, making them powerful for similarity-based tasks where traditional classification doesn't fit.