Loss functions play a critical role in the performance of your fine-tuned model. Sadly, there is no "one size fits all" loss function. Ideally, this table should help narrow…
Loss Overview
Loss Table
Loss functions play a critical role in the performance of your fine-tuned model. Sadly, there is no "one size fits all" loss function. Ideally, this table should help narrow down your choice of loss function(s) by matching them to your data formats.
Legend: Loss functions marked with ★ are commonly recommended default choices.
| Inputs | Labels | Appropriate Loss Functions |
|---|---|---|
(anchor, positive) pairs |
none |
MultiVectorMultipleNegativesRankingLoss ★CachedMultiVectorMultipleNegativesRankingLoss ★ |
(anchor, positive, negative) triplets |
none |
MultiVectorMultipleNegativesRankingLoss ★CachedMultiVectorMultipleNegativesRankingLoss ★ |
(anchor, positive, negative_1, ..., negative_n) |
none |
MultiVectorMultipleNegativesRankingLoss ★CachedMultiVectorMultipleNegativesRankingLoss ★ |
Distillation
These loss functions are specifically designed to be used when distilling the knowledge from one model into another. Distillation from a strong cross-encoder teacher is how the strongest late-interaction models (e.g. ColBERTv2, GTE-ModernColBERT) are trained.
| Inputs | Labels | Appropriate Loss Functions |
|---|---|---|
(query, positive, negative) |
gold_sim(query, positive) - gold_sim(query, negative) |
MultiVectorMarginMSELoss |
(query, positive, negative_1, ..., negative_n) |
[gold_sim(query, positive) - gold_sim(query, negative_i) for i in 1..n] |
MultiVectorMarginMSELoss |
(query, positive, negative) |
[gold_sim(query, positive), gold_sim(query, negative)] |
MultiVectorMarginMSELoss |
(query, doc_1, ..., doc_n) |
[gold_sim(query, doc_i) for i in 1..n] |
MultiVectorDistillKLDivLoss ★MultiVectorMarginMSELoss |
Loss modifiers
These loss functions can be seen as loss modifiers: they work on top of standard retrieval losses, applying them in different ways to instil useful properties into the trained embedding model. For index compression, you can use MultiVectorTokenPoolingLoss to train for fewer document vectors, MultiVectorMatryoshkaLoss to train for reduced embedding dimensionality, or stack them to train for both.
| Inputs | Labels | Appropriate Loss Functions |
|---|---|---|
any |
any |
MultiVectorTokenPoolingLoss``MultiVectorMatryoshkaLoss |
See the token pooling and Matryoshka examples for training comparisons, measured results, and inference.
Commonly used Loss Functions
In practice, not all loss functions get used equally often. The most common scenarios are:
(anchor, positive) pairswithout any labels: MultiVectorMultipleNegativesRankingLoss (a.k.a. InfoNCE or in-batch negatives loss) is cheap to obtain data for and generally very performant, especially combined with mined hard negatives. CachedMultiVectorMultipleNegativesRankingLoss extends it to very large effective batch sizes.(query, doc_1, ..., doc_n)with teacher scores: MultiVectorDistillKLDivLoss implements the n-way knowledge distillation recipe behind the strongest late-interaction models.
Custom Loss Functions
Source and license
Source: Sentence Transformers documentation, adapted for this site. Licensing and attribution follow the terms of the upstream project.