nreimers commited on
Commit
15af0e8
1 Parent(s): 04fd60e
1_Pooling/config.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "word_embedding_dimension": 384,
3
+ "pooling_mode_cls_token": false,
4
+ "pooling_mode_mean_tokens": true,
5
+ "pooling_mode_max_tokens": false,
6
+ "pooling_mode_mean_sqrt_len_tokens": false
7
+ }
README.md ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ pipeline_tag: sentence-similarity
3
+ tags:
4
+ - sentence-transformers
5
+ - feature-extraction
6
+ - sentence-similarity
7
+ - transformers
8
+ ---
9
+
10
+ # msmarco-MiniLM-L6-cos-v5
11
+ This is a [sentence-transformers](https://www.SBERT.net) model: It maps sentences & paragraphs to a 768 dimensional dense vector space and was designed for **semantic search**. It has been trained on 500k (query, answer) pairs from the [MS MARCO Passages dataset](https://github.com/microsoft/MSMARCO-Passage-Ranking). For an introduction to semantic search, have a look at: [SBERT.net - Semantic Search](https://www.sbert.net/examples/applications/semantic-search/README.html)
12
+
13
+
14
+ ## Usage (Sentence-Transformers)
15
+ Using this model becomes easy when you have [sentence-transformers](https://www.SBERT.net) installed:
16
+
17
+ ```
18
+ pip install -U sentence-transformers
19
+ ```
20
+
21
+ Then you can use the model like this:
22
+ ```python
23
+ from sentence_transformers import SentenceTransformer, util
24
+
25
+ query = "How many people live in London?"
26
+ docs = ["Around 9 Million people live in London", "London is known for its financial district"]
27
+
28
+ #Load the model
29
+ model = SentenceTransformer('sentence-transformers/msmarco-MiniLM-L6-cos-v5')
30
+
31
+ #Encode query and documents
32
+ query_emb = model.encode(query)
33
+ doc_emb = model.encode(docs)
34
+
35
+ #Compute dot score between query and all document embeddings
36
+ scores = util.dot_score(query_emb, doc_emb)[0].cpu().tolist()
37
+
38
+ #Combine docs & scores
39
+ doc_score_pairs = list(zip(docs, scores))
40
+
41
+ #Sort by decreasing score
42
+ doc_score_pairs = sorted(doc_score_pairs, key=lambda x: x[1], reverse=True)
43
+
44
+ #Output passages & scores
45
+ for doc, score in doc_score_pairs:
46
+ print(score, doc)
47
+ ```
48
+
49
+
50
+ ## Usage (HuggingFace Transformers)
51
+ Without [sentence-transformers](https://www.SBERT.net), you can use the model like this: First, you pass your input through the transformer model, then you have to apply the correct pooling-operation on-top of the contextualized word embeddings.
52
+
53
+ ```python
54
+ from transformers import AutoTokenizer, AutoModel
55
+ import torch
56
+ import torch.nn.functional as F
57
+
58
+ #Mean Pooling - Take average of all tokens
59
+ def mean_pooling(model_output, attention_mask):
60
+ token_embeddings = model_output.last_hidden_state #First element of model_output contains all token embeddings
61
+ input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
62
+ return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
63
+
64
+
65
+ #Encode text
66
+ def encode(texts):
67
+ # Tokenize sentences
68
+ encoded_input = tokenizer(texts, padding=True, truncation=True, return_tensors='pt')
69
+
70
+ # Compute token embeddings
71
+ with torch.no_grad():
72
+ model_output = model(**encoded_input, return_dict=True)
73
+
74
+ # Perform pooling
75
+ embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
76
+
77
+ # Normalize embeddings
78
+ embeddings = F.normalize(embeddings, p=2, dim=1)
79
+
80
+ return embeddings
81
+
82
+
83
+ # Sentences we want sentence embeddings for
84
+ query = "How many people live in London?"
85
+ docs = ["Around 9 Million people live in London", "London is known for its financial district"]
86
+
87
+ # Load model from HuggingFace Hub
88
+ tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/msmarco-MiniLM-L6-cos-v5")
89
+ model = AutoModel.from_pretrained("sentence-transformers/msmarco-MiniLM-L6-cos-v5")
90
+
91
+ #Encode query and docs
92
+ query_emb = encode(query)
93
+ doc_emb = encode(docs)
94
+
95
+ #Compute dot score between query and all document embeddings
96
+ scores = torch.mm(query_emb, doc_emb.transpose(0, 1))[0].cpu().tolist()
97
+
98
+ #Combine docs & scores
99
+ doc_score_pairs = list(zip(docs, scores))
100
+
101
+ #Sort by decreasing score
102
+ doc_score_pairs = sorted(doc_score_pairs, key=lambda x: x[1], reverse=True)
103
+
104
+ #Output passages & scores
105
+ for doc, score in doc_score_pairs:
106
+ print(score, doc)
107
+ ```
108
+
109
+ ## Technical Details
110
+
111
+ In the following some technical details how this model must be used:
112
+
113
+ | Setting | Value |
114
+ | --- | :---: |
115
+ | Dimensions | 768 |
116
+ | Produces normalized embeddings | Yes |
117
+ | Pooling-Method | Mean pooling |
118
+ | Suitable score functions | dot-product (`util.dot_score`), cosine-similarity (`util.cos_sim`), or euclidean distance |
119
+
120
+ Note: When loaded with `sentence-transformers`, this model produces normalized embeddings with length 1. In that case, dot-product and cosine-similarity are equivalent. dot-product is preferred as it is faster. Euclidean distance is proportional to dot-product and can also be used.
121
+
122
+ ## Citing & Authors
123
+
124
+ This model was trained by [sentence-transformers](https://www.sbert.net/).
125
+
126
+ 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):
127
+ ```bibtex
128
+ @inproceedings{reimers-2019-sentence-bert,
129
+ title = "Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks",
130
+ author = "Reimers, Nils and Gurevych, Iryna",
131
+ booktitle = "Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing",
132
+ month = "11",
133
+ year = "2019",
134
+ publisher = "Association for Computational Linguistics",
135
+ url = "http://arxiv.org/abs/1908.10084",
136
+ }
137
+ ```
config.json ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "old_models/msmarco-MiniLM-L-6-v3/0_Transformer",
3
+ "architectures": [
4
+ "BertModel"
5
+ ],
6
+ "attention_probs_dropout_prob": 0.1,
7
+ "gradient_checkpointing": false,
8
+ "hidden_act": "gelu",
9
+ "hidden_dropout_prob": 0.1,
10
+ "hidden_size": 384,
11
+ "initializer_range": 0.02,
12
+ "intermediate_size": 1536,
13
+ "layer_norm_eps": 1e-12,
14
+ "max_position_embeddings": 512,
15
+ "model_type": "bert",
16
+ "num_attention_heads": 12,
17
+ "num_hidden_layers": 6,
18
+ "pad_token_id": 0,
19
+ "position_embedding_type": "absolute",
20
+ "transformers_version": "4.7.0",
21
+ "type_vocab_size": 2,
22
+ "use_cache": true,
23
+ "vocab_size": 30522
24
+ }
config_sentence_transformers.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "__version__": {
3
+ "sentence_transformers": "2.0.0",
4
+ "transformers": "4.7.0",
5
+ "pytorch": "1.9.0+cu102"
6
+ }
7
+ }
flax_model.msgpack ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4ce670108f3abe4a1513aa973c696e273397db899a983deecdd86a987f9a63dc
3
+ size 90856603
modules.json ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "idx": 0,
4
+ "name": "0",
5
+ "path": "",
6
+ "type": "sentence_transformers.models.Transformer"
7
+ },
8
+ {
9
+ "idx": 1,
10
+ "name": "1",
11
+ "path": "1_Pooling",
12
+ "type": "sentence_transformers.models.Pooling"
13
+ },
14
+ {
15
+ "idx": 2,
16
+ "name": "2",
17
+ "path": "2_Normalize",
18
+ "type": "sentence_transformers.models.Normalize"
19
+ }
20
+ ]
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5e3a29b2fc7bce0f6b0bdd35dcd6e6d1c1dd5fc191561d0b9c5d3aadf3891e0b
3
+ size 90895153
sentence_bert_config.json ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ {
2
+ "max_seq_length": 512,
3
+ "do_lower_case": false
4
+ }
special_tokens_map.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"unk_token": "[UNK]", "sep_token": "[SEP]", "pad_token": "[PAD]", "cls_token": "[CLS]", "mask_token": "[MASK]"}
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"do_lower_case": true, "unk_token": "[UNK]", "sep_token": "[SEP]", "pad_token": "[PAD]", "cls_token": "[CLS]", "mask_token": "[MASK]", "tokenize_chinese_chars": true, "strip_accents": null, "model_max_length": 512, "name_or_path": "old_models/msmarco-MiniLM-L-6-v3/0_Transformer", "do_basic_tokenize": true, "never_split": null, "special_tokens_map_file": "old_models/msmarco-MiniLM-L-6-v3/0_Transformer/special_tokens_map.json"}
vocab.txt ADDED
The diff for this file is too large to render. See raw diff