opensi·si

Loss functions play a critical role in the performance of your fine-tuned Cross Encoder model. Sadly, there is no "one size fits all" loss function. Ideally, this table should…

Loss Overview

Loss Table

Loss functions play a critical role in the performance of your fine-tuned Cross Encoder 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.

Inputs Labels Number of Model Output Labels Appropriate Loss Functions
(input_A, input_B) pairs class num_classes CrossEntropyLoss
(anchor, positive) pairs none 1 MultipleNegativesRankingLoss``CachedMultipleNegativesRankingLoss
(anchor, positive/negative) pairs 1 if positive, 0 if negative 1 BinaryCrossEntropyLoss
(input_A, input_B) pairs float similarity score between 0 and 1 1 BinaryCrossEntropyLoss
(anchor, positive, negative) triplets none 1 MultipleNegativesRankingLoss``CachedMultipleNegativesRankingLoss
(anchor, positive, negative_1, ..., negative_n) none 1 MultipleNegativesRankingLoss``CachedMultipleNegativesRankingLoss
(query, [doc1, doc2, ..., docN]) [score1, score2, ..., scoreN] 1 LambdaLoss``PListMLELoss``ListNetLoss``RankNetLoss``ListMLELoss``ADRMSELoss

Distillation

These loss functions are specifically designed to be used when distilling the knowledge from one model into another. For example, when finetuning a small model to behave more like a larger & stronger one, or when finetuning a model to become multi-lingual.

Inputs Labels Appropriate Loss Functions
(input_A, input_B) pairs similarity score MSELoss
(query, document_one, document_two) triplets gold_sim(query, document_one) - gold_sim(query, document_two) MarginMSELoss
(query, positive, negative_1, ..., negative_n) [gold_sim(query, positive) - gold_sim(query, negative_i) for i in 1..n] MarginMSELoss
(query, positive, negative) [gold_sim(query, positive), gold_sim(query, negative)] MarginMSELoss
(query, positive, negative_1, ..., negative_n) [gold_sim(query, positive), gold_sim(query, negative_i)...] MarginMSELoss

Commonly used Loss Functions

In practice, not all loss functions get used equally often. The most common scenarios are:

  • (input_A, input_B) pairs with float similarity score or 1 if positive, 0 if negative: BinaryCrossEntropyLoss is a traditional option that remains very challenging to outperform.
  • (anchor, positive) pairs without any labels: combined with mine_hard_negatives
    • with output_format="labeled-list", then LambdaLoss is frequently used for learning-to-rank tasks.
    • with output_format="labeled-pair", then BinaryCrossEntropyLoss remains a strong option.

Custom Loss Functions

Source and license

Source: Sentence Transformers documentation, adapted for this site. Licensing and attribution follow the terms of the upstream project.