nreimers commited on
Commit
6ad9e5f
1 Parent(s): 53a4791
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 millions of paraphrase sentences. 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
+ ```
config.json ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "RobertaModel"
4
+ ],
5
+ "attention_probs_dropout_prob": 0.1,
6
+ "bos_token_id": 0,
7
+ "eos_token_id": 2,
8
+ "gradient_checkpointing": false,
9
+ "hidden_act": "gelu",
10
+ "hidden_dropout_prob": 0.1,
11
+ "hidden_size": 768,
12
+ "initializer_range": 0.02,
13
+ "intermediate_size": 3072,
14
+ "layer_norm_eps": 1e-05,
15
+ "max_position_embeddings": 514,
16
+ "model_type": "roberta",
17
+ "num_attention_heads": 12,
18
+ "num_hidden_layers": 6,
19
+ "output_hidden_states": true,
20
+ "pad_token_id": 1,
21
+ "type_vocab_size": 1,
22
+ "vocab_size": 50265
23
+ }
merges.txt ADDED
The diff for this file is too large to render. See raw diff
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2aa37d61a123f825231675f8b4ad07705a1c031b89c9e197a694b7be5e573351
3
+ size 328515982
sentence_bert_config.json ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ {
2
+ "max_seq_length": 128
3
+ }
special_tokens_map.json ADDED
@@ -0,0 +1 @@
 
1
+ {"bos_token": "<s>", "eos_token": "</s>", "unk_token": "<unk>", "sep_token": "</s>", "pad_token": "<pad>", "cls_token": "<s>", "mask_token": {"content": "<mask>", "single_word": false, "lstrip": true, "rstrip": false, "normalized": false}}
tokenizer_config.json ADDED
@@ -0,0 +1 @@
 
1
+ {"add_special_tokens": false, "model_max_length": 512, "special_tokens_map_file": "/home/ukp-reimers/.cache/torch/transformers/4f4743e3f4fbeb763d116a0f2697f5e03117bd130711d90eaf795aeaeb7c4659.01d47f83d2e88283cc7f6be55eaef5d08d20297fc3bc1c1618ac15c35d1b97dd", "full_tokenizer_file": null}
vocab.json ADDED
The diff for this file is too large to render. See raw diff