The SpladeLoss implements a specialized loss function for SPLADE (Sparse Lexical and Expansion) models. It combines a main loss function with regularization terms to balance…
Loss Overview
Sparse specific Loss Functions
SPLADE Loss
The SpladeLoss implements a specialized loss function for SPLADE (Sparse Lexical and Expansion) models. It combines a main loss function with regularization terms to balance effectiveness and efficiency:
- Main loss: Supports all the losses from the Loss Table and Distillation, with SparseMultipleNegativesRankingLoss, SparseMarginMSELoss and SparseDistillKLDivLoss commonly used.
- Regularization loss: FlopsLoss is used to control sparsity, but supports custom regularizers.
query_regularizeranddocument_regularizercan be set to any custom regularization loss.query_regularizer_thresholdanddocument_regularizer_thresholdcan be set to control the sparsity strictness for queries and documents separately, setting the regularization loss to zero if an embedding has less than the threshold number of active (non-zero) dimensions.
Cached SPLADE Loss
The CachedSpladeLoss is a variant of the SPLADE loss adopting GradCache, which allows for much larger batch sizes without additional GPU memory usage. It achieves this by computing and caching loss gradients in mini-batches.
Main losses that use in-batch negatives, primarily SparseMultipleNegativesRankingLoss, benefit heavily from larger batch sizes, as it results in more negatives and a stronger training signal.
CSR Loss
If you are using the SparseAutoEncoder module, then you have to use the CSRLoss (Contrastive Sparse Representation Loss). It combines two components:
- Main loss: Supports all the losses from the Loss Table and Distillation, with SparseMultipleNegativesRankingLoss used in the CSR Paper.
- Reconstruction loss: CSRReconstructionLoss is used to ensure that sparse representation can faithfully reconstruct the original dense embeddings.
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 |
SparseMultipleNegativesRankingLoss ★ |
(input_A, input_B) pairs |
float similarity score between 0 and 1 |
SparseCoSENTLoss``SparseAnglELoss``SparseCosineSimilarityLoss |
(anchor, positive, negative) triplets |
none |
SparseMultipleNegativesRankingLoss ★SparseTripletLoss |
(anchor, positive, negative_1, ..., negative_n) |
none |
SparseMultipleNegativesRankingLoss ★ |
Distillation
These loss functions are specifically designed to be used when distilling the knowledge from one model into another. This is rather commonly used when training Sparse embedding models.
| Inputs | Labels | Appropriate Loss Functions |
|---|---|---|
input |
model embeddings |
SparseMSELoss |
(input_1, input_2, ..., input_N) |
model embeddings |
SparseMSELoss |
(query, document_one, document_two) |
gold_sim(query, document_one) - gold_sim(query, document_two) |
SparseMarginMSELoss |
(query, positive, negative_1, ..., negative_n) |
[gold_sim(query, positive) - gold_sim(query, negative_i) for i in 1..n] |
SparseMarginMSELoss |
(query, positive, negative) |
[gold_sim(query, positive), gold_sim(query, negative)] |
SparseDistillKLDivLoss``SparseMarginMSELoss |
(query, positive, negative_1, ..., negative_n) |
[gold_sim(query, positive), gold_sim(query, negative_i)...] |
SparseDistillKLDivLoss``SparseMarginMSELoss |
Commonly used Loss Functions
In practice, not all loss functions get used equally often. The most common scenarios are:
(anchor, positive) pairswithout any labels: SparseMultipleNegativesRankingLoss (a.k.a. InfoNCE or in-batch negatives loss) is commonly used to train the top performing embedding models. This data is often relatively cheap to obtain, and the models are generally very performant. Here for our sparse retrieval tasks, this format works well with SpladeLoss, CachedSpladeLoss, or CSRLoss, all typically using InfoNCE as their underlying loss function.(query, positive, negative_1, ..., negative_n)format: This structure with multiple negatives is particularly effective with SpladeLoss configured with SparseMarginMSELoss, especially in knowledge distillation scenarios where a teacher model provides similarity scores. The strongest models are trained with distillation losses like SparseDistillKLDivLoss or SparseMarginMSELoss.
Custom Loss Functions
Source and license
Source: Sentence Transformers documentation, adapted for this site. Licensing and attribution follow the terms of the upstream project.