← Lessons

quiz vs the machine

Gold1400

Machine Learning

The Triplet Loss

Anchor, positive, negative — and a margin that enforces meaningful gaps.

5 min read · core · beat Gold to climb

Three inputs at once

The triplet loss trains an embedding using three items per step:

  • An anchor.
  • A positive that should be close to the anchor.
  • A negative that should be far from the anchor.

It wants the anchor to be closer to the positive than to the negative by at least a margin.

The margin matters

Without a margin the model could satisfy the loss by collapsing all vectors together. The margin forces a real gap: the negative must be farther than the positive plus the margin before the loss reaches zero. This produces well separated clusters.

Triplet mining

Most random triplets are already easy and give no gradient. Performance depends on mining informative triplets:

  • Hard negatives are negatives closer than the positive.
  • Semi hard negatives are farther than the positive but still inside the margin.

Choosing semi hard triplets is a popular stable strategy made famous by FaceNet.

Key idea

Triplet loss pushes the anchor closer to its positive than to its negative by a fixed margin, and mining hard or semi hard triplets is what gives it a strong training signal.

Check yourself

Answer to earn rating on the learn ladder.

1. What three items does a triplet loss use?

2. Why is a margin included in the triplet loss?

3. Why does triplet mining matter?