- Introduced by
- Reimers, N. & Gurevych, I. (SBERT); Conneau, A. et al. (InferSent)
- Year
- 2017–2019
- Type
- Transfer learning / sentence representation
- Family
- Deep learning / NLP / CV
What does it do?
Transfer learning with sentence embeddings starts from a large pre-trained sentence encoder, such as Sentence-BERT or the Universal Sentence Encoder, which already maps text to fixed-length vectors reflecting general language knowledge.
The encoder is then adapted to a new task or domain using a comparatively small set of labelled examples. The approach is most useful when labelled data are scarce and training a sentence encoder from scratch is impractical.
The intuition behind the method
Training a sentence encoder from scratch requires large text collections and substantial compute. A pre-trained encoder has already learned to place sentences with similar meaning close together in a dense vector space. Transfer learning reuses that space: your sentences are passed through the encoder, and a small downstream component, such as a classifier, a regression head, or a similarity threshold, is fitted on the resulting vectors. The encoder itself may also be updated. The gain comes from not relearning sentence-level semantics from limited labelled data, much as an experienced translator draws on an established vocabulary rather than relearning the language for each conversation.
History
Reuse of pre-trained sentence representations was explored in InferSent (Conneau et al., 2017), which trained a BiLSTM encoder on natural language inference data and reported transfer to other tasks. Naive use of BERT token embeddings, which Reimers and Gurevych (2019) identify as giving weak sentence similarity, motivated the next step. Their Sentence-BERT approach fine-tuned siamese BERT networks with classification and regression objectives, producing sentence embeddings that performed well on semantic textual similarity benchmarks. Encoders of this kind are built on BERT-style transformer backbones.
When to use it
Choose this approach when your inputs are sentences or short paragraphs, labelled data are limited, and the target is a semantic task such as similarity, semantic search, clustering, or text classification. It is most reasonable when a pre-trained encoder exists for your language or domain that is close to your text, because the pre-trained knowledge reduces the amount of labelled data needed. Avoid it when your text differs strongly from the encoder's pre-training corpus and no suitable domain encoder exists, such as highly technical clinical notes. Also avoid it when the task requires token-level predictions, such as named entity recognition, because pooling into one vector discards token positions. If strict latency or memory budgets rule out running a large encoder, a smaller encoder or a non-neural baseline should be considered. Key assumptions: the target sentences should resemble the encoder's pre-training domain, because a large domain gap is the main reason a frozen or lightly tuned representation fits poorly; the labelled pairs or examples should be representative of the inputs the system will receive in use; and the encoder's pooling method and maximum input length must suit how the task's texts are prepared, with longer documents chunked as described in the FAQ.
Limitations and considerations
- Sentence-level pooling discards token positions, so the approach is unsuitable for token-level tasks such as named entity recognition or span extraction.
- When the target domain is far from the pre-training distribution and no domain-adapted encoder exists, a frozen or lightly tuned representation may fit the task poorly.
- Large encoders, such as 768-dimensional BERT-based models, carry memory and latency costs that can be prohibitive on constrained hardware.
- Contrastive fine-tuning depends on carefully curated positive and negative sentence pairs, and building such pairs can be costly.
- Performance on semantic similarity benchmarks (STS) does not always predict performance on downstream task benchmarks, so similarity scores alone are weak evidence for a classification or retrieval application.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- With a strong pre-trained encoder, useful similarity and retrieval performance can be reached from a small labelled set, because only a light adjustment of the representation is needed.
- Fixed-length vectors allow cosine similarity to be computed directly, which makes large-scale nearest-neighbour retrieval practical.
- In a bi-encoder setup, document embeddings can be computed once and indexed offline, so query-time work is limited to encoding the query and comparing vectors.
- Multilingual encoders, such as paraphrase-multilingual-MiniLM, map several languages into one vector space, so cross-lingual comparison does not require a separate model for each language.
Applications
- Semantic search and document retrieval over large text collections.
- Duplicate and paraphrase detection, for example among community question-answering posts.
- Sentence-level clustering of short texts such as social media posts or survey responses.
- Text classification using embedding features with a small trained head, including comparing query embeddings with embeddings of label descriptions.
- Cross-lingual and multilingual semantic similarity using multilingual sentence encoders.
How it works
How it works
Choose an encoder whose pre-training matches your language and domain, for example Sentence-BERT, all-MiniLM, or the multilingual Universal Sentence Encoder. Larger encoders generally carry richer representations but cost more memory and compute. Domain-matched models, such as SciBERT-based encoders for scientific abstracts, can reduce the gap between pre-training text and your text, although a suitable domain-matched sentence encoder may not exist for every task.
Pass each input sentence through the encoder to obtain one fixed-length dense vector. Most modern sentence encoders pool token embeddings from a transformer backbone, commonly by mean pooling, to produce the sentence vector. The vector is intended to reflect semantic content rather than surface token overlap.
Formula
\mathbf{e}_i = \text{Encoder}(s_i) \in \mathbb{R}^dDefine an objective on top of the embeddings that matches the task: a cosine-similarity loss for semantic textual similarity, a contrastive or triplet loss for ranking, or cross-entropy for classification. With a frozen encoder only the task head is trained. In a siamese or bi-encoder setup, the encoder is trained jointly with the head, typically at a learning rate lower than that of the head; the exact values should be selected on a held-out validation set.
Decide how much of the encoder to update: none, only the top layers, or all layers, while training the task head on labelled examples. Begin with the frozen encoder and a linear probe as the baseline, since it shows how much the pre-trained vectors already provide, and unfreeze layers only if validation performance indicates a gain. A strong pre-trained encoder already yields useful vectors, so specialisation often requires only limited weight adjustment and a modest amount of labelled data. Updating all layers with an aggressive learning rate risks overwriting the pre-trained representation; the learning rate and the number of unfrozen layers should be chosen on a held-out validation set.
Use metrics that match the task: Spearman correlation for similarity, Mean Average Precision or NDCG for retrieval, and F1 or AUC for classification. Compare results with a zero-shot baseline, such as frozen embeddings with a linear probe, to measure the gain that fine-tuning actually contributes.
Common pitfalls
- Using a general-domain encoder for medical, legal, or scientific text without first checking whether a domain-specific encoder exists.
- Evaluating only on in-distribution similarity pairs and ignoring retrieval precision at rank k, which is closer to how such systems are used in practice.
- Fine-tuning all encoder layers at a high learning rate, which can cause catastrophic forgetting of the pre-trained representation.
- Confusing sentence embedding models with cross-encoder rerankers. Cross-encoders score a sentence pair jointly and are typically more accurate, but they cannot index a large corpus in advance.
- Assuming that higher embedding dimensionality improves task performance without checking this on your own validation data.
Frequently asked questions
When should I fine-tune the encoder versus keeping it frozen?
Keep the encoder frozen when labelled data are very scarce, since updating many weights risks overfitting. Update the top layers or the full encoder when you have enough labelled pairs to constrain the update and the target domain differs from the pre-training text. Use a frozen-encoder linear probe as the default baseline before unfreezing any layer. The right boundary depends on the task and should be checked on a held-out validation set, where the learning rate and the number of unfrozen layers are also selected.
What is the difference between a bi-encoder and a cross-encoder?
A bi-encoder processes each sentence independently and computes similarity via dot product or cosine, enabling fast large-scale retrieval. A cross-encoder processes sentence pairs jointly through the attention mechanism, yielding higher accuracy but requiring comparison of every query-document pair, which is too slow for large-scale retrieval. A common pattern is bi-encoder retrieval followed by cross-encoder reranking.
How many labelled examples do I need to fine-tune effectively?
There is no fixed number. Similarity tasks can benefit from a relatively small set of labelled pairs when the starting encoder is strong, while classification with many categories typically needs more examples per class. Zero-shot use of the frozen encoder is a useful baseline to compare against.
Which sentence embedding model should I choose?
The choice depends on language, domain, and speed requirements. all-MiniLM-L6-v2 is a common choice for general English where speed matters, and paraphrase-multilingual-MiniLM or LaBSE are common starting points for multilingual tasks. For scientific text, domain-specific encoders should be considered, but SPECTER is a document-level rather than sentence-level encoder, so it suits paper-level similarity better than sentence-level tasks. Benchmark candidate models on your own data before committing.
Can sentence embeddings handle long documents?
Most sentence encoders accept only a limited input length, so longer documents must be truncated or split into chunks whose embeddings are then aggregated, for example by averaging. Long-context transformer models exist for longer inputs but are not sentence encoders and need separate evaluation. Chunk overlap should be tested against retrieval quality on your data.
Related methods
Sources
Reimers, N. & Gurevych, I. (2019). Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks. Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing (EMNLP), 3982–3992.
Open publication referenceConneau, A., Kiela, D., Schwentz, H., Barrault, L. & Bordes, A. (2017). Supervised Learning of Universal Sentence Representations from Natural Language Inference Data. Proceedings of the 2017 Conference on Empirical Methods in Natural Language Processing (EMNLP), 670–680.
Open publication reference