Why DigiYatra cannot use an ordinary classifier
At a DigiYatra e-gate in an Indian airport, a traveller enrols once with a single selfie linked to their boarding pass, then walks up to a camera a few minutes later expecting the gate to open in under two seconds. Frame this as a machine learning problem the way you would frame any Grade 10 classification task and it breaks immediately. A standard softmax classifier needs a fixed set of output classes decided at training time, and it needs many labelled examples per class to place a stable decision boundary around each one. On any given day, the "classes" at a DigiYatra gate are the tens of thousands of individual travellers passing through that terminal, each represented by exactly one enrolment photo, and the exact set of people is different tomorrow. You cannot add a new output neuron and retrain a deep convolutional network every time someone checks in, and you certainly cannot do it in the two seconds between enrolment and boarding.
This is not a new problem invented by airports. In 1993, Yann LeCun and Jane Bromley at Bell Labs faced the identical structural issue while building a system to verify signatures on bank cheques: a bank might have five or six signature samples for each of millions of customers, far too few to train a per-customer classifier, and the set of customers keeps growing. Their solution, which is the founding idea of metric learning, was to stop trying to classify "whose signature is this" and instead learn a general-purpose function that answers a narrower, reusable question: "are these two things the same or different?" Once you can answer that question reliably for any pair of inputs — including a pair the network has never seen during training — enrolment becomes trivial: store one embedding per person, and verification becomes a single distance computation against that stored embedding.
From raw pixels to an embedding space
Formally, metric learning trains an embedding function f_θ : X → R^d, usually a convolutional or transformer network with parameters θ, that maps a raw input — a face crop, a signature image, a product photo — to a vector in a d-dimensional real space. Once the mapping exists, similarity between two raw inputs x1 and x2 is defined purely geometrically, using a fixed and simple distance function on the embeddings: usually Euclidean distance D(u, v) = ||u − v||₂ or cosine similarity cos(u, v) = (u·v) / (||u|| ||v||). The entire burden of "understanding" what makes two signatures belong to the same person, or two faces belong to the same traveller, is pushed into learning f_θ, so that after training, ordinary straight-line distance in R^d becomes a proxy for semantic sameness.
This is the crucial difference from a classification network trained with cross-entropy loss. Cross-entropy only ever asks the network to place a point on the correct side of a decision boundary among a fixed, known set of classes; it has no incentive to arrange the internal feature space so that distances between points are meaningful, and it offers no mechanism at all for handling a class it never saw during training. Metric learning explicitly optimises the geometry of the embedding space itself — points from the same identity should cluster tightly, points from different identities should be pushed apart — which is exactly the property needed to compare a brand-new enrolment photo against a brand-new probe photo, neither of which the network has ever encountered.
Contrastive loss and triplet loss
The earliest formulation, contrastive loss, works on pairs. Given two inputs mapped to embeddings u and v, a Euclidean distance d = ||u − v||₂, and a binary label y = 1 if the pair is a genuine match and y = 0 otherwise, the loss is:
L_contrastive = y · d² + (1 − y) · max(0, margin − d)²
Read this as two regimes. For a genuine pair, the loss is just d², so gradient descent keeps pulling the two embeddings together with no floor — the network is never satisfied that a genuine pair is "close enough." For an impostor pair, the loss is max(0, margin − d)²: once the two embeddings are already farther apart than margin, the loss is exactly zero and no gradient flows, so the network stops wasting capacity pushing already-separated impostors even farther apart.
The dominant formulation in modern deep metric learning, however, is triplet loss, because it encodes a relative comparison rather than an absolute distance threshold, which is a much easier and more stable target to learn. A triplet consists of an anchor a, a positive p from the same class as the anchor, and a negative n from a different class. Writing d(a, p) and d(a, n) for the Euclidean distances between the anchor's embedding and the other two, the loss is:
L_triplet = max(0, d(a, p) − d(a, n) + margin)
This is zero exactly when the negative is already farther from the anchor than the positive by at least margin, i.e. when d(a, n) ≥ d(a, p) + margin. Whenever that inequality fails, the loss is positive and its gradient simultaneously pulls p toward a and pushes n away from a, in a single backward pass.
Worked example: two signature triplets
Suppose a bank's signature-verification network has already been trained and produces 2-D embeddings for illustration (real systems use 128 or 512 dimensions; two dimensions here keeps the arithmetic and the diagram traceable by hand). Take an anchor signature a, a second genuine sample of the same signature p, and two candidate negatives — a forged signature that is obviously different, n_easy, and a skilled forgery that the network has not yet learned to separate well, n_hard:
a = (1.0, 2.0)
p = (1.3, 2.4)
n_easy = (3.0, 2.0)
n_hard = (1.4, 2.5)
By hand: d(a, p) has components (0.3, 0.4), so d(a, p) = √(0.3² + 0.4²) = √0.25 = 0.5. For the easy negative, the components are (2.0, 0.0), giving d(a, n_easy) = 2.0 exactly. For the hard negative, the components are (0.4, 0.5), giving d(a, n_hard) = √(0.16 + 0.25) = √0.41 ≈ 0.6403. With a margin of 1.0, the two triplet losses are max(0, 0.5 − 2.0 + 1.0) = max(0, −0.5) = 0 for the easy negative, and max(0, 0.5 − 0.6403 + 1.0) = max(0, 0.8597) ≈ 0.860 for the hard negative. The following code reproduces both computations:
import numpy as np
def euclidean(u, v):
return np.sqrt(np.sum((u - v) ** 2))
def triplet_loss(a, p, n, margin=1.0):
d_ap = euclidean(a, p)
d_an = euclidean(a, n)
loss = max(0.0, d_ap - d_an + margin)
return loss, d_ap, d_an
a = np.array([1.0, 2.0])
p = np.array([1.3, 2.4])
n_easy = np.array([3.0, 2.0])
n_hard = np.array([1.4, 2.5])
loss_easy, d_ap, d_an_easy = triplet_loss(a, p, n_easy)
loss_hard, _, d_an_hard = triplet_loss(a, p, n_hard)
print(f"d(a,p) = {d_ap:.3f}")
print(f"easy negative: d(a,n) = {d_an_easy:.3f}, loss = {loss_easy:.3f}")
print(f"hard negative: d(a,n) = {d_an_hard:.3f}, loss = {loss_hard:.3f}")
Tracing it line by line: euclidean squares and sums the two components then square-roots, matching the hand computation exactly. The printed output is:
d(a,p) = 0.500
easy negative: d(a,n) = 2.000, loss = 0.000
hard negative: d(a,n) = 0.640, loss = 0.860
The easy negative contributes nothing to training — its gradient is zero because the margin condition is already satisfied — while the hard negative is the one actually driving the network to improve. This distinction is the practical reason production metric-learning pipelines never sample triplets uniformly at random once training has progressed past the first few epochs: as the embedding space becomes reasonably well organised, a random negative is overwhelmingly likely to look like n_easy, contribute zero loss, and waste an entire forward-backward pass. Instead, systems perform hard-negative or semi-hard-negative mining inside each training batch — explicitly searching for negatives near the margin boundary, like n_hard, that still produce a useful gradient.
Diagram: shared-weight network and the resulting embedding geometry
The diagram below traces the full pipeline for this worked example: the same signature-verification network, with identical weights, processing all three images, followed by the embedding-space geometry that the triplet loss is judging. The dashed purple circle marks the margin boundary from the anchor — everything computed above.
Common misconception: "three networks trained together," not one shared network
A very common misreading of a triplet diagram like the one above is to assume the anchor, positive, and negative are each processed by a separate, independently trained sub-network, and that "metric learning" is the name for whatever combines their three outputs. This is wrong, and the error matters, because the entire generalisation guarantee of metric learning depends on it being false. In reality there is exactly one network, f_θ, with one set of parameters, applied three times in the same forward pass — once to the anchor, once to the positive, once to the negative. During backpropagation, the gradients computed from all three branches of the triplet loss flow back and accumulate into that single shared parameter set; nothing about the network's weights depends on whether an input happened to enter through the "anchor" slot or the "negative" slot in a particular training triplet.
Weight sharing is not an optimisation shortcut — it is what makes the resulting distance function a genuine, consistent metric. If the anchor and negative branches were separate networks, the same signature image could produce two different embeddings depending on which branch it passed through, and "distance between embeddings" would stop meaning anything at inference time, when there are no branches at all, just one enrolled embedding and one probe embedding compared directly. Because the weights are tied, any input — including one the network never saw during training, such as a brand-new DigiYatra traveller's face — is guaranteed to be mapped by the exact same function, so its embedding sits in the same learned geometric space as every training embedding and can be meaningfully compared against any of them.
Cosine similarity, L2 normalisation, and the classical Mahalanobis view
Production face- and signature-embedding systems (FaceNet, ArcFace, and their descendants) almost always L2-normalise embeddings onto the unit hypersphere before computing distance, and it is worth deriving why this makes Euclidean and cosine-based training nearly interchangeable. For unit vectors u and v with ||u|| = ||v|| = 1, expand the squared Euclidean distance:
||u − v||² = ||u||² + ||v||² − 2(u·v) = 1 + 1 − 2·cos(θ) = 2 − 2·cos(θ)
where θ is the angle between them. Since 2 − 2cos(θ) is a strictly decreasing function of cos(θ) over the relevant range, minimising squared Euclidean distance between normalised embeddings and maximising cosine similarity produce identical rankings of "which candidate is closest." This is why some systems train with a Euclidean-based triplet loss and deploy with cosine similarity at inference (or vice versa) without any inconsistency — on normalised vectors, they are the same ordering wearing different arithmetic.
It is also worth knowing that deep triplet networks are not the origin of metric learning; they are the nonlinear extension of an older, purely linear idea. Classical algorithms such as Large Margin Nearest Neighbour (LMNN) learn a positive semi-definite matrix M and define the Mahalanobis distance d_M(x, y) = √((x − y)ᵀ M (x − y)). Setting M to the identity matrix I collapses this exactly to ordinary Euclidean distance, since (x − y)ᵀ I (x − y) = (x − y)·(x − y) = ||x − y||². Because M is positive semi-definite it factors as M = LᵀL for some matrix L, so d_M(x, y) = ||L(x − y)|| — a Mahalanobis metric is precisely the Euclidean distance after applying a learned linear transform L to the raw features, stretching discriminative directions and compressing noisy ones. A deep triplet network is the same idea with L replaced by a nonlinear network f_θ: both approaches learn the coordinate transform first and leave the distance formula itself untouched.
Where the same mechanism shows up beyond faces and signatures
Myntra and Flipkart's "visually similar" product search embeds every product photo into the same space using a network trained with contrastive or triplet losses on pairs of images humans judged similar, then answers a query by finding nearby embeddings rather than running a classifier over a fixed catalogue of product categories. Plagiarism and code-similarity detectors embed documents or source files the same way, so that a submission can be checked against a corpus that keeps growing without retraining a classifier for each new document. In every one of these systems, once embeddings are produced, the actual search step is a nearest-neighbour query over millions or billions of points, and computing exact distances to all of them per query is far too slow. Production systems instead build approximate nearest-neighbour (ANN) index structures over the embedding space — graph-based structures such as HNSW, or tree/cluster-based structures such as those in FAISS — that exploit the fact that a well-trained metric places genuinely similar items near each other, so a graph walk that only explores a local neighbourhood finds the true nearest neighbours with high probability, in time roughly logarithmic in the size of the corpus rather than linear.
Active recall
Attempt each question before reading its answer.
- Why can't a standard softmax classifier be used for face verification at a DigiYatra gate where a new traveller enrols with only one selfie?
- Given anchor
a = (1.0, 2.0), positivep = (1.3, 2.4), and margin 1.0, what is the minimumd(a, n)required for a negativenso that the triplet loss equals zero? - Why does randomly sampling triplets become an inefficient training strategy as training progresses, and why did the "hard negative" in the worked example (loss 0.86) matter more than the "easy negative" (loss 0.00)?
- Derive the relationship between squared Euclidean distance and cosine similarity for two unit-norm embeddings, and explain what this implies about training with a Euclidean-based loss versus a cosine-based loss on normalised embeddings.
- A classmate says: "In a triplet network, the anchor, positive, and negative images each pass through a separately trained network." What is wrong with this statement, and why does the correction matter for generalisation to unseen identities?
- Classical metric learning learns a Mahalanobis matrix
Mwithd_M(x, y) = √((x − y)ᵀ M (x − y)). Show what happens whenM = I, and state in one sentence what learningMbeyondIbuys you that raw Euclidean distance cannot.
Worked answers
- A softmax classifier requires a fixed number of output classes decided at training time, each backed by enough labelled examples to fit a decision boundary. A new traveller is an unseen class represented by a single photo — there is no output neuron for them, and adding one would require retraining the whole network on every enrolment. Metric learning sidesteps this by training one embedding function on many identities so that Euclidean or cosine distance in the resulting space is meaningful for identities never seen during training; verification becomes thresholding the distance between the stored enrolment embedding and the probe embedding, with no retraining needed per traveller.
- The loss is zero exactly when
d(a,p) − d(a,n) + margin ≤ 0, i.e.d(a,n) ≥ d(a,p) + margin. Withd(a,p) = 0.5and margin1.0, this requiresd(a,n) ≥ 1.5. - Once the embedding space is reasonably well organised, most randomly chosen negatives are already comfortably outside the margin boundary — like
n_easyat distance 2.0, which sits well past the required 1.5 and yields loss 0.00 and zero gradient, wasting the training step. The hard negative,n_hard, sits inside the margin boundary at distance 0.64, producing loss 0.86 and a nonzero gradient that actually pulls the negative farther away and the positive closer. This is why real triplet-loss pipelines use hard- or semi-hard-negative mining inside each batch rather than sampling triplets uniformly at random. - For unit vectors,
||u − v||² = ||u||² + ||v||² − 2(u·v) = 1 + 1 − 2cos(θ) = 2 − 2cos(θ). Because this is a strictly decreasing function ofcos(θ), minimising squared Euclidean distance and maximising cosine similarity produce the identical ranking of candidates on L2-normalised embeddings. Consequently, a network trained with a Euclidean triplet loss on normalised embeddings can be deployed with cosine similarity at inference (or the reverse) without changing which candidates are judged closest — the two are the same ordering computed two different ways. - The statement is wrong: there is exactly one network with one set of weights, applied three times in the same forward pass (a weight-tied or Siamese architecture), not three independently trained networks. This matters because weight sharing guarantees that the same input always maps to the same point in embedding space regardless of which branch it is fed through, and gradients from all three branches accumulate into one shared parameter set. Without that tying, "distance between embeddings" would depend on which branch produced each embedding, and the network would have no consistent metric to apply to a brand-new pair of inputs at inference time, defeating the entire purpose of learning a reusable similarity function.
- Setting
M = Igives(x − y)ᵀ I (x − y) = (x − y)·(x − y) = ||x − y||², sod_M(x, y)reduces exactly to ordinary Euclidean distance. LearningM(equivalently a linear mapLwithM = LᵀL, sod_M(x,y) = ||L(x−y)||) lets the metric stretch discriminative feature directions and compress noisy or irrelevant ones, so that distance reflects learned semantic similarity rather than the arbitrary scale and correlation structure of the raw, untransformed coordinates.
Think About It
Think about this: How would you explain metric learning: learning similarity measures to a friend who has never seen a computer? What real-world analogy would you use? Imagine you had to build a system using these concepts — what would be your first step? Try this: before moving on, write down three things you learned and one question you still have.
Practice Exercises
Now it is time to practice! Complete these challenges to solidify your understanding:
- Exercise 1: Write a short program that demonstrates the core concept from this chapter. Test it with at least 3 different inputs.
- Exercise 2: Find a real-world example where metric learning: learning similarity measures is used in an Indian company (like TCS, Infosys, Flipkart, or ISRO). Write a paragraph explaining the connection.
- Exercise 3: Create a mind-map connecting metric learning: learning similarity measures to at least 3 other topics you have studied.
Key Takeaways — Summary and Recap
Let us recap what we covered: the core ideas behind metric learning: learning similarity measures, how they connect to real-world applications, and why they matter for your journey in computer science. Remember these key points as you move forward. For competitive exam preparation (CBSE, JEE, BITSAT), focus on understanding the WHY behind each concept, not just the WHAT.