Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Hugging Face 27 min read How-to mediumAdept

Training and Finetuning Sparse Embedding Models with Sentence Transformers

Sentence 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.

Related posts

  • Training and Finetuning Embedding Models with Sentence Transformers

    Hugging Face mediumAdept

    Sentence 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 and Fine-Tune Sentence Transformers Models

    Hugging Face mediumAdept

    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.

  • Train 400x faster Static Embedding Models with Sentence Transformers

    Hugging Face hardMaster

    Static embedding models, which replace the attention-based encoder with a token embedding lookup plus mean pooling (an EmbeddingBag), can be trained with modern contrastive losses and Matryoshka Representation Learning to run 100x to 400x faster on CPU than models like all-mpnet-base-v2 and multilingual-e5-small, while keeping at least 85% of their benchmark quality. The recipe trains two 1024-dimensional models from randomly initialized weights using BERT tokenizers and Sentence Transformers' StaticEmbedding module: an English retrieval model trained on 13 QA datasets chosen to avoid overlap with BEIR, and a multilingual similarity model trained on parallel-sentence and positive-pair datasets. The post covers dataset selection, loss choice, training arguments, evaluators, NanoBEIR results on CPU and GPU, and Matryoshka truncation tradeoffs. The resulting models suit on-device, in-browser and edge workloads.

  • Training and Finetuning Multi-Vector Embedding Models with Sentence Transformers

    Hugging Face mediumAdept

    Sentence 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.

  • Train a Sentence Embedding Model with 1B Training Pairs

    Hugging Face hardMaster

    Sentence 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.

The quest for software engineering knowledge.

Look:
Layout: