Views
No views yet
8,192 tokens vs the 512 tokens supported by standard BERT models.1import txtai
2
3embeddings = txtai.Embeddings(path="neuml/bioclinical-modernbert-base-embeddings", content=True)
4embeddings.index(documents())
5
6# Run a query
7embeddings.search("query to run")1from sentence_transformers import SentenceTransformer
2sentences = ["This is an example sentence", "Each sentence is converted"]
3
4model = SentenceTransformer("neuml/bioclinical-modernbert-base-embeddings")
5embeddings = model.encode(sentences)
6print(embeddings)1from transformers import AutoTokenizer, AutoModel
2import torch
3
4# Mean Pooling - Take attention mask into account for correct averaging
5def meanpooling(output, mask):
6 embeddings = output[0] # First element of model_output contains all token embeddings
7 mask = mask.unsqueeze(-1).expand(embeddings.size()).float()
8 return torch.sum(embeddings * mask, 1) / torch.clamp(mask.sum(1), min=1e-9)
9
10# Sentences we want sentence embeddings for
11sentences = ['This is an example sentence', 'Each sentence is converted']
12
13# Load model from HuggingFace Hub
14tokenizer = AutoTokenizer.from_pretrained("neuml/bioclinical-modernbert-base-embeddings")
15model = AutoModel.from_pretrained("neuml/bioclinical-modernbert-base-embeddings")
16
17# Tokenize sentences
18inputs = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
19
20# Compute token embeddings
21with torch.no_grad():
22 output = model(**inputs)
23
24# Perform pooling. In this case, mean pooling.
25embeddings = meanpooling(output, inputs['attention_mask'])
26
27print("Sentence embeddings:")
28print(embeddings)| Model | PubMed QA | PubMed Subset | PubMed Summary | Average |
|---|---|---|---|---|
| all-MiniLM-L6-v2 | 90.40 | 95.92 | 94.07 | 93.46 |
| bioclinical-modernbert-base-embeddings | 92.49 | 97.10 | 97.04 | 95.54 |
| bge-base-en-v1.5 | 91.02 | 95.82 | 94.49 | 93.78 |
| gte-base | 92.97 | 96.90 | 96.24 | 95.37 |
| pubmedbert-base-embeddings | 93.27 | 97.00 | 96.58 | 95.62 |
| S-PubMedBert-MS-MARCO | 90.86 | 93.68 | 93.54 | 92.69 |
8,192 tokens vs 512.{'batch_size': 8, 'sampler': 'torch.utils.data.sampler.RandomSampler', 'batch_sampler': 'torch.utils.data.sampler.BatchSampler'}sentence_transformers.losses.MultipleNegativesRankingLoss.MultipleNegativesRankingLoss with parameters:{'scale': 20.0, 'similarity_fct': 'cos_sim'}{
"epochs": 1,
"evaluation_steps": 2000,
"evaluator": "sentence_transformers.evaluation.EmbeddingSimilarityEvaluator.EmbeddingSimilarityEvaluator",
"max_grad_norm": 1,
"optimizer_class": "<class 'torch.optim.adamw.AdamW'>",
"optimizer_params": {
"lr": 2e-05
},
"scheduler": "WarmupLinear",
"steps_per_epoch": null,
"warmup_steps": 10000,
"weight_decay": 0.01
}SentenceTransformer(
(0): Transformer({'max_seq_length': 8192, 'do_lower_case': False}) with Transformer model: ModernBertModel
(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, 'pooling_mode_weightedmean_tokens': False, 'pooling_mode_lasttoken': False, 'include_prompt': True})
)