Train and Fine-Tune Sentence Transformers Models
Sentence Transformers models map text to fixed-size embeddings by running a pre-trained Transformer and pooling its token embeddings, and training them depends on matching the dataset format to the right loss. Four common formats map to specific losses: labeled pairs (ContrastiveLoss, SoftmaxLoss, CosineSimilarityLoss), unlabeled positive pairs (MultipleNegativesRankingLoss), class-labeled sentences (batch triplet losses), and unlabeled triplets (TripletLoss). A worked example builds a distilroberta-base model, trains it on QQP triplets with TripletLoss via the legacy `fit` API, and pushes it to the Hugging Face Hub. The guide is marked outdated: since v3.0, SentenceTransformerTrainer replaces this API.
Related posts
- Training and Finetuning Embedding Models with Sentence Transformers
Hugging Face mediumAdeptSentence Transformers v3 trains and finetunes embedding models through a SentenceTransformerTrainer that combines Hugging Face datasets, a loss function, optional training arguments and evaluators. Finetuning matters because each task needs its own notion of similarity. Dataset column order must match the chosen loss's inputs, with an optional "label" or "score" column, and extraneous columns must be removed. A worked example finetunes MPNet on AllNLI triplets with MultipleNegativesRankingLoss and a no-duplicates batch sampler, and evaluates with TripletEvaluator and EmbeddingSimilarityEvaluator on STSb.
- Train a Sentence Embedding Model with 1B Training Pairs
Hugging Face hardMasterSentence embedding models trained with a contrastive multiple-negatives ranking loss (InfoNCE) on up to 1 billion sentence pairs, using in-batch negatives, a scaled similarity (C≈20), and cosine vs. dot-product tradeoffs for clustering and ANN search. Batch composition drives quality: larger batches, hard negatives, and batches drawn mostly from one dataset but mixing at least two to learn both local and global topic structure. Training ran on TPU v3-8s with JAX/Flax and produced 20 general-purpose Sentence Transformers models (MiniLM, RoBERTa, DistilBERT, MPNet) reported as state of the art on sentence similarity tasks, plus 8 datasets and a Spaces demo.
- Training and Finetuning Sparse Embedding Models with Sentence Transformers
Hugging Face mediumAdeptSentence Transformers' SparseEncoder API can train and finetune neural sparse embedding models such as SPLADE, inference-free SPLADE and Contrastive Sparse Representation (CSR). The guide walks through each training component: model architecture, datasets, loss functions, training arguments, evaluators and the trainer. Sparse models produce vocabulary-sized vectors with learned query and document expansion, which makes them interpretable and strong in hybrid search. Finetuning teaches domain-specific expansions, for example mapping "cephalalgia" to "headache". Practical details include using router_mapping for the query/document split in inference-free SPLADE and a much higher learning rate for the SparseStaticEmbedding module.
- Training and Finetuning Multi-Vector Embedding Models with Sentence Transformers
Hugging Face mediumAdeptSentence Transformers v6.0 adds a MultiVectorEncoder model type for ColBERT-style late-interaction retrieval, with a full training stack covering models, datasets, losses, training arguments, evaluators and the trainer. A guide walks through finetuning on MIRIAD medical question-passage pairs. The resulting mLateOn-medical model, trained in 14.5 hours on one RTX 3090, beats every general-purpose dense, sparse, lexical and multi-vector retriever the author tested. Key findings: '-unsupervised' checkpoints taken before supervised finetuning adapt to a new domain better than finished ones, which barely improved or regressed. Lifting the 180–512 token document caps matters too, since truncation cost up to 0.24 NDCG@10 on long passages. A punctuation skiplist shrinks the index by 9.6% with a slight quality gain, and CachedMultiVectorMultipleNegativesRankingLoss allows large effective batch sizes.
- Training and Finetuning Reranker Models with Sentence Transformers
Hugging Face mediumAdeptSentence Transformers can finetune reranker (cross-encoder) models on domain data. The guide covers each training component: dataset loading and column-format rules, hard-negative mining with mine_hard_negatives, loss functions such as BinaryCrossEntropyLoss and CachedMultipleNegativesRankingLoss, training arguments, evaluators and the CrossEncoderTrainer. A small ModernBERT-base reranker finetuned on GooAQ with mined hard negatives outperforms the 13 most commonly used public rerankers on that evaluation data, including models 4x larger. The large variant beats all general-purpose rerankers tested there, which makes the case for domain finetuning in retrieve-and-rerank pipelines.