Add new SentenceTransformer model.
Browse files- .gitattributes +1 -0
- 1_Pooling/config.json +7 -0
- README.md +189 -0
- config.json +26 -0
- config_sentence_transformers.json +7 -0
- modules.json +14 -0
- pytorch_model.bin +3 -0
- sentence_bert_config.json +4 -0
- special_tokens_map.json +7 -0
- tokenizer.json +0 -0
- tokenizer_config.json +16 -0
- vocab.txt +0 -0
.gitattributes
CHANGED
@@ -32,3 +32,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
35 |
+
pytorch_model.bin filter=lfs diff=lfs merge=lfs -text
|
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,189 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
pipeline_tag: sentence-similarity
|
3 |
+
tags:
|
4 |
+
- sentence-transformers
|
5 |
+
- feature-extraction
|
6 |
+
- sentence-similarity
|
7 |
+
- transformers
|
8 |
+
---
|
9 |
+
|
10 |
+
# msmarco-bert-base-dot-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 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-bert-base-dot-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 |
+
print("Query:", query)
|
46 |
+
for doc, score in doc_score_pairs:
|
47 |
+
print(score, doc)
|
48 |
+
```
|
49 |
+
|
50 |
+
|
51 |
+
## Usage (HuggingFace Transformers)
|
52 |
+
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.
|
53 |
+
|
54 |
+
```python
|
55 |
+
from transformers import AutoTokenizer, AutoModel
|
56 |
+
import torch
|
57 |
+
|
58 |
+
#Mean Pooling - Take attention mask into account for correct averaging
|
59 |
+
def mean_pooling(model_output, attention_mask):
|
60 |
+
token_embeddings = model_output.last_hidden_state
|
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 |
+
return embeddings
|
78 |
+
|
79 |
+
|
80 |
+
# Sentences we want sentence embeddings for
|
81 |
+
query = "How many people live in London?"
|
82 |
+
docs = ["Around 9 Million people live in London", "London is known for its financial district"]
|
83 |
+
|
84 |
+
# Load model from HuggingFace Hub
|
85 |
+
tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/msmarco-bert-base-dot-v5")
|
86 |
+
model = AutoModel.from_pretrained("sentence-transformers/msmarco-bert-base-dot-v5")
|
87 |
+
|
88 |
+
#Encode query and docs
|
89 |
+
query_emb = encode(query)
|
90 |
+
doc_emb = encode(docs)
|
91 |
+
|
92 |
+
#Compute dot score between query and all document embeddings
|
93 |
+
scores = torch.mm(query_emb, doc_emb.transpose(0, 1))[0].cpu().tolist()
|
94 |
+
|
95 |
+
#Combine docs & scores
|
96 |
+
doc_score_pairs = list(zip(docs, scores))
|
97 |
+
|
98 |
+
#Sort by decreasing score
|
99 |
+
doc_score_pairs = sorted(doc_score_pairs, key=lambda x: x[1], reverse=True)
|
100 |
+
|
101 |
+
#Output passages & scores
|
102 |
+
print("Query:", query)
|
103 |
+
for doc, score in doc_score_pairs:
|
104 |
+
print(score, doc)
|
105 |
+
```
|
106 |
+
|
107 |
+
## Technical Details
|
108 |
+
|
109 |
+
In the following some technical details how this model must be used:
|
110 |
+
|
111 |
+
| Setting | Value |
|
112 |
+
| --- | :---: |
|
113 |
+
| Dimensions | 768 |
|
114 |
+
| Max Sequence Length | 512 |
|
115 |
+
| Produces normalized embeddings | No |
|
116 |
+
| Pooling-Method | Mean pooling |
|
117 |
+
| Suitable score functions | dot-product (e.g. `util.dot_score`) |
|
118 |
+
|
119 |
+
|
120 |
+
## Evaluation Results
|
121 |
+
|
122 |
+
<!--- Describe how your model was evaluated -->
|
123 |
+
|
124 |
+
For an automated evaluation of this model, see the *Sentence Embeddings Benchmark*: [https://seb.sbert.net](https://seb.sbert.net?model_name=msmarco-bert-base-base-dot-v5)
|
125 |
+
|
126 |
+
|
127 |
+
## Training
|
128 |
+
|
129 |
+
See `train_script.py` in this repository for the used training script.
|
130 |
+
|
131 |
+
|
132 |
+
|
133 |
+
The model was trained with the parameters:
|
134 |
+
|
135 |
+
**DataLoader**:
|
136 |
+
|
137 |
+
`torch.utils.data.dataloader.DataLoader` of length 7858 with parameters:
|
138 |
+
```
|
139 |
+
{'batch_size': 64, 'sampler': 'torch.utils.data.sampler.RandomSampler', 'batch_sampler': 'torch.utils.data.sampler.BatchSampler'}
|
140 |
+
```
|
141 |
+
|
142 |
+
**Loss**:
|
143 |
+
|
144 |
+
`sentence_transformers.losses.MarginMSELoss.MarginMSELoss`
|
145 |
+
|
146 |
+
Parameters of the fit()-Method:
|
147 |
+
```
|
148 |
+
{
|
149 |
+
"callback": null,
|
150 |
+
"epochs": 30,
|
151 |
+
"evaluation_steps": 0,
|
152 |
+
"evaluator": "NoneType",
|
153 |
+
"max_grad_norm": 1,
|
154 |
+
"optimizer_class": "<class 'transformers.optimization.AdamW'>",
|
155 |
+
"optimizer_params": {
|
156 |
+
"lr": 1e-05
|
157 |
+
},
|
158 |
+
"scheduler": "WarmupLinear",
|
159 |
+
"steps_per_epoch": null,
|
160 |
+
"warmup_steps": 10000,
|
161 |
+
"weight_decay": 0.01
|
162 |
+
}
|
163 |
+
```
|
164 |
+
|
165 |
+
|
166 |
+
## Full Model Architecture
|
167 |
+
```
|
168 |
+
SentenceTransformer(
|
169 |
+
(0): Transformer({'max_seq_length': 512, 'do_lower_case': False}) with Transformer model: bert-base-uncased
|
170 |
+
(1): Pooling({'word_embedding_dimension': 768, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': True, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False})
|
171 |
+
)
|
172 |
+
```
|
173 |
+
|
174 |
+
## Citing & Authors
|
175 |
+
|
176 |
+
This model was trained by [sentence-transformers](https://www.sbert.net/).
|
177 |
+
|
178 |
+
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):
|
179 |
+
```bibtex
|
180 |
+
@inproceedings{reimers-2019-sentence-bert,
|
181 |
+
title = "Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks",
|
182 |
+
author = "Reimers, Nils and Gurevych, Iryna",
|
183 |
+
booktitle = "Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing",
|
184 |
+
month = "11",
|
185 |
+
year = "2019",
|
186 |
+
publisher = "Association for Computational Linguistics",
|
187 |
+
url = "http://arxiv.org/abs/1908.10084",
|
188 |
+
}
|
189 |
+
```
|
config.json
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "/home/ec2-user/.cache/torch/sentence_transformers/sentence-transformers_msmarco-bert-base-dot-v5/",
|
3 |
+
"architectures": [
|
4 |
+
"BertModel"
|
5 |
+
],
|
6 |
+
"attention_probs_dropout_prob": 0.1,
|
7 |
+
"classifier_dropout": null,
|
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-12,
|
15 |
+
"max_position_embeddings": 512,
|
16 |
+
"model_type": "bert",
|
17 |
+
"num_attention_heads": 12,
|
18 |
+
"num_hidden_layers": 4,
|
19 |
+
"pad_token_id": 0,
|
20 |
+
"position_embedding_type": "absolute",
|
21 |
+
"torch_dtype": "float32",
|
22 |
+
"transformers_version": "4.25.1",
|
23 |
+
"type_vocab_size": 2,
|
24 |
+
"use_cache": true,
|
25 |
+
"vocab_size": 30522
|
26 |
+
}
|
config_sentence_transformers.json
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"__version__": {
|
3 |
+
"sentence_transformers": "2.0.0",
|
4 |
+
"transformers": "4.6.1",
|
5 |
+
"pytorch": "1.8.1"
|
6 |
+
}
|
7 |
+
}
|
modules.json
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
]
|
pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:06c03a2ecfbd35a2e5253a8481b434340b8745df6ce0ee600652b381995614c3
|
3 |
+
size 211145033
|
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,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cls_token": "[CLS]",
|
3 |
+
"mask_token": "[MASK]",
|
4 |
+
"pad_token": "[PAD]",
|
5 |
+
"sep_token": "[SEP]",
|
6 |
+
"unk_token": "[UNK]"
|
7 |
+
}
|
tokenizer.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
tokenizer_config.json
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cls_token": "[CLS]",
|
3 |
+
"do_basic_tokenize": true,
|
4 |
+
"do_lower_case": true,
|
5 |
+
"mask_token": "[MASK]",
|
6 |
+
"model_max_length": 512,
|
7 |
+
"name_or_path": "/home/ec2-user/.cache/torch/sentence_transformers/sentence-transformers_msmarco-bert-base-dot-v5/",
|
8 |
+
"never_split": null,
|
9 |
+
"pad_token": "[PAD]",
|
10 |
+
"sep_token": "[SEP]",
|
11 |
+
"special_tokens_map_file": "/bos/tmp0/luyug/outputs/condenser/models/l2-s6-km-L128-e8-lr1e-4-b256/special_tokens_map.json",
|
12 |
+
"strip_accents": null,
|
13 |
+
"tokenize_chinese_chars": true,
|
14 |
+
"tokenizer_class": "BertTokenizer",
|
15 |
+
"unk_token": "[UNK]"
|
16 |
+
}
|
vocab.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|