davidmezzetti
commited on
Commit
•
8c124a4
1
Parent(s):
f2d8356
Initial version
Browse files- 1_Pooling/config.json +7 -0
- README.md +149 -0
- added_tokens.json +7 -0
- config.json +25 -0
- config_sentence_transformers.json +7 -0
- modules.json +14 -0
- pytorch_model.bin +3 -0
- sentence_bert_config.json +4 -0
- similarity_evaluation_results.csv +2 -0
- special_tokens_map.json +7 -0
- tokenizer.json +0 -0
- tokenizer_config.json +58 -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,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
pipeline_tag: sentence-similarity
|
3 |
+
tags:
|
4 |
+
- sentence-transformers
|
5 |
+
- feature-extraction
|
6 |
+
- sentence-similarity
|
7 |
+
- transformers
|
8 |
+
language: en
|
9 |
+
license: apache-2.0
|
10 |
+
---
|
11 |
+
|
12 |
+
# PubMedBERT Embeddings
|
13 |
+
|
14 |
+
This is a [PubMedBERT-base](https://huggingface.co/microsoft/BiomedNLP-PubMedBERT-base-uncased-abstract-fulltext) model fined-tuned using [sentence-transformers](https://www.SBERT.net). It maps sentences & paragraphs to a 768 dimensional dense vector space and can be used for tasks like clustering or semantic search. The training dataset was generated using a random sample of [PubMed](https://pubmed.ncbi.nlm.nih.gov/) title-abstract pairs along with similar title pairs.
|
15 |
+
|
16 |
+
PubMedBERT Embeddings produces higher quality embeddings than generalized models for medical literature. Further fine-tuning for a medical subdomain will result in even better performance.
|
17 |
+
|
18 |
+
## Usage (txtai)
|
19 |
+
|
20 |
+
This model can be used to build embeddings databases with [txtai](https://github.com/neuml/txtai) for semantic search and/or as a knowledge source for retrieval augmented generation (RAG).
|
21 |
+
|
22 |
+
```python
|
23 |
+
import txtai
|
24 |
+
|
25 |
+
embeddings = txtai.Embeddings(path="neuml/pubmedbert-base-embeddings", content=True)
|
26 |
+
embeddings.index(documents())
|
27 |
+
|
28 |
+
# Run a query
|
29 |
+
embeddings.search("query to run")
|
30 |
+
```
|
31 |
+
|
32 |
+
## Usage (Sentence-Transformers)
|
33 |
+
|
34 |
+
Alternatively, the model can be loaded with [sentence-transformers](https://www.SBERT.net).
|
35 |
+
|
36 |
+
```python
|
37 |
+
from sentence_transformers import SentenceTransformer
|
38 |
+
sentences = ["This is an example sentence", "Each sentence is converted"]
|
39 |
+
|
40 |
+
model = SentenceTransformer("neuml/pubmedbert-base-embeddings")
|
41 |
+
embeddings = model.encode(sentences)
|
42 |
+
print(embeddings)
|
43 |
+
```
|
44 |
+
|
45 |
+
## Usage (Hugging Face Transformers)
|
46 |
+
|
47 |
+
The model can also be used directly with Transformers.
|
48 |
+
|
49 |
+
```python
|
50 |
+
from transformers import AutoTokenizer, AutoModel
|
51 |
+
import torch
|
52 |
+
|
53 |
+
# Mean Pooling - Take attention mask into account for correct averaging
|
54 |
+
def meanpooling(output, mask):
|
55 |
+
embeddings = output[0] # First element of model_output contains all token embeddings
|
56 |
+
mask = mask.unsqueeze(-1).expand(embeddings.size()).float()
|
57 |
+
return torch.sum(embeddings * mask, 1) / torch.clamp(mask.sum(1), min=1e-9)
|
58 |
+
|
59 |
+
# Sentences we want sentence embeddings for
|
60 |
+
sentences = ['This is an example sentence', 'Each sentence is converted']
|
61 |
+
|
62 |
+
# Load model from HuggingFace Hub
|
63 |
+
tokenizer = AutoTokenizer.from_pretrained("neuml/pubmedbert-base-embeddings")
|
64 |
+
model = AutoModel.from_pretrained("neuml/pubmedbert-base-embeddings")
|
65 |
+
|
66 |
+
# Tokenize sentences
|
67 |
+
inputs = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
|
68 |
+
|
69 |
+
# Compute token embeddings
|
70 |
+
with torch.no_grad():
|
71 |
+
output = model(**inputs)
|
72 |
+
|
73 |
+
# Perform pooling. In this case, mean pooling.
|
74 |
+
embeddings = meanpooling(output, inputs['attention_mask'])
|
75 |
+
|
76 |
+
print("Sentence embeddings:")
|
77 |
+
print(embeddings)
|
78 |
+
```
|
79 |
+
|
80 |
+
## Evaluation Results
|
81 |
+
|
82 |
+
Performance of this model compared to the top base models on the [MTEB leaderboard](https://huggingface.co/spaces/mteb/leaderboard) is shown below. A popular smaller model was also evaluated along with the most downloaded PubMed similarity model on the Hugging Face Hub.
|
83 |
+
|
84 |
+
The following datasets were used to evaluate model performance.
|
85 |
+
|
86 |
+
- [PubMed QA](https://huggingface.co/datasets/pubmed_qa)
|
87 |
+
- Subset: pqa_labeled, Split: train, Pair: (question, long_answer)
|
88 |
+
- [PubMed Subset](https://huggingface.co/datasets/zxvix/pubmed_subset_new)
|
89 |
+
- Split: test, Pair: (title, text)
|
90 |
+
- [PubMed Summary](https://huggingface.co/datasets/scientific_papers)
|
91 |
+
- Subset: pubmed, Split: validation, Pair: (article, abstract)
|
92 |
+
|
93 |
+
Evaluation results are shown below. The [Pearson correlation coefficient](https://en.wikipedia.org/wiki/Pearson_correlation_coefficient) is used as the evaluation metric.
|
94 |
+
|
95 |
+
| Model | PubMed QA | PubMed Subset | PubMed Summary | Average |
|
96 |
+
| ----------------------------------------------------------------------------- | --------- | ------------- | -------------- | --------- |
|
97 |
+
| [all-MiniLM-L6-v2](https://hf.co/sentence-transformers/all-MiniLM-L6-v2) | 90.40 | 95.86 | 94.07 | 93.44 |
|
98 |
+
| [bge-base-en-v1.5](https://hf.co/BAAI/bge-large-en-v1.5) | 91.02 | 95.60 | 94.49 | 93.70 |
|
99 |
+
| [gte-base](https://hf.co/thenlper/gte-base) | 92.97 | 96.83 | 96.24 | 95.35 |
|
100 |
+
| [pubmedbert-base-embeddings](https://hf.co/neuml/pubmedbert-base-embeddings) | **93.27** | **97.07** | **96.58** | **95.64** |
|
101 |
+
| [S-PubMedBert-MS-MARCO](https://hf.co/pritamdeka/S-PubMedBert-MS-MARCO) | 90.86 | 93.33 | 93.54 | 92.58 |
|
102 |
+
|
103 |
+
## Training
|
104 |
+
|
105 |
+
The model was trained with the parameters:
|
106 |
+
|
107 |
+
**DataLoader**:
|
108 |
+
|
109 |
+
`torch.utils.data.dataloader.DataLoader` of length 20191 with parameters:
|
110 |
+
```
|
111 |
+
{'batch_size': 24, 'sampler': 'torch.utils.data.sampler.RandomSampler', 'batch_sampler': 'torch.utils.data.sampler.BatchSampler'}
|
112 |
+
```
|
113 |
+
|
114 |
+
**Loss**:
|
115 |
+
|
116 |
+
`sentence_transformers.losses.MultipleNegativesRankingLoss.MultipleNegativesRankingLoss` with parameters:
|
117 |
+
```
|
118 |
+
{'scale': 20.0, 'similarity_fct': 'cos_sim'}
|
119 |
+
```
|
120 |
+
|
121 |
+
Parameters of the fit()-Method:
|
122 |
+
```
|
123 |
+
{
|
124 |
+
"epochs": 1,
|
125 |
+
"evaluation_steps": 500,
|
126 |
+
"evaluator": "sentence_transformers.evaluation.EmbeddingSimilarityEvaluator.EmbeddingSimilarityEvaluator",
|
127 |
+
"max_grad_norm": 1,
|
128 |
+
"optimizer_class": "<class 'torch.optim.adamw.AdamW'>",
|
129 |
+
"optimizer_params": {
|
130 |
+
"lr": 2e-05
|
131 |
+
},
|
132 |
+
"scheduler": "WarmupLinear",
|
133 |
+
"steps_per_epoch": null,
|
134 |
+
"warmup_steps": 10000,
|
135 |
+
"weight_decay": 0.01
|
136 |
+
}
|
137 |
+
```
|
138 |
+
|
139 |
+
## Full Model Architecture
|
140 |
+
```
|
141 |
+
SentenceTransformer(
|
142 |
+
(0): Transformer({'max_seq_length': 512, 'do_lower_case': False}) with Transformer model: BertModel
|
143 |
+
(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})
|
144 |
+
)
|
145 |
+
```
|
146 |
+
|
147 |
+
## More Information
|
148 |
+
|
149 |
+
Read more about this model and how it was built in [this article](https://medium.com/neuml/embeddings-for-medical-literature-74dae6abf5e0).
|
added_tokens.json
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"[CLS]": 2,
|
3 |
+
"[MASK]": 4,
|
4 |
+
"[PAD]": 0,
|
5 |
+
"[SEP]": 3,
|
6 |
+
"[UNK]": 1
|
7 |
+
}
|
config.json
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "microsoft/BiomedNLP-PubMedBERT-base-uncased-abstract-fulltext",
|
3 |
+
"architectures": [
|
4 |
+
"BertModel"
|
5 |
+
],
|
6 |
+
"attention_probs_dropout_prob": 0.1,
|
7 |
+
"classifier_dropout": null,
|
8 |
+
"hidden_act": "gelu",
|
9 |
+
"hidden_dropout_prob": 0.1,
|
10 |
+
"hidden_size": 768,
|
11 |
+
"initializer_range": 0.02,
|
12 |
+
"intermediate_size": 3072,
|
13 |
+
"layer_norm_eps": 1e-12,
|
14 |
+
"max_position_embeddings": 512,
|
15 |
+
"model_type": "bert",
|
16 |
+
"num_attention_heads": 12,
|
17 |
+
"num_hidden_layers": 12,
|
18 |
+
"pad_token_id": 0,
|
19 |
+
"position_embedding_type": "absolute",
|
20 |
+
"torch_dtype": "float32",
|
21 |
+
"transformers_version": "4.34.0",
|
22 |
+
"type_vocab_size": 2,
|
23 |
+
"use_cache": true,
|
24 |
+
"vocab_size": 30522
|
25 |
+
}
|
config_sentence_transformers.json
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"__version__": {
|
3 |
+
"sentence_transformers": "2.2.2",
|
4 |
+
"transformers": "4.34.0",
|
5 |
+
"pytorch": "2.0.1+cu117"
|
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:0bdb9787bcb608f0e4dbfa2724821b7d66a66be79508bff915a9d2e3fe1f3853
|
3 |
+
size 437995689
|
sentence_bert_config.json
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"max_seq_length": 512,
|
3 |
+
"do_lower_case": false
|
4 |
+
}
|
similarity_evaluation_results.csv
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
epoch,steps,cosine_pearson,cosine_spearman,euclidean_pearson,euclidean_spearman,manhattan_pearson,manhattan_spearman,dot_pearson,dot_spearman
|
2 |
+
-1,-1,0.9616227888194525,0.8655338878240353,0.9392462019157571,0.865178743767881,0.9391683403350186,0.8652078047869656,0.9520250497966435,0.8654981897193533
|
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,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"added_tokens_decoder": {
|
3 |
+
"0": {
|
4 |
+
"content": "[PAD]",
|
5 |
+
"lstrip": false,
|
6 |
+
"normalized": false,
|
7 |
+
"rstrip": false,
|
8 |
+
"single_word": false,
|
9 |
+
"special": true
|
10 |
+
},
|
11 |
+
"1": {
|
12 |
+
"content": "[UNK]",
|
13 |
+
"lstrip": false,
|
14 |
+
"normalized": false,
|
15 |
+
"rstrip": false,
|
16 |
+
"single_word": false,
|
17 |
+
"special": true
|
18 |
+
},
|
19 |
+
"2": {
|
20 |
+
"content": "[CLS]",
|
21 |
+
"lstrip": false,
|
22 |
+
"normalized": false,
|
23 |
+
"rstrip": false,
|
24 |
+
"single_word": false,
|
25 |
+
"special": true
|
26 |
+
},
|
27 |
+
"3": {
|
28 |
+
"content": "[SEP]",
|
29 |
+
"lstrip": false,
|
30 |
+
"normalized": false,
|
31 |
+
"rstrip": false,
|
32 |
+
"single_word": false,
|
33 |
+
"special": true
|
34 |
+
},
|
35 |
+
"4": {
|
36 |
+
"content": "[MASK]",
|
37 |
+
"lstrip": false,
|
38 |
+
"normalized": false,
|
39 |
+
"rstrip": false,
|
40 |
+
"single_word": false,
|
41 |
+
"special": true
|
42 |
+
}
|
43 |
+
},
|
44 |
+
"additional_special_tokens": [],
|
45 |
+
"clean_up_tokenization_spaces": true,
|
46 |
+
"cls_token": "[CLS]",
|
47 |
+
"do_basic_tokenize": true,
|
48 |
+
"do_lower_case": true,
|
49 |
+
"mask_token": "[MASK]",
|
50 |
+
"model_max_length": 1000000000000000019884624838656,
|
51 |
+
"never_split": null,
|
52 |
+
"pad_token": "[PAD]",
|
53 |
+
"sep_token": "[SEP]",
|
54 |
+
"strip_accents": null,
|
55 |
+
"tokenize_chinese_chars": true,
|
56 |
+
"tokenizer_class": "BertTokenizer",
|
57 |
+
"unk_token": "[UNK]"
|
58 |
+
}
|
vocab.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|