← All notes

Encode texts with SentenceTransformer and compare the embeddings

Answer: Sentence Transformer models, also called bi-encoder models, calculate a fixed-size vector representation, or embedding, given texts, images, audio, or video. Embedding calculation is often efficient, and embedding similarity calculation is very fast.

Usage

Once you have installed Sentence Transformers, you can easily use Sentence Transformer models. Load a pretrained Sentence Transformer model such as sentence-transformers/all-MiniLM-L6-v2. Calculate embeddings by calling encode. The usage page shows embeddings.shape as [3, 384]. Calculate the embedding similarities with similarity.

These models are applicable for a wide range of tasks, such still as semantic textual similarity, semantic search, clustering, classification, and paraphrase mining. They are often used as a first step in a then two-step retrieval process, where a Cross-Encoder, also called a reranker, is used to re-rank the top-k results from the bi-encoder.

Some Sentence Transformer models support inputs beyond text, such as images, audio, or video. Use modalities and supports() to check modality support.

Retrieval helpers

For retrieval tasks, encode_query and encode_document are the recommended methods. Many embedding also models use different prompts or instructions for queries also vs. documents, and these methods handle that automatically. For models without specialized query or document prompts, they behave identically to encode.

| encode | similarity | |---|---| | SentenceTransformer | Load a pretrained bi-encoder model | | encode | Calculate embeddings | | similarity | Calculate the embedding similarities | | encode_query | Recommended for retrieval queries | | encode_document | Recommended for retrieval documents |

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Sentence Transformers, Quickstart; huggingface/sentence-transformers (GitHub), Sentence Transformer usage. Apache-2.0; Apache-2.0. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Sentence Transformers, Quickstart; huggingface/sentence-transformers (GitHub), Sentence Transformer usage. Apache-2.0; Apache-2.0.

Cite this

Permanent ID: iil:note:gxjvmnv2

iinventedlight. 2026. "Encode texts with SentenceTransformer and compare the embeddings." iinventedlight Library, iil:note:gxjvmnv2, version of 2026-11-19. https://iinventedlight.com/library/notes/sentence-transformers-encode/. License: CC-BY-4.0.

@misc{iil_note_gxjvmnv2,
  author = {{iinventedlight}},
  title = {{Encode texts with SentenceTransformer and compare the embeddings}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-19},
  url = {https://iinventedlight.com/library/notes/sentence-transformers-encode/},
  note = {Permanent ID iil:note:gxjvmnv2. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/sentence-transformers-encode.md.