Sentence Transformers
Sentence Transformers computes embeddings with Sentence Transformer models and calculates similarity scores with Cross-Encoder (reranker) models.
What it is · Main features · Getting started
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
- Homepage
- https://www.sbert.net
- Verified
Main features
Sparse Encoder models generate sparse embeddings.
Multi-Vector Encoder models compute token-level embeddings for ColBERT-style late-interaction retrieval.
The listed applications include semantic search, semantic textual similarity and paraphrase mining.
Over 15,000 pre-trained Sentence Transformers models are available on Hugging Face.
The project provides pretrained models for over 100 languages.
Some of the pretrained models are general purpose, while others produce embeddings for specific use cases.
The framework lets users fine-tune their own sentence embedding methods to get task-specific sentence embeddings.
A companion blog post on embedding models covers end-to-end training of bi-encoder embedding models.
A companion blog post on training reranker models covers training Cross Encoder models for the second stage of retrieve-and-rerank pipelines.
A companion blog post on sparse embedding models covers training SPLADE and other sparse encoders.
For reranker models, the first step is to download a pretrained reranker, also called a Cross Encoder model.
In the reranker example,
model.rankavoids having to perform the reranking manually.For sparse embeddings, the first step is to download a pretrained sparse embedding model, also called a Sparse Encoder model.
For multi-vector models, the first step is to download a pretrained multi-vector model, also called a late-interaction (ColBERT-style) model.
A companion blog post on multimodal embedding and reranker models covers using text, image, audio, and video models through a single API.
Getting started
The project recommends Python 3.10 or newer, PyTorch 2.2 or newer, and transformers 5.0 or newer.
The installation documentation covers uv, conda, source and editable installs, CUDA setup, and extras.
Logging in to Hugging Face gives higher Hugging Face Hub rate limits and access to private or gated models that the account can read.
For embeddings, the first step is to download a pretrained embedding model, also called a Sentence Transformer model.
The result is numpy arrays with the embeddings, one for each text.
These embeddings can then be used to compute similarities.
pip install -U sentence-transformersfrom sentence_transformers import SentenceTransformer
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")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)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.
- GitHub READMEhttps://github.com/huggingface/sentence-transformersFetched
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.
