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.
