Introduction of pubmedbert-base-embeddings-matryoshka
Model Details of pubmedbert-base-embeddings-matryoshka
PubMedBERT Embeddings Matryoshka
This is a version of
PubMedBERT Embeddings
with
Matryoshka Representation Learning
applied. This enables dynamic embeddings sizes of
64
,
128
,
256
,
384
,
512
and the full size of
768
. It's important to note while this method saves space, the same computational resources are used regardless of the dimension size.
Sentence Transformers 2.4 added support for Matryoshka Embeddings. More can be read in
this blog post
.
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).
import txtai
# New embeddings with requested dimensionality
embeddings = txtai.Embeddings(
path="neuml/pubmedbert-base-embeddings-matryoshka",
content=True,
dimensionality=256
)
embeddings.index(documents())
# Run a query
embeddings.search("query to run")
from sentence_transformers import SentenceTransformer
sentences = ["This is an example sentence", "Each sentence is converted"]
model = SentenceTransformer("neuml/pubmedbert-base-embeddings-matryoshka")
embeddings = model.encode(sentences)
# Requested dimensionality
dimensionality = 256print(embeddings[:, :dimensionality])
Usage (Hugging Face Transformers)
The model can also be used directly with Transformers.
from transformers import AutoTokenizer, AutoModel
import torch
# Mean Pooling - Take attention mask into account for correct averagingdefmeanpooling(output, mask):
embeddings = output[0] # First element of model_output contains all token embeddings
mask = mask.unsqueeze(-1).expand(embeddings.size()).float()
return torch.sum(embeddings * mask, 1) / torch.clamp(mask.sum(1), min=1e-9)
# Sentences we want sentence embeddings for
sentences = ['This is an example sentence', 'Each sentence is converted']
# Load model from HuggingFace Hub
tokenizer = AutoTokenizer.from_pretrained("neuml/pubmedbert-base-embeddings-matryoshka")
model = AutoModel.from_pretrained("neuml/pubmedbert-base-embeddings-matryoshka")
# Tokenize sentences
inputs = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
# Compute token embeddingswith torch.no_grad():
output = model(**inputs)
# Perform pooling. In this case, mean pooling.
embeddings = meanpooling(output, inputs['attention_mask'])
# Requested dimensionality
dimensionality = 256print("Sentence embeddings:")
print(embeddings[:, :dimensionality])
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.
See the table below for evaluation results per dimension for
pubmedbert-base-embeddings-matryoshka
.
Model
PubMed QA
PubMed Subset
PubMed Summary
Average
Dimensions = 64
92.16
95.85
95.67
94.56
Dimensions = 128
92.80
96.44
96.22
95.15
Dimensions = 256
93.11
96.68
96.53
95.44
Dimensions = 384
93.42
96.79
96.61
95.61
Dimensions = 512
93.37
96.87
96.61
95.62
Dimensions = 768
93.53
96.95
96.70
95.73
This model performs slightly better overall compared to the original model.
The bigger takeaway is how competitive it is at lower dimensions. For example,
Dimensions = 256
performs better than all the other models originally tested above. Even
Dimensions = 64
performs better than
all-MiniLM-L6-v2
and
bge-base-en-v1.5
.
Training
The model was trained with the parameters:
DataLoader
:
torch.utils.data.dataloader.DataLoader
of length 20191 with parameters:
pubmedbert-base-embeddings-matryoshka huggingface.co is an AI model on huggingface.co that provides pubmedbert-base-embeddings-matryoshka's model effect (), which can be used instantly with this NeuML pubmedbert-base-embeddings-matryoshka model. huggingface.co supports a free trial of the pubmedbert-base-embeddings-matryoshka model, and also provides paid use of the pubmedbert-base-embeddings-matryoshka. Support call pubmedbert-base-embeddings-matryoshka model through api, including Node.js, Python, http.
pubmedbert-base-embeddings-matryoshka huggingface.co is an online trial and call api platform, which integrates pubmedbert-base-embeddings-matryoshka's modeling effects, including api services, and provides a free online trial of pubmedbert-base-embeddings-matryoshka, you can try pubmedbert-base-embeddings-matryoshka online for free by clicking the link below.
NeuML pubmedbert-base-embeddings-matryoshka online free url in huggingface.co:
pubmedbert-base-embeddings-matryoshka is an open source model from GitHub that offers a free installation service, and any user can find pubmedbert-base-embeddings-matryoshka on GitHub to install. At the same time, huggingface.co provides the effect of pubmedbert-base-embeddings-matryoshka install, users can directly use pubmedbert-base-embeddings-matryoshka installed effect in huggingface.co for debugging and trial. It also supports api for free installation.
pubmedbert-base-embeddings-matryoshka install url in huggingface.co: