This is a PubMedBERT-base model fined-tuned using sentence-transformers. 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 title-abstract pairs along with similar title pairs.
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.
Usage (txtai)
This model can be used to build embeddings databases with txtai for semantic search and/or as a knowledge source for retrieval augmented generation (RAG).
python
1import txtai
23embeddings = txtai.Embeddings(path="neuml/pubmedbert-base-embeddings", content=True)4embeddings.index(documents())56# Run a query7embeddings.search("query to run")
1from sentence_transformers import SentenceTransformer
2sentences =["This is an example sentence","Each sentence is converted"]34model = SentenceTransformer("neuml/pubmedbert-base-embeddings")5embeddings = model.encode(sentences)6print(embeddings)
Usage (Hugging Face Transformers)
The model can also be used directly with Transformers.
python
1from transformers import AutoTokenizer, AutoModel
2import torch
34# Mean Pooling - Take attention mask into account for correct averaging5defmeanpooling(output, mask):6 embeddings = output[0]# First element of model_output contains all token embeddings7 mask = mask.unsqueeze(-1).expand(embeddings.size()).float()8return torch.sum(embeddings * mask,1)/ torch.clamp(mask.sum(1),min=1e-9)910# Sentences we want sentence embeddings for11sentences =['This is an example sentence','Each sentence is converted']1213# Load model from HuggingFace Hub14tokenizer = AutoTokenizer.from_pretrained("neuml/pubmedbert-base-embeddings")15model = AutoModel.from_pretrained("neuml/pubmedbert-base-embeddings")1617# Tokenize sentences18inputs = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')1920# Compute token embeddings21with torch.no_grad():22 output = model(**inputs)2324# Perform pooling. In this case, mean pooling.25embeddings = meanpooling(output, inputs['attention_mask'])2627print("Sentence embeddings:")28print(embeddings)
Evaluation Results
Performance of this model compared to the top base models on the 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.
The following datasets were used to evaluate model performance.