Problemcore
Sentence embedding
Also called sentence representation learning.
Map a sentence or a short text to one fixed-size vector so that the cosine of two vectors reflects the semantic similarity of the two texts.
Sentence embedding is the problem of mapping a sentence, or any short text, to one vector of fixed size, so that texts with similar meanings get vectors with a high cosine, and so that the vectors serve as features for classification and as keys for retrieval. There is no single correct answer: a method is judged by measurement, mainly by the rank correlation of its cosines with human similarity scores. Averaging word vectors is a strong baseline, and trained encoders such as Sentence-BERT and SimCSE measure best today.
In plain words
Computers compare vectors easily, but they cannot compare the meaning of two sentences directly. A sentence embedding gives every sentence a list of numbers of the same length, for example 768 numbers. The goal is that "A man is playing a guitar" and "Someone plays an instrument" get lists that point in nearly the same direction, while "The stock market fell" points elsewhere. Once every sentence has such a list, finding similar sentences, grouping them, or training a classifier on them is fast and simple. The hard part is to find a method that makes the directions agree with what people judge as similar.
Definition
Input. A set of texts, each a sequence of tokens, and possibly training data: a large unlabeled corpus, or labeled pairs (similar or not, entailment or contradiction).
Output. A function from texts to , for a fixed , that does not depend on other texts at test time.
The question. The similarity of texts and is scored as . On a test set of pairs with human scores, for example from 0 (unrelated) to 5 (the same meaning) in the STS benchmark of Cer et al. 2017, the scores of should have a high rank correlation (Spearman) or linear correlation (Pearson) with the human scores. In the transfer form, a simple classifier, such as logistic regression, trained on should predict labels such as sentiment well.
Known complexity. The question has no exact solution to compute; it is an empirical problem. What can be stated exactly is the cost of using a given : evaluations of for texts, per pair score, and per exact nearest neighbor query.
Variants
- Unsupervised or supervised. Unsupervised methods use only raw text and word frequencies; supervised ones use labeled pairs, often natural language inference data.
- Symmetric or asymmetric. Similarity of two sentences is symmetric. Retrieval of passages for a question is asymmetric, and often uses two encoders, as in dense passage retrieval.
- Length. Sentences, paragraphs and whole documents. Paragraph vectors were designed for documents.
- Cross-lingual. Texts in different languages with the same meaning should get close vectors, which allows mining of parallel sentences (see sentence alignment).
- Pair scorers. A cross-encoder reads both texts at once and returns a score. It does not give a vector per text, so it is not a solution of this problem, but it is the usual upper reference for the quality of pair scores.
Examples
Take the 2-dimensional word vectors dog , man and bites , and let be the plain average. Then and , so their cosine is 1, although the meanings differ. With cat , , whose cosine with is . The average sees which words occur, not their order or roles.
A measured example (Reimers and Gurevych 2019): as the Spearman correlation times 100, averaged over seven STS data sets with no STS training, average GloVe vectors gave 61.32, mean-pooled BERT vectors 54.81, the BERT CLS vector 29.19, and Sentence-BERT trained on NLI 74.89. A large pretrained network used without training for this problem did worse than the average of static word vectors.
Known algorithms
| method | training data | how is computed |
|---|---|---|
| average of word vectors | none beyond the word vectors | mean of the word vectors |
| tf-idf weighted average | document frequencies | mean with IDF weights |
| SIF | word frequencies | mean with weights , minus one common direction |
| Paragraph vector | unlabeled corpus | gradient steps per new text |
| Skip-thought (Kiros et al. 2015) | ordered sentences | an encoder trained to predict the neighbor sentences |
| InferSent (Conneau et al. 2017) | NLI pairs | a BiLSTM with max pooling |
| Sentence-BERT | NLI pairs, then STS | a fine-tuned BERT with mean pooling |
| SimCSE | raw sentences, or NLI pairs | a BERT trained with a contrastive loss |
Measured results (all measurements): SIF beat many trained encoders of its time on STS. On the average of seven STS tasks, Gao et al. 2021 reported 76.25 for unsupervised SimCSE and 81.57 for supervised SimCSE, both with BERT-base, above the 74.89 of SBERT-NLI-base. Word mover's distance compares two texts without one vector per text, at a much higher cost per pair.
| Algorithm | Time | Space | Properties | Code |
|---|---|---|---|---|
| SimCSE | O(n^2 d) worst | O(n^2 + n d) extra | randomizednot exactofflineiterativenot supervised | 1 language |
Bounds
- Order (theorem). Any that is a function of the multiset of words, such as every weighted average, gives the same vector to two permutations of one sentence, so it cannot separate "dog bites man" from "man bites dog".
- Rank (theorem). For texts with unit vectors in , the matrix of all cosines is the Gram matrix with . It is positive semidefinite, has 1 on the diagonal, and has rank at most . A target similarity matrix that lacks these properties cannot be matched exactly in any dimension , and a matrix of rank more than cannot be matched in dimension .
- Cost (theorem). With an embedding, scoring all pairs of texts needs encoder passes and dot products. A pair scorer needs passes. For , Reimers and Gurevych estimated about 65 hours for BERT as a cross-encoder against about 5 seconds for SBERT (a measurement).
Reductions
- Most similar text to nearest neighbor search. For unit vectors, , so the text with the highest cosine is the nearest by Euclidean distance and the one with the largest dot product. Retrieval by cosine therefore reduces to nearest neighbor search or maximum inner product search, and approximate indexes such as HNSW and product quantization apply.
- Clustering. Grouping texts by meaning reduces to k-means or agglomerative clustering on the vectors.
- Classification. A text classification task reduces to a linear classifier on the vectors, which is how the transfer form of the problem is measured.
Applications
- Semantic search: find the passages or questions closest to a query.
- Finding duplicate questions and paraphrases among many texts.
- Clustering of texts by topic.
- Mining parallel sentences from texts in two languages.
- Features for small classifiers when labeled data is scarce.
- The retrieval step of systems that answer questions from a collection of documents.
References
- Daniel Cer, Mona Diab, Eneko Agirre, Iñigo Lopez-Gazpio and Lucia Specia, SemEval-2017 Task 1, Semantic textual similarity multilingual and crosslingual focused evaluation, SemEval-2017, pages 1 to 14, the STS benchmark, its scale from 0 to 5, and the Pearson correlation used to score systems
- Arora, Liang and Ma 2017, the weighted average baseline and its comparison with trained encoders on the textual similarity tasks
- Alexis Conneau, Douwe Kiela, Holger Schwenk, Loïc Barrault and Antoine Bordes, Supervised learning of universal sentence representations from natural language inference data, EMNLP 2017, pages 670 to 680, the transfer evaluation of sentence vectors with a classifier on top, and the InferSent encoder
- Nils Reimers and Iryna Gurevych, Sentence-BERT, Sentence embeddings using siamese BERT-networks, EMNLP-IJCNLP 2019, pages 3982 to 3992, the STS comparison of average GloVe vectors, plain BERT vectors and SBERT, and the cost of the cross-encoder on all pairs
- Le and Mikolov 2014, the paragraph vector models
- Tianyu Gao, Xingcheng Yao and Danqi Chen, SimCSE, Simple contrastive learning of sentence embeddings, EMNLP 2021, pages 6894 to 6910, the unsupervised and supervised contrastive objectives and the STS averages
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag 83BE7H Edit this page History Report an error Markdown