upload
Browse files- 1_Pooling/config.json +7 -0
- README.md +137 -0
- config.json +23 -0
- config_sentence_transformers.json +7 -0
- modules.json +20 -0
- pytorch_model.bin +3 -0
- sentence_bert_config.json +4 -0
- special_tokens_map.json +1 -0
- tokenizer.json +0 -0
- tokenizer_config.json +1 -0
- vocab.txt +0 -0
1_Pooling/config.json
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"word_embedding_dimension": 768,
|
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-distilbert-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-distilbert-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-distilbert-cos-v5")
|
89 |
+
model = AutoModel.from_pretrained("sentence-transformers/msmarco-distilbert-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,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "old_models/msmarco-distilbert-base-v4/0_Transformer",
|
3 |
+
"activation": "gelu",
|
4 |
+
"architectures": [
|
5 |
+
"DistilBertModel"
|
6 |
+
],
|
7 |
+
"attention_dropout": 0.1,
|
8 |
+
"dim": 768,
|
9 |
+
"dropout": 0.1,
|
10 |
+
"hidden_dim": 3072,
|
11 |
+
"initializer_range": 0.02,
|
12 |
+
"max_position_embeddings": 512,
|
13 |
+
"model_type": "distilbert",
|
14 |
+
"n_heads": 12,
|
15 |
+
"n_layers": 6,
|
16 |
+
"pad_token_id": 0,
|
17 |
+
"qa_dropout": 0.1,
|
18 |
+
"seq_classif_dropout": 0.2,
|
19 |
+
"sinusoidal_pos_embds": false,
|
20 |
+
"tie_weights_": true,
|
21 |
+
"transformers_version": "4.7.0",
|
22 |
+
"vocab_size": 30522
|
23 |
+
}
|
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 |
+
}
|
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:e195dbed3e6acc34edb29780c48382b6984f258bcd964a0c4d1a042899023b55
|
3 |
+
size 265486777
|
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, "special_tokens_map_file": null, "name_or_path": "old_models/msmarco-distilbert-base-v4/0_Transformer"}
|
vocab.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|