Skip to content
Deep Dive

Contrastive Loss Explained: The Math That Teaches Networks to Compare Faces

Contrastive loss is a training rule that pulls embeddings of the same person together and pushes different people apart until they are at least a set margin away. Triplet loss does the same with three images at once: an anchor, a match and a non-match. Most modern face recognition, including Ollie's model, uses margin-based losses such as CosFace or ArcFace instead.

Liam BradleySeptember 11, 2026 · 5 min read
Chart of error falling over training steps for different learning rates
Image: Kirlf, CC BY-SA 4.0, via Wikimedia Commons (resized)

The Training Problem: Teaching Similarity

Training a neural network requires a loss function, a mathematical measure of how wrong the network's output is for each training example. The network adjusts its parameters to reduce this loss, gradually learning to produce outputs that correctly reflect the target relationship. For face recognition, the target relationship is: embeddings of the same person should be close together, and embeddings of different people should be far apart.

A plain classification loss, which only asks the network to pick the right name from a fixed list, doesn't guarantee this. It can label the training faces correctly while leaving the embedding space loosely organised. There are two ways to fix that: pairwise losses such as contrastive and triplet loss, covered below, and margin-based classification losses such as CosFace and ArcFace, covered at the end.

How Contrastive Loss Works

The contrastive loss function operates on pairs. For a pair of embeddings (A, B) and a binary label (1 = same identity, 0 = different identity), the function computes the Euclidean distance D between the embeddings. For same-identity pairs (label = 1), the loss increases as D increases: the pair is penalised proportionally to D². For different-identity pairs (label = 0), the loss is proportional to max(0, M − D)², where M is a margin hyperparameter. The pair is only penalised if D < M,if the different-identity embeddings are already pushed far enough apart, no further adjustment is needed.

The combined effect of training on many such pairs is that same-identity embeddings are pulled toward each other, and different-identity embeddings are pushed apart until they satisfy the margin constraint. The result is an embedding space with a clear structure: tight clusters per identity, with wide margins between clusters.

Triplet Loss: An Important Alternative

Triplet loss extends the pairwise idea by training on triples: an anchor face A, a positive example P (same identity as A), and a negative example N (different identity). The loss encourages the anchor-positive distance to be smaller than the anchor-negative distance by at least a margin: D(A,P) + margin < D(A,N). This formulation is often more informative per training step because it directly compares the same-identity and different-identity distances for the same anchor.

A critical component of effective triplet loss training is hard negative mining: specifically selecting negatives where the anchor-negative distance is smaller than expected, the most confusing pairs. Training predominantly on easy negatives (very different faces) provides little gradient signal. Hard negatives force the network to learn finer discriminations.

Modern Approaches

Contemporary face recognition systems often use ArcFace or CosFace loss functions, which add an angular margin to the classification objective applied in embedding space. These losses empirically produce better-structured embedding spaces than vanilla contrastive or triplet loss, particularly at large scale. They have become the standard approach for training production-grade face recognition models.

Ollie's network was trained with CosFace (scale 64, margin 0.40) on the MS1MV2 dataset of about 5.8 million photos of 85,742 people. The margin means the network must pick the right person by a clear angle, not just barely, which pulls each person's photos into a tight cluster and leaves wide gaps between people.

Frequently Asked Questions

What is contrastive loss in simple terms?

Contrastive loss is a training objective that pulls same-person face embeddings closer together and pushes different-person embeddings further apart, shaping the embedding space so similarity is preserved.

What is the margin in contrastive loss?

The margin M is a minimum distance that different-identity pairs must be separated by. Pairs already further apart than M contribute no loss; pairs closer than M are penalised and pushed apart.

What is the difference between contrastive loss and triplet loss?

Contrastive loss operates on pairs with a binary same/different label. Triplet loss operates on anchor-positive-negative triples, directly comparing same-identity and different-identity distances for the same anchor face.

Try it yourself

Find your celebrity lookalike

Upload a photo and see which celebrities you look most like. Free to try, and your photo is never stored.

Find my celebrity look alike

Related Articles