---
kind: concept
slug: metric-learning
title: Metric learning
summary: >-
  Train an embedding so that matching inputs end up close and non-matching ones far apart, using pair, triplet, N-pair or proxy-based losses over a distance or cosine score. Pair and triplet losses need only similar/dissimilar labels but need hard-negative mining and converge slowly. Proxy-based losses avoid pair mining and need a class label per sample, plus a proxy count per class.
tags: [metric-learning, embeddings, loss-function, representation-learning]
papers: ["2010.05113", "2004.13513", "2004.00440", "2004.07180", "1908.10084", "2201.09206", "2205.12446", "1904.03451", "2303.11851", "2011.13005", "1610.06136", "2012.01506", "2103.09096"]
related: [contrastive-learning, angular-margin-loss, face-recognition, knowledge-distillation, continual-learning, prototype-learning, vision-transformer, language-model-pretraining, zero-shot-learning, domain-adaptation, text-embedding, local-feature-matching, few-shot-learning, meta-learning, image-manipulation-detection, frequency-domain-learning]
status: draft
updated_at: 2026-11-01
---

## Overview

Metric learning trains an encoder so that a chosen distance (or similarity) ranks
related inputs above unrelated ones. The review describes the field as spanning
metric learning, NLP, vision, audio and reinforcement learning, without a common
framework, and places the older metric-learning losses at the root of the later
contrastive family [arxiv:2010.05113 §I] [arxiv:2010.05113 §IV]. The shared core
is a score $S(\mathbf q,\mathbf k)$ (distance, dot product, cosine or bilinear)
and a loss that pulls positive pairs together and pushes negatives apart
[arxiv:2010.05113 §III.A] [arxiv:2010.05113 §III.E.1].

The choices that separate the variants are (1) which comparisons enter the loss,
(2) how many per query, (3) whether classes are represented by samples or by
learned proxies, and (4) how the trained embedding is used at test time, for
example by nearest class mean [arxiv:2004.00440 §3.1].

Two constraints from the review hold across variants. First, the score matters:
the dot product is unbounded and scales with vector norms, so cosine or an
explicit norm penalty is the usual remedy [arxiv:2010.05113 §III.E.1]. Second,
using only positive pairs is not enough: minimising the positive distance alone
can be solved by a constant encoder, so negatives or an architectural constraint
must be present [arxiv:2010.05113 §III.E].

## Variants

### Pair loss (energy-based, Siamese)

Positive pairs are pulled to zero distance; negative pairs contribute only while
they lie inside a margin $m$ [arxiv:2010.05113 eq. 2]. The review credits
Chopra, Hadsell and LeCun for the pair loss and Bromley et al. for the Siamese
setting in metric learning [arxiv:2010.05113 §IV] [arxiv:2010.05113 tab. 1].
Note that eq. 2 places the margin on the squared distance, while the text calls
$m$ a radius; check which form an implementation uses [arxiv:2010.05113 §III.E.2].
Card: [Contrastive Representation Learning: A Framework and Review](/papers/2010.05113).

### Triplet loss

An anchor must be closer to its positive than to its negative by a margin
[arxiv:2010.05113 eq. 3]. It is the most common energy-based loss in the review's
tables: image retrieval, sentence embeddings, knowledge-graph embeddings (TransE)
and speech-to-text matching (CSTNet) all use it [arxiv:2010.05113 tab. 2]
[arxiv:2010.05113 tab. 3] [arxiv:2010.05113 tab. 5] [arxiv:2010.05113 tab. 6].
Each query sees one positive and one negative per step, so training converges
slowly and hard-negative mining is essential [arxiv:2010.05113 §III.E.2]. The
review cites Hermans et al. for the importance of mining hard samples
[arxiv:2010.05113 tab. 1].

### Citation-defined triplets with hard negatives (SPECTER)

Here relatedness comes from a citation graph, not from class labels: a paper is a
positive for a query when the query cites it [arxiv:2004.07180 §2.3]. Each query
gets up to five triples: two hard negatives, which are papers cited by a paper the
query cites but not cited by the query, and three random easy negatives
[arxiv:2004.07180 §2.4] [arxiv:2004.07180 §4]. The loss is a triplet margin loss on
L2 distance with margin 1 [arxiv:2004.07180 eq. 2]. The paper reports that cosine
distance did worse for this loss [arxiv:2004.07180 §2.3]. Only title and abstract
are needed at inference, so papers without citations can still be embedded
[arxiv:2004.07180 §2.5]. Dropping the hard negatives lowers the author-reported
12-metric average from 80.0 to 78.4 [arxiv:2004.07180 tab. 2]. In the paper's
comparison, fine-tuning SciBERT on each user-activity signal scored between 76.0
and 78.0 against 80.0 for the fixed embedding [arxiv:2004.07180 tab. 3]. Card:
[SPECTER: Document-level Representation Learning using Citation-informed
Transformers](/papers/2004.07180).

### Siamese pooled encoder with cosine score (Sentence-BERT)

One pretrained encoder (BERT or RoBERTa) is applied to each sentence separately.
Its token outputs are mean-pooled into one vector, and two vectors are compared
by cosine similarity, so each sentence is embedded once and the vectors can be
stored and searched [arxiv:1908.10084 §3] [arxiv:1908.10084 §6]. Three training
objectives are given: a three-way NLI softmax over the concatenation of the two
embeddings and their absolute difference, a mean-squared-error regression of the
cosine against a gold score, and a triplet hinge on Euclidean distance with margin
1 [arxiv:1908.10084 §3]. The softmax head is used only for training and is dropped
at inference [arxiv:1908.10084 §6]. On the STS benchmark dev set, mean pooling
beats CLS and MAX pooling (87.44 against 86.62 and 69.92) [arxiv:1908.10084 tab. 6].
The authors report that the closest pair among 10,000 sentences takes about 65 hours
with a cross-encoder and about 5 seconds with this setup, on their hardware
[arxiv:1908.10084 §1]. The paper reports SBERT-NLI-large at 76.55 average Spearman
over seven STS tasks with no STS training data [arxiv:1908.10084 tab. 1]. Card:
[Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks](/papers/1908.10084).

### Single-center loss with softmax (face forgery)

Real faces are pulled toward one learnable center, and fake faces must sit at
least a margin farther from it than the real faces do [arxiv:2103.09096 eq. 1]
[arxiv:2103.09096 §3.3]. The paper's reason for a one-sided rule is that
triplet or center losses would squeeze fakes from many manipulation methods
into one cluster, though these fakes differ from each other [arxiv:2103.09096
§1] [arxiv:2103.09096 §3.3]. The loss is added to softmax cross-entropy with
weight $\lambda$ [arxiv:2103.09096 eq. 6]. The margin is $m\sqrt{D}$, where $D$ is
the embedding dimension, so the final model's $m = 0.3$ gives about 9.49 at
$D = 1000$; this value is derived on the card and not stated in the paper
[arxiv:2103.09096 §4.2.1]. On FF++ c40, softmax alone reaches AUC 0.860 and
pAUC(0.1) 0.652, centre plus softmax 0.868 and 0.666, triplet plus softmax 0.863
and 0.655, and single-center plus softmax 0.916 and 0.790 [arxiv:2103.09096 tab. 1].
These are author-reported. The center's learning rate and the batch-level
handling are not stated [arxiv:2103.09096 §3.3] [arxiv:2103.09096 Pitfalls].
Card: [Frequency-aware Discriminative Feature Learning Supervised by
Single-Center Loss for Face Forgery Detection](/papers/2103.09096).

### Reconstruction error as the class score (few-shot)

Each class is scored by how well its support feature maps can rebuild the
query's feature map. The support features of one class, across all shots and
spatial positions, form one pool; a ridge regression rebuilds the query from
that pool, and the class score is the negative mean squared error over
locations [arxiv:2012.01506 §3.1] [arxiv:2012.01506 eq. 4]. The solve is closed
form, so no gradient step runs at test time [arxiv:2012.01506 §3.4]
[arxiv:2012.01506 §4.2]. Beyond the backbone, the learned parameters are a
penalty scale, a recalibration factor and the softmax temperature
[arxiv:2012.01506 §3.2]. The paper reports 83.16% (1-shot) and 92.59% (5-shot)
on cropped CUB with ResNet-12, against 80.80% and 91.19% for its best listed
baseline; these are author-reported [arxiv:2012.01506 tab. 3]. On mini-ImageNet
the 1-shot result is slightly below FEAT (66.45% against 66.78%) [arxiv:2012.01506
tab. 7]. Pre-training needs a separate head with one learnable map per base class,
which is discarded afterwards [arxiv:2012.01506 §3.6]. Card: [Few-Shot
Classification with Feature Map Reconstruction Networks](/papers/2012.01506).

### Softmax plus triplet embedding for tracking (POI)

The embedding is a 128-d appearance feature for re-identifying people across
frames. A GoogLeNet-like network is trained with a softmax identity loss, which
makes features discriminative, plus a triplet loss, which keeps same-identity
cosine distances small [arxiv:1610.06136 §2]. Features are compared by cosine
similarity, and the cosine is one factor in a product with motion and shape
affinities [arxiv:1610.06136 §3, eq. 1] [arxiv:1610.06136 §3, eq. 4]. The triplet
margin, the triplet sampling and the weight between the two losses are not stated
[arxiv:1610.06136 §2]. On MOT16 train, with the same detector and online tracker,
the paper's feature reaches MOTA 65.2 against 63.6 for a baseline labelled
"GoogLeNet feature" [arxiv:1610.06136 tab. 2]; the card notes that this label is
not confirmed by the text. These are author-reported. Card: [POI: Multiple Object
Tracking with High Performance Detection and Appearance Feature](/papers/1610.06136).

### Multi-pair losses (lifted structure, N-pair)

Lifted-structure and its generalisation use many positives and negatives per
query within a batch, which gives more comparisons per gradient step than a
triplet [arxiv:2010.05113 §III.E.2]. The N-pair loss is a non-parametric
classification over one positive and several negatives; the review writes Sohn's
multi-class N-pair loss in the same form as the NCE-global loss, with in-batch
samples as negatives [arxiv:2010.05113 §III.E.3] [arxiv:2010.05113 eq. 10].
The review's discussion says negative quality matters, hard negatives included, as
well as the number of negatives [arxiv:2010.05113 §VI.B].

### Circle loss on dense correspondences (PREDATOR)

Here the pairs are points of two 3D scans, and the positives are the ground-truth
correspondences under a known transform. The loss pulls each anchor's positives
within a radius $r_p$ of it and pushes its negatives beyond a radius $r_s$, with
a log-sum-exp over the pairs and a per-pair weight that grows with the distance
from its margin [arxiv:2011.13005 eq. 8] [arxiv:2011.13005 §3.5]. The margins are
$\Delta_p = 0.1$ and $\Delta_n = 1.4$, and the scale $\gamma$ is 24 for 3DMatch,
64 for ModelNet40 and 48 for odometryKITTI [arxiv:2011.13005 §3.5] [arxiv:2011.13005
tab. 6]. The circle loss is one of three terms trained jointly with an overlap and
a matchability term [arxiv:2011.13005 §3.5]. The paper gives its own loss weights
as equal, and it does not report a circle-loss ablation: its ablation varies the
overlap-attention parts instead [arxiv:2011.13005 tab. 3]. The paper's footnote says the
loss was added to the released code after publication and is not described in the
paper body, so the code is the reference for it [arxiv:2011.13005 §3.5]
[arxiv:2011.13005 Paper vs code]. Card: [PREDATOR: Registration of 3D Point Clouds
with Low Overlap](/papers/2011.13005).

### Proxy-based classifier (LSC in PODNet)

Each class is represented by $K$ learned proxies instead of by sample pairs. The
score of a class is a soft-attention-weighted sum of cosine similarities to its
proxies [arxiv:2004.13513 eq. 9], trained with a modified NCA loss that has a
margin $\delta$ on the target logit and a hinge [arxiv:2004.13513 eq. 10]
[arxiv:2004.13513 §3.2]. The paper reports that NCA converged faster than plain
cross-entropy [arxiv:2004.13513 §3.2]. New classes get
their proxies from k-means centroids of their features under the current model,
which the paper reports adds about 1 point on CIFAR100 [arxiv:2004.13513 §3.2].
In its class-incremental experiments, going from one to ten proxies per class
added 1.18 points on CIFAR100 and 1.51 on ImageNet100 with ten steps; the paper
reports $K$ as stable between 5 and 15 [arxiv:2004.13513 §6.3]. Card:
[PODNet: Pooled Outputs Distillation for Small-Tasks Incremental Learning](/papers/2004.13513).

### Triplet loss with nearest-class-mean classification

An embedding network trained with the triplet loss classifies a test image by
the nearest class prototype, the mean embedding of that class's training images
[arxiv:2004.00440 §3.1] [arxiv:2004.00440 eq. 2] [arxiv:2004.00440 eq. 3]. In
class-incremental learning on six-task CUB-200-2011 with ResNet-18, the paper
reports average incremental accuracy of 37.4 for embedding fine-tuning against
11.4 for softmax fine-tuning [arxiv:2004.00440 tab. 1]. Stored prototypes go
stale as the embedding moves. Semantic Drift Compensation shifts each stored
prototype by a Gaussian-weighted average of the embedding drift measured on
current-task images, without any old data [arxiv:2004.00440 §4.1]
[arxiv:2004.00440 eq. 10] [arxiv:2004.00440 eq. 11]. The gains are
author-reported and differ by dataset; see Contradictions. Card:
[Semantic Drift Compensation for Class-Incremental Learning](/papers/2004.00440).

### Cross-view triplet with region branches (FSRA)

FSRA encodes each view with a ViT-S and adds region branches next to a global
one. It splits the patch tokens into regions by their mean activation and pools
each region [arxiv:2201.09206 §3.1] [arxiv:2201.09206 §3.2]
[arxiv:2201.09206 eq. 3] [arxiv:2201.09206 eq. 4]. Each region vector and the
global feature go through a classifier head trained with cross-entropy, and the
triplet loss is applied to every branch with margin 0.3, using only pairs from
different views [arxiv:2201.09206 §3.4] [arxiv:2201.09206 eq. 10]. Each
satellite image is repeated $k$ times with augmentation; the paper reports $k=3$
as best at batch size 8 [arxiv:2201.09206 §3.3] [arxiv:2201.09206 fig. 11].
On University-1652, drone-to-satellite R@1 is 84.51 with $k=3$, against 75.93
for LPN with ResNet-50 [arxiv:2201.09206 tab. 2]. These are author-reported.
Card: [FSRA, a transformer with region alignment for UAV-view
geo-localization](/papers/2201.09206).

### Symmetric InfoNCE with GPS and similarity-based hard negatives (cross-view geo-localisation)

One ConvNeXt-B encodes both street-view and satellite images, with mean-pooled
features and no attention pooling or polar transform [arxiv:2303.11851 §3.2]
[arxiv:2303.11851 fig. 2]. Each batch is scored in both directions: every street
image is a query over the satellite images, and the reverse, with the matching
pair as the positive and all other batch items as negatives
[arxiv:2303.11851 §3.1] [arxiv:2303.11851 eq. 1]. Negatives are chosen in two
stages. Early in training, nearby GPS locations are placed in the same batch
[arxiv:2303.11851 §3.3]. Later, Dynamic Similarity Sampling takes the top K
most similar samples per query under cosine similarity, keeps the first k/2 of
them and draws k/2 at random from the rest of the pool; the pool is recomputed
every e epochs, with k = 64, K = 128 and e = 4 [arxiv:2303.11851 §3.4]. On
CVUSA, GPS plus DSS reaches R@1 98.68 against 97.83 for random batches
[arxiv:2303.11851 supp. "Sampling Strategies", tab. 10]. The symmetric loss
beats either single direction on VIGOR SAME (R@1 77.86 against 77.49 and 74.69)
[arxiv:2303.11851 supp. "Symmetric InfoNCE Loss", tab. 8]. All numbers are
author-reported. The triplet and InfoNCE comparison is under Contradictions.
Card: [Sample4Geo: Hard Negative Sampling For Cross-View Geo-Localisation](/papers/2303.11851).

### Triplet ranking with domain and semantic losses (zero-shot sketch retrieval)

Two VGG-16 encoders with attention embed sketches and photos into one space,
ranked by L2 distance [arxiv:1904.03451 §4.2]. Training sums three terms: a
triplet ranking loss on L2 distances with margin $\mu>0$ [arxiv:1904.03451 eq. 1]
[arxiv:1904.03451 §4.3]; a domain loss that labels sketches and photos as two
domains [arxiv:1904.03451 eq. 2]; and a semantic loss that scores each embedding
against the word vector of its category, using a half cosine distance
[arxiv:1904.03451 eq. 3]. The paper does not state the margin or the loss
weights, and does not state how negatives are sampled. The released code fixes
the margin at 1.0 and samples negatives by distance between category vectors
[code: src/loss/loss.py#L79] [code: src/data/quickdraw_extended.py#L129-L130].
In the ablation, the triplet-only baseline scores 0.3020 mAP on Sketchy,
0.0590 on TU-Berlin and 0.0354 on QuickDraw. Adding attention, domain and
semantic losses raises these to 0.3691, 0.1094 and 0.0752
[arxiv:1904.03451 tab. 3]. These are author-reported full-gallery mAP values on
unseen test categories. Card: [Doodle to Search: Practical Zero-Shot Sketch-based
Image Retrieval](/papers/1904.03451).

### Bi-encoder with additive-margin softmax (speech-text retrieval)

Speech and text are embedded separately, and each batch is scored as a softmax
over its in-batch pairs, with the matching pair on the diagonal and an additive
margin. The loss runs in both directions, speech to text and text to speech
[arxiv:2205.12446 §4.3]. Only the 600M mSLAM model is fine-tuned for this
retrieval, so the paper offers no second model to compare with [arxiv:2205.12446 tab. 5]. The
margin, scale, similarity function and pooling are not stated [arxiv:2205.12446
§4.3]. Author-reported P@1 over all 102 languages is 76.9 for speech to text and
74.4 for text to speech, but CJK falls to 4.7 in both directions [arxiv:2205.12446 tab. 5].
Card: [FLEURS: Few-shot Learning Evaluation of Universal Representations of
Speech](/papers/2205.12446).

## When to use which

| Situation | Use | Why |
|---|---|---|
| Only "similar / not similar" labels per pair, no class identity | Pair or triplet loss, with hard-negative mining | Needs only the pair definition; the review notes slow convergence and that hard-negative mining is essential [arxiv:2010.05113 §III.E.2] |
| Batch can hold many items and you want more comparisons per step | Lifted-structure or N-pair style loss | Each query is compared with many positives and negatives per batch [arxiv:2010.05113 §III.E.2] [arxiv:2010.05113 §III.E.3] |
| Dense point-to-point correspondences between two scans, with ground truth from a known transform | Circle loss on feature-space pairs, with positive and negative radii (PREDATOR) | Positives lie within $r_p$ and negatives beyond $r_s$ of each anchor [arxiv:2011.13005 eq. 8]; it is trained with overlap and matchability terms [arxiv:2011.13005 §3.5]; the card has no ablation of the loss alone [arxiv:2011.13005 tab. 3] |
| Class labels exist and you want to avoid pair mining | Proxy-based classifier (LSC-style) | Classes are scored through K learned proxies, so no pairs are sampled [arxiv:2004.13513 eq. 9]; K between 5 and 15 was stable in the paper [arxiv:2004.13513 §6.3] |
| Full softmax over every negative is too expensive | Sampled or in-batch negatives | The normaliser sums over every negative in the dataset, which the review calls expensive; NCE-style approximations replace it [arxiv:2010.05113 §III.E.3] |
| Classes arrive over time and old-class data is limited | Proxy classifier plus a distillation term (PODNet) | With POD-spatial, LSC scores 61.42% NME versus 54.32% for a cosine classifier on CIFAR100 with 50 steps; without distillation the gap is 41.56% versus 40.76% [arxiv:2004.13513 tab. 3] |
| Class-incremental learning with an embedding network and no stored old data | Triplet-trained embedding with nearest-class-mean and drift-corrected prototypes (SDC) | Prototypes are shifted by estimated drift on current-task images [arxiv:2004.00440 §4.1]; gains are reported on CUB-200-2011 and Flowers-102 and are not uniform across datasets [arxiv:2004.00440 tab. 1] [arxiv:2004.00440 tab. 5] |
| Many sentences must be compared or clustered, and a pair model would need one pass per pair | Siamese pooled encoder with cosine score, trained on NLI pairs and then on graded similarity labels (Sentence-BERT) | Each sentence is encoded once and compared by a vector calculation [arxiv:1908.10084 §1] [arxiv:1908.10084 §6]; the paper's MEAN pooling beat CLS and MAX on STSb dev [arxiv:1908.10084 tab. 6] |
| Relatedness is given by a graph (for example citations) and you need fixed embeddings without fine-tuning | Triplet loss over graph-defined triples with hard and easy negatives (SPECTER) | Positives are direct citations and hard negatives are citations of citations [arxiv:2004.07180 §2.4]; removing the hard negatives costs 1.6 points on the author-reported average [arxiv:2004.07180 tab. 2]; the fixed embedding beat fine-tuning SciBERT on the same signals [arxiv:2004.07180 tab. 3] |
| Two views of the same entity from different sources, one positive per entity | Cross-view triplet on each branch, margin 0.3 on normalised features | Only cross-view pairs are used [arxiv:2201.09206 §3.4]; $k$ augmented copies of the single satellite image help, with $k=3$ best at batch size 8 [arxiv:2201.09206 §3.3] |
| Re-identify objects across video frames for tracking-by-detection, with identity labels available | Softmax plus triplet embedding with cosine affinity (POI) | The feature is one factor of the affinity [arxiv:1610.06136 §3, eq. 4]; the paper's feature beats its GoogLeNet-labelled baseline on MOT16 train [arxiv:1610.06136 tab. 2]; triplet margin and loss weight are not stated, so they must be set by the implementer |
| Scores are compared across embeddings with varying norms | Normalise embeddings or use cosine | The unbounded dot product depends on norms; cosine bounds the score to [-1, 1] [arxiv:2010.05113 §III.E.1] |
| Paired views (street and satellite) that one shared encoder must match, many in-batch items per step | Symmetric InfoNCE over the batch in both directions, with GPS-neighbour and then similarity-based negatives (Sample4Geo) | The symmetric form beat either single direction on VIGOR SAME [arxiv:2303.11851 supp. "Symmetric InfoNCE Loss", tab. 8]; a triplet loss with one negative per anchor collapsed to R@1 0.00 on CVUSA in the paper's run [arxiv:2303.11851 supp. "Loss comparison", tab. 9] |
| Unseen-category queries (for example sketches) against a photo gallery, with a large domain gap | Triplet ranking plus domain and semantic losses (zero-shot SBIR) | Each added term raised author-reported mAP over the triplet-only baseline on all three datasets [arxiv:1904.03451 tab. 3]; the margin and loss weights are not stated |
| Paired items from two modalities (speech and text) and no hand-built hard negatives | Bi-encoder with additive-margin softmax over in-batch pairs, both directions (FLEURS) | The paper trains on all in-batch pairs in both directions [arxiv:2205.12446 §4.3]; margin and scale are not stated, so they must be set by the implementer; CJK retrieval is near 5% P@1 [arxiv:2205.12446 tab. 5], so check per-language results |
| Binary real-versus-manipulated decisions where the fake class is many unrelated methods, with softmax already in use | Softmax plus single-center loss, margin scaled by embedding size (face forgery) | The real class is compacted and fakes are only pushed beyond a margin [arxiv:2103.09096 eq. 1] [arxiv:2103.09096 §3.3]; the paper's c40 ablation gains 0.055 AUC over softmax alone [arxiv:2103.09096 tab. 3]; the margin must be rescaled if the backbone changes D [arxiv:2103.09096 eq. 1] |
| Few-shot classification where spatial detail matters, such as fine-grained classes | Reconstruction-error score over the class's support maps (FRN) | Scores come from a closed-form solve with no test-time gradient [arxiv:2012.01506 §3.4]; author-reported gains on fine-grained tables [arxiv:2012.01506 tab. 3] [arxiv:2012.01506 tab. 5]; on mini-ImageNet and tiered-ImageNet the results are mixed against baselines [arxiv:2012.01506 tab. 7], so check the benchmark |
| No labels at all, only augmented views of the same input | Use the contrastive family | Instance discrimination is covered on [contrastive-learning](/concepts/contrastive-learning) |

No card compares proxy-based losses with triplet or pair losses head to head.
The table above follows from each method's constraints, not from a measured
ranking. For face embeddings trained through a classification head, see
[angular-margin-loss](/concepts/angular-margin-loss).

## Implementation sketch

The common core is one batch, one score and one margin loss. The variants differ
in which pairs enter and how the loss normalises.

```pseudocode
# Pair and triplet losses on a batch  [arxiv:2010.05113 eq. 2] [eq. 3]
# Inputs: embeddings z (B, d), margin m; the review's margin is on D^2

def pair_loss(z_q, z_k, is_pos, m):
    # Squared distance on the embeddings  [eq. 2]
    d2 = sum((z_q - z_k) ** 2)
    # Positives are pulled in; negatives count only inside the margin  [eq. 2]
    return where(is_pos, d2, max(0, m - d2))

def triplet_loss(z_a, z_p, z_n, m):
    # Anchor nearer to positive than to negative by at least m  [eq. 3]
    d_ap = sum((z_a - z_p) ** 2)
    d_an = sum((z_a - z_n) ** 2)
    return max(0, d_ap - d_an + m)

# Proxy-based logits, K proxies per class  [arxiv:2004.13513 eq. 9]
# Inputs: embedding z (d,), proxies (C, K, d)

def proxy_logits(z, proxies):
    # Cosine similarity of z to every proxy of every class  [eq. 9]
    sims = cosine(z, proxies)                  # (C, K)
    # Soft attention over the K proxies of each class  [eq. 9]
    attn = softmax(sims, axis=-1)
    return sum(attn * sims, axis=-1)           # (C,) one logit per class

# Nearest-class-mean prediction  [arxiv:2004.00440 eq. 2] [eq. 3]
# Inputs: query embedding z (d,), prototypes (C, d), one per class

def nearest_class_mean(z, prototypes):
    # Squared distance to each class mean  [eq. 2]
    d2 = sum((z - prototypes) ** 2, axis=-1)   # (C,)
    # Predicted class is the closest prototype  [eq. 2]
    return argmin(d2)
```

Notes that follow from the cards. The margin in eq. 2 is on the squared
distance, not on the distance [arxiv:2010.05113 §III.E.2]. The PODNet paper
states cosine similarity, but the official code uses the negative squared
Euclidean distance of L2-normalised vectors, which on unit vectors equals
$2\cos - 2$; the code's logits are therefore twice the cosine logits at the same
scale [arxiv:2004.13513 Paper vs code]. The code also builds a per-target margin
that is never used, so the margin $\delta$ in eq. 10 has no effect there
[arxiv:2004.13513 Paper vs code]. Anyone reproducing LSC has to decide which of
these to follow and should say so.

For the nearest-class-mean sketch, the paper writes the distance only as a
generic dist(·,·), while the official code uses Euclidean distance
[arxiv:2004.00440 Paper vs code]. The triplet margin is not stated in the SDC
paper, and the code default is 0 [arxiv:2004.00440 Pitfalls]. FSRA uses margin
0.3 on L2-normalised features, whereas its paper writes the raw distance; with
normalisation, the margin acts on the unit sphere [arxiv:2201.09206 Paper vs code]
[arxiv:2201.09206 Pitfalls].

The sketch's triplet loss uses squared distances. SPECTER's eq. 2 uses L2 distance
with margin 1 [arxiv:2004.07180 eq. 2], so the same margin value means different
things on the two scales. Pick one, state it, and keep it fixed across training
and evaluation. Sentence-BERT's triplet hinge also uses Euclidean distance with
margin 1 in the paper [arxiv:1908.10084 §3], but the official library's default
triplet margin is 5, so a run that takes the default is not the paper's setting
[arxiv:1908.10084 Paper vs code].

For the circle loss, the PREDATOR code clips the pair weights at zero and
detaches them from the graph, and it keeps only correspondences within the
positive radius minus 1 mm, then randomly keeps at most $n_p$ of them
[arxiv:2011.13005 Paper vs code]. A reimplementation that follows eq. 8 literally
will weight pairs differently from the released code.

For the single-center loss, the margin is $m\sqrt{D}$, so changing the backbone
changes the margin unless $m$ is rescaled [arxiv:2103.09096 eq. 1]. The
loss needs at least one real and one fake sample in each batch, which the paper
does not address, and the batch size is not stated [arxiv:2103.09096 Pitfalls].
The card records no official code, so the margin and loss form here come from the paper
alone [arxiv:2103.09096 Paper vs code].

For FRN, the penalty scales with $kr/d$ so that repeating a support pool does
not change the reconstruction, and the Woodbury form inverts a $d \times d$
matrix once per class per episode [arxiv:2012.01506 §3.2] [arxiv:2012.01506 §3.4].
The released code differs from the paper in the auxiliary loss and adds a small
constant to the penalty [arxiv:2012.01506 Paper vs code].

## Contradictions

The review's slow-convergence remark concerns pair and triplet losses
[arxiv:2010.05113 §III.E.2], while PODNet's concerns NCA against cross-entropy
on proxies [arxiv:2004.13513 §3.2]; these do not conflict. The PODNet paper and
code disagree on the similarity function and on the margin, as described in the
sketch notes [arxiv:2004.13513 Paper vs code]. Further disagreements:

- **Hard-negative mining.** The review calls hard-negative mining essential for
  triplet losses [arxiv:2010.05113 §III.E.2]. The SDC demo selects a triplet loss
  that pairs each anchor by batch position, not by hardest negative
  [arxiv:2004.00440 Paper vs code]. These are different settings, so neither
  settles the other. FSRA's code uses the hardest cross-view positive and
  negative, which the paper does not describe [arxiv:2201.09206 Paper vs code].
  SPECTER measures a smaller effect of hard negatives than "essential" suggests:
  removing them lowers its author-reported average by 1.6 points
  [arxiv:2004.07180 tab. 2]. That is one setting, and the review does not
  quantify its claim.
- **Triplet loss collapse.** The review calls hard-negative mining essential
  for triplet losses [arxiv:2010.05113 §III.E.2]. Sample4Geo states that hard
  negatives with a triplet loss can make the model collapse [arxiv:2303.11851 §1]
  [arxiv:2303.11851 §3.1]. In its CVUSA run, a triplet loss with one negative per
  anchor reached R@1 0.00, while a soft-margin triplet reached 91.83 and InfoNCE
  98.68 [arxiv:2303.11851 supp. "Loss comparison", tab. 9]. The paper does not
  state the triplet run's margin, mining or batch composition, so this result
  does not show that triplet losses fail in general.
- **Cosine versus L2.** The review recommends normalising or using cosine when the
  dot product depends on norms [arxiv:2010.05113 §III.E.1]. SPECTER reports that
  L2 distance beat cosine distance for its triplet loss [arxiv:2004.07180 §2.3].
  Sentence-BERT scores with cosine at inference and reports that negative
  Manhattan and Euclidean distances gave roughly the same results, without
  numbers [arxiv:1908.10084 §4]. The papers compare different things, so none
  of them decides the choice for another dataset.
- **SDC gains differ by dataset.** Within one paper, E-LwF+SDC over E-LwF is
  +1.3 on six-task CUB-200-2011 and +6.2 on six-task Flowers-102
  [arxiv:2004.00440 tab. 1], but 0.0 on eleven-task CIFAR100 and −1.0 on
  eleven-task ImageNet-Subset [arxiv:2004.00440 tab. 5]. The paper does not
  resolve this difference; a reader should not assume SDC helps on every dataset.
- **FSRA table versus text.** The k = 1 row of Table 2 matches the
  KL-plus-triplet row of Table 7, but §3.4 says the KL loss was not used in the
  final model. The triplet-only row differs slightly [arxiv:2201.09206 tab. 2]
  [arxiv:2201.09206 tab. 7] [arxiv:2201.09206 §3.4].

## Papers

- [Contrastive Representation Learning: A Framework and Review](/papers/2010.05113): a framework that places pair, triplet, N-pair and NCE losses in one taxonomy of similarity, encoders, heads and losses.
- [PODNet: Pooled Outputs Distillation for Small-Tasks Incremental Learning](/papers/2004.13513): a proxy-based classifier (LSC) with K proxies per class, used with a distillation loss in class-incremental learning.
- [Semantic Drift Compensation for Class-Incremental Learning](/papers/2004.00440): a triplet-trained embedding network with nearest-class-mean classification, plus a drift-based correction of stored class prototypes.
- [SPECTER: Document-level Representation Learning using Citation-informed Transformers](/papers/2004.07180): a SciBERT encoder trained with a triplet loss on citation-defined triples, giving one embedding per paper from its title and abstract.
- [Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks](/papers/1908.10084): a BERT or RoBERTa siamese encoder with mean pooling, trained with NLI softmax, cosine regression or triplet losses, and compared by cosine similarity.
- [FSRA, a transformer with region alignment for UAV-view geo-localization](/papers/2201.09206): a ViT-S with region branches and cross-view triplet losses for drone-to-satellite matching.
- [Doodle to Search: Practical Zero-Shot Sketch-based Image Retrieval](/papers/1904.03451): two attention-equipped VGG-16 encoders trained with triplet, domain and semantic losses for sketch-to-photo retrieval of unseen categories.
- [Sample4Geo: Hard Negative Sampling For Cross-View Geo-Localisation](/papers/2303.11851): one shared ConvNeXt-B trained with symmetric InfoNCE, using GPS-based and then similarity-based hard negatives.
- [FLEURS: Few-shot Learning Evaluation of Universal Representations of Speech](/papers/2205.12446): a 102-language speech benchmark whose speech-text retrieval baseline trains a margin softmax over in-batch pairs in both directions.
- [PREDATOR: Registration of 3D Point Clouds with Low Overlap](/papers/2011.13005): a point-cloud registration network whose per-point descriptors are trained with a circle loss over ground-truth correspondences, alongside overlap and matchability terms.
- [Frequency-aware Discriminative Feature Learning Supervised by Single-Center Loss for Face Forgery Detection](/papers/2103.09096): an Xception with a learned DCT-frequency branch, trained with softmax plus a single-center loss that pulls real faces to a center and pushes fakes beyond a margin.
- [Few-Shot Classification with Feature Map Reconstruction Networks](/papers/2012.01506): scores each class by the ridge-regression reconstruction error of the query's spatial feature map from that class's support maps.
- [POI: Multiple Object Tracking with High Performance Detection and Appearance Feature](/papers/1610.06136): a Faster R-CNN tracker whose 128-d appearance embedding is trained with softmax and triplet losses and combined with motion and shape affinities.
