nreimers commited on
Commit
1bbe943
1 Parent(s): b6c24cd
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
+ ```
added_tokens.json ADDED
@@ -0,0 +1 @@
 
1
+ {}
config.json ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "roberta-large",
3
+ "architectures": [
4
+ "RobertaModel"
5
+ ],
6
+ "attention_probs_dropout_prob": 0.1,
7
+ "bos_token_id": 0,
8
+ "eos_token_id": 2,
9
+ "gradient_checkpointing": false,
10
+ "hidden_act": "gelu",
11
+ "hidden_dropout_prob": 0.1,
12
+ "hidden_size": 1024,
13
+ "initializer_range": 0.02,
14
+ "intermediate_size": 4096,
15
+ "layer_norm_eps": 1e-05,
16
+ "max_position_embeddings": 514,
17
+ "model_type": "roberta",
18
+ "num_attention_heads": 16,
19
+ "num_hidden_layers": 24,
20
+ "pad_token_id": 1,
21
+ "position_embedding_type": "absolute",
22
+ "type_vocab_size": 1,
23
+ "vocab_size": 50265
24
+ }
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:af41d20e942d508261a0f61e30e82d75e8a92740cb691a0d11f0a8025e49e4d4
3
+ size 1421534091
sentence_bert_config.json ADDED
@@ -0,0 +1,4 @@
 
 
 
 
1
+ {
2
+ "max_seq_length": 128,
3
+ "do_lower_case": true
4
+ }
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
+ {"unk_token": "<unk>", "bos_token": "<s>", "eos_token": "</s>", "add_prefix_space": false, "errors": "replace", "sep_token": "</s>", "cls_token": "<s>", "pad_token": "<pad>", "mask_token": "<mask>", "model_max_length": 512, "name_or_path": "roberta-large"}
vocab.json ADDED
The diff for this file is too large to render. See raw diff