---
title: "Train and Fine-Tune Sentence Transformers Models"
description: "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."
url: "https://swe.quest/articles/train-and-fine-tune-sentence-transformers-models-49bc0c36b2cdc22e"
canonical: "https://huggingface.co/blog/how-to-train-sentence-transformers"
source: "Hugging Face"
published: "2022-08-10"
difficulty: "medium"
kind: "How-to"
reading_minutes: 11
tags: ["model-training","machine-learning","python","search"]
---

# Train and Fine-Tune Sentence Transformers Models

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2022-08-10, 11 min read, How-to. Original: [https://huggingface.co/blog/how-to-train-sentence-transformers](https://huggingface.co/blog/how-to-train-sentence-transformers)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Model training](https://swe.quest/tags/model-training.md) (AI & ML)
- [Machine learning](https://swe.quest/tags/machine-learning.md) (AI & ML)
- [Python](https://swe.quest/tags/python.md) (Languages)
- [Search](https://swe.quest/tags/search.md) (Backend & APIs)

## Prerequisites

- Python ([easy posts on Python](https://swe.quest/tags/python.md?difficulty=easy))
- PyTorch DataLoader basics
- text embeddings ([easy posts on RAG & retrieval](https://swe.quest/tags/rag.md?difficulty=easy))

## Keywords

Sentence Transformers, Hugging Face Hub, MultipleNegativesRankingLoss, TripletLoss, distilroberta-base, all-MiniLM-L6-v2, Hugging Face Datasets

## Related posts

- [Training and Finetuning Embedding Models with Sentence Transformers](https://swe.quest/articles/training-and-finetuning-embedding-models-with-sentence-transformers-d6dcb3eebe384143.md): Hugging Face · 2024-05-28 · Medium · 14 min read · Model training, Python, Machine learning, Search, Evaluation
  Sentence Transformers v3 trains and finetunes embedding models through a SentenceTransformerTrainer that combines Hugging Face datasets, a loss function, optional training arguments and evaluators.
- [Train a Sentence Embedding Model with 1B Training Pairs](https://swe.quest/articles/train-a-sentence-embedding-model-with-1b-training-pairs-f0642783223f8c14.md): Hugging Face · 2021-10-25 · Hard · 6 min read · Model training, Machine learning, Vector search
  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.
- [Training and Finetuning Sparse Embedding Models with Sentence Transformers](https://swe.quest/articles/training-and-finetuning-sparse-embedding-models-with-sentence-transformers-bcdd540c54167ab1.md): Hugging Face · 2025-07-01 · Medium · 27 min read · Search, Model training, Python, RAG & retrieval
  Sentence Transformers' SparseEncoder API can train and finetune neural sparse embedding models such as SPLADE, inference-free SPLADE and Contrastive Sparse Representation (CSR).
- [Training and Finetuning Multi-Vector Embedding Models with Sentence Transformers](https://swe.quest/articles/training-and-finetuning-multi-vector-embedding-models-with-sentence-transformers-454449569f0d4793.md): Hugging Face · 2026-08-26 · Medium · 27 min read · Model training, Search, Python, RAG & retrieval
  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.
- [Training and Finetuning Reranker Models with Sentence Transformers](https://swe.quest/articles/training-and-finetuning-reranker-models-with-sentence-transformers-56e6eabec0c38454.md): Hugging Face · 2025-03-26 · Medium · 24 min read · Model training, Search, RAG & retrieval, Python
  Sentence Transformers can finetune reranker (cross-encoder) models on domain data.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
