Training and Finetuning Embedding Models with Sentence Transformers
Hugging Face has detailed a streamlined workflow for training and finetuning embedding models using the Sentence Transformers library. This framework allows developers to adapt general-purpose embedding models to specific notions of similarity required for tasks such as retrieval augmented generation (RAG), semantic search, and paraphrase mining.
The Necessity of Finetuning for Task-Specific Similarity
Finetuning is critical because different applications require different definitions of "similarity." For example, two news headlines about different companies (e.g., Apple and NVIDIA) may be considered similar by a news classification model because both are in the "Technology" category, but must be considered dissimilar by a semantic textual similarity or retrieval model because they describe different events.
Core Training Components
Training a Sentence Transformer model involves five primary components:
- Dataset: The training and evaluation data, typically loaded as
datasets.Datasetordatasets.DatasetDictinstances. - Loss Function: A function that quantifies model performance and guides optimization based on the available data and target task.
- Training Arguments: Optional parameters via
SentenceTransformersTrainingArgumentsthat control training efficiency, tracking, and debugging. - Evaluator: Optional tools to assess model performance using concrete metrics before, during, or after training.
- Trainer: The
SentenceTransformerTrainerwhich integrates the model, dataset, loss function, and other components.
Dataset Requirements and Formatting
Datasets can be sourced from the Hugging Face Hub (often tagged with sentence-transformers) or loaded locally from CSV, JSON, Parquet, Arrow, or SQL formats. To ensure compatibility with the chosen loss function, datasets must follow specific formatting rules:
- Labels: If a loss function requires a label, the dataset must include a column named
labelorscore. - Inputs: All columns other than the label are treated as inputs. The number and order of these columns must match the requirements of the loss function (e.g., an
(anchor, positive, negative)triplet format).
Loss Functions and Training Arguments
Loss functions are initialized with the model being trained. The choice of loss depends on the data available (e.g., CoSENTLoss for pairs with floating-point similarity scores).
Training performance can be tuned using SentenceTransformersTrainingArguments, which includes parameters for num_train_epochs, per_device_train_batch_size, warmup_ratio, and hardware-specific settings like fp16 or bf16. For losses utilizing "in-batch negatives," the batch_sampler=BatchSamplers.NO_DUPLICATES argument is recommended.
Model Evaluation
While the trainer can provide evaluation loss, dedicated evaluators provide task-specific metrics. Available evaluators include:
- EmbeddingSimilarityEvaluator: For pairs with similarity scores (e.g., using the STSb benchmark).
- TripletEvaluator: For
(anchor, positive, negative)pairs (e.g., using the AllNLI dataset). - InformationRetrievalEvaluator: For queries, corpora, and relevant documents.
- BinaryClassificationEvaluator: For pairs with class labels.
Multiple evaluators can be combined into a single SequentialEvaluator to track various metrics simultaneously during training.
Advanced Training Workflows
Multi-Dataset Training
High-performance models often require training on multiple datasets simultaneously. The SentenceTransformerTrainer supports this by accepting a dictionary of datasets and a corresponding dictionary of loss functions. This allows different losses to be applied to different datasets within the same training run.
Sampling from multiple datasets can be handled via MultiDatasetBatchSamplers using two strategies:
- ROUND_ROBIN: Samples equally from each dataset until one is exhausted.
- PROPORTIONAL: Samples proportionally to the size of each dataset, ensuring all samples are used.
Transition to SentenceTransformerTrainer
With the release of Sentence Transformers v3.0, the traditional SentenceTransformer.fit method now uses SentenceTransformerTrainer internally. While legacy code remains functional, Hugging Face recommends adopting the new trainer approach to leverage advanced features like multi-GPU training and improved loss logging.
Performance Example
In a provided example, finetuning the microsoft/mpnet-base model on AllNLI triplets using MultipleNegativesRankingLoss resulted in a significant performance increase. The base model scored 68.32% on the development set, while the finetuned model achieved 90.04% on the development set and 91.5% on the testing set, measured by triplet accuracy using cosine similarity.