nreimers
commited on
Commit
•
cdd99d4
1
Parent(s):
4a24972
update
Browse files
README.md
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Sentence Embeddings Models trained on Paraphrases
|
2 |
+
This model is from the [sentence-transformers](https://github.com/UKPLab/sentence-transformers)-repository. It was trained on SNLI + MultiNLI and on STS benchmark dataset. Further details on SBERT can be found in the paper: [Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks](https://arxiv.org/abs/1908.10084)
|
3 |
+
|
4 |
+
## Usage (HuggingFace Models Repository)
|
5 |
+
|
6 |
+
You can use the model directly from the model repository to compute sentence embeddings:
|
7 |
+
```python
|
8 |
+
from transformers import AutoTokenizer, AutoModel
|
9 |
+
import torch
|
10 |
+
|
11 |
+
|
12 |
+
#Mean Pooling - Take attention mask into account for correct averaging
|
13 |
+
def mean_pooling(model_output, attention_mask):
|
14 |
+
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
|
15 |
+
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
16 |
+
sum_embeddings = torch.sum(token_embeddings * input_mask_expanded, 1)
|
17 |
+
sum_mask = torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
18 |
+
return sum_embeddings / sum_mask
|
19 |
+
|
20 |
+
|
21 |
+
|
22 |
+
#Sentences we want sentence embeddings for
|
23 |
+
sentences = ['This framework generates embeddings for each input sentence',
|
24 |
+
'Sentences are passed as a list of string.',
|
25 |
+
'The quick brown fox jumps over the lazy dog.']
|
26 |
+
|
27 |
+
#Load AutoModel from huggingface model repository
|
28 |
+
tokenizer = AutoTokenizer.from_pretrained("model_name")
|
29 |
+
model = AutoModel.from_pretrained("model_name")
|
30 |
+
|
31 |
+
#Tokenize sentences
|
32 |
+
encoded_input = tokenizer(sentences, padding=True, truncation=True, max_length=128, return_tensors='pt')
|
33 |
+
|
34 |
+
#Compute token embeddings
|
35 |
+
with torch.no_grad():
|
36 |
+
model_output = model(**encoded_input)
|
37 |
+
|
38 |
+
#Perform pooling. In this case, mean pooling
|
39 |
+
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
|
40 |
+
```
|
41 |
+
|
42 |
+
## Usage (Sentence-Transformers)
|
43 |
+
Using this model becomes more convenient when you have [sentence-transformers](https://github.com/UKPLab/sentence-transformers) installed:
|
44 |
+
```
|
45 |
+
pip install -U sentence-transformers
|
46 |
+
```
|
47 |
+
|
48 |
+
Then you can use the model like this:
|
49 |
+
```python
|
50 |
+
from sentence_transformers import SentenceTransformer
|
51 |
+
model = SentenceTransformer('model_name')
|
52 |
+
sentences = ['This framework generates embeddings for each input sentence',
|
53 |
+
'Sentences are passed as a list of string.',
|
54 |
+
'The quick brown fox jumps over the lazy dog.']
|
55 |
+
sentence_embeddings = model.encode(sentences)
|
56 |
+
|
57 |
+
print("Sentence embeddings:")
|
58 |
+
print(sentence_embeddings)
|
59 |
+
```
|
60 |
+
|
61 |
+
|
62 |
+
## Citing & Authors
|
63 |
+
If you find this model helpful, feel free to cite our publication [Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks](https://arxiv.org/abs/1908.10084):
|
64 |
+
```
|
65 |
+
@inproceedings{reimers-2019-sentence-bert,
|
66 |
+
title = "Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks",
|
67 |
+
author = "Reimers, Nils and Gurevych, Iryna",
|
68 |
+
booktitle = "Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing",
|
69 |
+
month = "11",
|
70 |
+
year = "2019",
|
71 |
+
publisher = "Association for Computational Linguistics",
|
72 |
+
url = "http://arxiv.org/abs/1908.10084",
|
73 |
+
}
|
74 |
+
```
|