This is a distilled version of PubMedBERT Embeddings using the Model2Vec library. It uses static embeddings, allowing text embeddings to be computed orders of magnitude faster on both GPU and CPU. It is designed for applications where computational resources are limited or where real-time performance is critical.
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
23# Create embeddings4embeddings = txtai.Embeddings(5 path="neuml/pubmedbert-base-embeddings-2M",6 content=True,7)8embeddings.index(documents())910# Run a query11embeddings.search("query to run")
1from sentence_transformers import SentenceTransformer
2from sentence_transformers.models import StaticEmbedding
34# Initialize a StaticEmbedding module5static = StaticEmbedding.from_model2vec("neuml/pubmedbert-base-embeddings-2M")6model = SentenceTransformer(modules=[static])78sentences =["This is an example sentence","Each sentence is converted"]9embeddings = model.encode(sentences)10print(embeddings)
Usage (Model2Vec)
The model can also be used directly with Model2Vec.
python
1from model2vec import StaticModel
23# Load a pretrained Model2Vec model4model = StaticModel.from_pretrained("neuml/pubmedbert-base-embeddings-2M")56# Compute text embeddings7sentences =["This is an example sentence","Each sentence is converted"]8embeddings = model.encode(sentences)9print(embeddings)
Evaluation Results
The following compares performance of this model against the models previously compared with PubMedBERT Embeddings. The following datasets were used to evaluate model performance.
As we can see, this model while not the top scoring model is certainly competitive.
Runtime performance
As another test, let's see how long each model takes to index 120K article abstracts using the following code. All indexing is done with a RTX 3090 GPU.
Clearly a static model's main upside is speed. It's important to note that if storage savings is the only concern, then take a look at PubMedBERT Embeddings Matryoshka. The 256 dimension model scores higher than this model, so does the 64 dimension model. The tradeoff is that the runtime performance is still as slow as the base model.
If runtime performance is the major concern, then a static model offers the best blend of accuracy and speed. Model2Vec models only need CPUs to run, no GPU required. Note how this model takes the same amount of time as building a BM25 index, which is normally an order of magnitude faster than vector models.
Training
This model was trained using the Tokenlearn library. First data was featurized with the following script.