Sentence Transformers

Sentence Transformers computes embeddings with Sentence Transformer models and calculates similarity scores with Cross-Encoder (reranker) models.

Verified

On this page

What it is

Sentence Transformers can be used to compute embeddings using Sentence Transformer models. The project was originally developed by the Ubiquitous Knowledge Processing (UKP) Lab at TU Darmstadt. Cross-Encoder models, also called reranker models, calculate similarity scores, while Sentence Transformer models compute embeddings.

Core facts

Read by a script from the official API, not rewritten by a model.

License
Apache-2.0
Languages
Python
Maintainer
huggingface
Verified

Main features

  1. Sparse Encoder models generate sparse embeddings.

  2. Multi-Vector Encoder models compute token-level embeddings for ColBERT-style late-interaction retrieval.

  3. The listed applications include semantic search, semantic textual similarity and paraphrase mining.

  4. Over 15,000 pre-trained Sentence Transformers models are available on Hugging Face.

  5. The project provides pretrained models for over 100 languages.

  6. Some of the pretrained models are general purpose, while others produce embeddings for specific use cases.

  7. The framework lets users fine-tune their own sentence embedding methods to get task-specific sentence embeddings.

  8. A companion blog post on embedding models covers end-to-end training of bi-encoder embedding models.

  9. A companion blog post on training reranker models covers training Cross Encoder models for the second stage of retrieve-and-rerank pipelines.

  10. A companion blog post on sparse embedding models covers training SPLADE and other sparse encoders.

  11. For reranker models, the first step is to download a pretrained reranker, also called a Cross Encoder model.

  12. In the reranker example, model.rank avoids having to perform the reranking manually.

  13. For sparse embeddings, the first step is to download a pretrained sparse embedding model, also called a Sparse Encoder model.

  14. For multi-vector models, the first step is to download a pretrained multi-vector model, also called a late-interaction (ColBERT-style) model.

  15. A companion blog post on multimodal embedding and reranker models covers using text, image, audio, and video models through a single API.

Getting started

  1. The project recommends Python 3.10 or newer, PyTorch 2.2 or newer, and transformers 5.0 or newer.

  2. The installation documentation covers uv, conda, source and editable installs, CUDA setup, and extras.

  3. Logging in to Hugging Face gives higher Hugging Face Hub rate limits and access to private or gated models that the account can read.

  4. For embeddings, the first step is to download a pretrained embedding model, also called a Sentence Transformer model.

  5. The result is numpy arrays with the embeddings, one for each text.

  6. These embeddings can then be used to compute similarities.

From the README
Shell
pip install -U sentence-transformers
From the README
python
from sentence_transformers import SentenceTransformer

model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
From the README
python
sentences = [
    "The weather is lovely today.",
    "It's so sunny outside!",
    "He drove to the stadium.",
]
embeddings = model.encode(sentences)
print(embeddings.shape)
# => (3, 384)
From the README
python
similarities = model.similarity(embeddings, embeddings)
print(similarities)
# tensor([[1.0000, 0.6660, 0.1046],
#         [0.6660, 1.0000, 0.1411],
#         [0.1046, 0.1411, 1.0000]])

Who it is for

The repository contains experimental software and is published only to give additional background details on the respective publication.

Sources

This page is written from the official sources below. Open them to check the original text.

Verified ·

This page is written by AI from the official sources above and checked automatically. This site has not tested it. Found a mistake? Tell us.