nreimers commited on
Commit
89bd51f
1 Parent(s): c45acac
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,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "attention_probs_dropout_prob": 0.1,
3
+ "finetuning_task": null,
4
+ "hidden_act": "gelu",
5
+ "hidden_dropout_prob": 0.1,
6
+ "hidden_size": 1024,
7
+ "initializer_range": 0.02,
8
+ "intermediate_size": 4096,
9
+ "layer_norm_eps": 1e-12,
10
+ "max_position_embeddings": 512,
11
+ "num_attention_heads": 16,
12
+ "num_hidden_layers": 24,
13
+ "num_labels": 2,
14
+ "output_attentions": false,
15
+ "output_hidden_states": false,
16
+ "torchscript": false,
17
+ "type_vocab_size": 2,
18
+ "vocab_size": 30522
19
+ }
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:462db872f78905f0b36d98b70d2a5b6dd0b23c6ae85d68fa03ac6d3ee91b39e7
3
+ size 1340703974
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
+ {"unk_token": "[UNK]", "sep_token": "[SEP]", "pad_token": "[PAD]", "cls_token": "[CLS]", "mask_token": "[MASK]"}
vocab.txt ADDED
The diff for this file is too large to render. See raw diff