Views
No views yet
1import txtai
2
3embeddings = txtai.Embeddings(
4 path="neuml/biomedbert-hash-nano-embeddings",
5 content=True,
6 vectors={"trust_remote_code": True}
7)
8embeddings.index(documents())
9
10# Run a query
11embeddings.search("query to run")1from sentence_transformers import SentenceTransformer
2sentences = ["This is an example sentence", "Each sentence is converted"]
3
4model = SentenceTransformer("neuml/biomedbert-hash-nano-embeddings", trust_remote_code=True)
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/biomedbert-hash-nano-embeddings", trust_remote_code=True)
15model = AutoModel.from_pretrained("neuml/biomedbert-hash-nano-embeddings", trust_remote_code=True)
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 |
| biomedbert-base-colbert | 94.59 | 97.18 | 96.21 | 95.99 |
| biomedbert-base-reranker | 97.66 | 99.76 | 98.81 | 98.74 |
| biomedbert-hash-nano-colbert | 90.45 | 96.81 | 92.00 | 93.09 |
| biomedbert-hash-nano-embeddings | 90.39 | 96.29 | 95.32 | 94.00 |
| pubmedbert-base-embeddings | 93.27 | 97.00 | 96.58 | 95.62 |
| pubmedbert-base-embeddings-8M | 90.05 | 94.29 | 94.15 | 92.83 |
pubmedbert-base-embeddings at 0.88% the size. The performance is also better than all-MiniLM-L6-v2, a commonly used small model and it's 23x smaller. It also performs much better than the 8M static embeddings model although it is slower given that model is static.SentenceTransformer(
(0): Transformer({'max_seq_length': 512, 'do_lower_case': False, 'architecture': 'BertHashModel'})
(1): Pooling({'word_embedding_dimension': 128, '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})
)