SapBERT from PubMedBERT fulltext

Providercambridgeltl
Categoryfeature-extraction
Licenseapache-2.0
Downloads1.6M
Stars0

Overview

SapBERT is a specialized encoder model designed for biomedical entity normalization. Unlike general-purpose BERT models, it is pre-trained on PubMed full-text and fine-tuned using self-alignment to map synonymous biomedical terms to a single canonical concept in a vector space. For developers, this means it excels at resolving nomenclature variations and mapping noisy clinical text to standardized ontologies like UMLS. It is primarily used for feature extraction, providing high-quality dense embeddings that significantly improve the accuracy of semantic similarity tasks and entity linking pipelines compared to standard BERT or BioBERT.

Highlights

  • Optimized for biomedical entity normalization and synonym mapping
  • Pre-trained on comprehensive PubMed full-text corpora
  • Generates high-precision embeddings for semantic similarity tasks
  • Apache-2.0 licensed for flexible commercial integration
  • Outperforms general encoders in clinical entity linking

Usage

Install
# Install Hugging Face transformers
pip install transformers torch
SDK Usage
# Load model with transformers
from transformers import AutoModel, AutoTokenizer

model = AutoModel.from_pretrained("cambridgeltl/SapBERT-from-PubMedBERT-fulltext")
tokenizer = AutoTokenizer.from_pretrained("cambridgeltl/SapBERT-from-PubMedBERT-fulltext")

Hugging Face Download

We recommend downloading the model via the Hugging Face CLI or Hub SDK.

Guidance:Before downloading, install huggingface_hub with:

Guidance
pip install -U huggingface_hub

CLI Download

Download the full repository

Download the full repository
huggingface-cli download cambridgeltl/SapBERT-from-PubMedBERT-fulltext

Download a single file to a local folder (e.g. config.json into ./dir)

Download a single file to a local folder (e.g. config.json into ./dir)
huggingface-cli download cambridgeltl/SapBERT-from-PubMedBERT-fulltext config.json --local-dir ./dir

See the official docs for more CLI options

SDK Download

SDK Download
# 模型下载
from huggingface_hub import snapshot_download
model_dir = snapshot_download('cambridgeltl/SapBERT-from-PubMedBERT-fulltext')

Git Download

Make sure git-lfs is installed first

Git Download
git lfs install
git clone https://huggingface.co/cambridgeltl/SapBERT-from-PubMedBERT-fulltext

To skip LFS large-file downloads, use:

Skip LFS
GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/cambridgeltl/SapBERT-from-PubMedBERT-fulltext

Model files are hosted on the Hugging Face Hub — download directly via HF CLI / SDK / Git, not through this site.

PyTorch / Transformers Usage

Install Transformers

Install Transformers
pip install -U transformers torch

Load the model and run inference

Load the model and run inference
from transformers import AutoModelForCausalLM, AutoTokenizer

model = AutoModelForCausalLM.from_pretrained('cambridgeltl/SapBERT-from-PubMedBERT-fulltext')
tokenizer = AutoTokenizer.from_pretrained('cambridgeltl/SapBERT-from-PubMedBERT-fulltext')

Full Documentation

来源: HuggingFace

---
license: apache-2.0
language:

  • en

tags:
  • biomedical

  • lexical semantics

  • bionlp

  • biology

  • science

  • embedding

  • entity linking

---
---

datasets:

  • UMLS

[news] A cross-lingual extension of SapBERT will appear in the main onference of ACL 2021! <br>
[news] SapBERT will appear in the conference proceedings of NAACL 2021!

SapBERT-PubMedBERT

SapBERT by Liu et al. (2020). Trained with UMLS 2020AA (English only), using microsoft/BiomedNLP-PubMedBERT-base-uncased-abstract-fulltext as the base model.

Expected input and output

The input should be a string of biomedical entity names, e.g., "covid infection" or "Hydroxychloroquine". The [CLS] embedding of the last layer is regarded as the output.

#### Extracting embeddings from SapBERT

The following script converts a list of strings (entity names) into embeddings.

python
import numpy as np
import torch
from tqdm.auto import tqdm
from transformers import AutoTokenizer, AutoModel

tokenizer = AutoTokenizer.from_pretrained("cambridgeltl/SapBERT-from-PubMedBERT-fulltext")
model = AutoModel.from_pretrained("cambridgeltl/SapBERT-from-PubMedBERT-fulltext").cuda()

replace with your own list of entity names

all_names = ["covid-19", "Coronavirus infection", "high fever", "Tumor of posterior wall of oropharynx"]

bs = 128 # batch size during inference
all_embs = []
for i in tqdm(np.arange(0, len(all_names), bs)):
toks = tokenizer.batch_encode_plus(all_names[i:i+bs],
padding="max_length",
max_length=25,
truncation=True,
return_tensors="pt")
toks_cuda = {}
for k,v in toks.items():
toks_cuda[k] = v.cuda()
cls_rep = model(**toks_cuda)[0][:,0,:] # use CLS representation as the embedding
all_embs.append(cls_rep.cpu().detach().numpy())

all_embs = np.concatenate(all_embs, axis=0)

For more details about training and eval, see SapBERT github repo.

Citation

bibtex
@inproceedings{liu-etal-2021-self,
    title = "Self-Alignment Pretraining for Biomedical Entity Representations",
    author = "Liu, Fangyu  and
      Shareghi, Ehsan  and
      Meng, Zaiqiao  and
      Basaldella, Marco  and
      Collier, Nigel",
    booktitle = "Proceedings of the 2021 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies",
    month = jun,
    year = "2021",
    address = "Online",
    publisher = "Association for Computational Linguistics",
    url = "https://www.aclweb.org/anthology/2021.naacl-main.334",
    pages = "4228--4238",
    abstract = "Despite the widespread success of self-supervised learning via masked language models (MLM), accurately capturing fine-grained semantic relationships in the biomedical domain remains a challenge. This is of paramount importance for entity-level tasks such as entity linking where the ability to model entity relations (especially synonymy) is pivotal. To address this challenge, we propose SapBERT, a pretraining scheme that self-aligns the representation space of biomedical entities. We design a scalable metric learning framework that can leverage UMLS, a massive collection of biomedical ontologies with 4M+ concepts. In contrast with previous pipeline-based hybrid systems, SapBERT offers an elegant one-model-for-all solution to the problem of medical entity linking (MEL), achieving a new state-of-the-art (SOTA) on six MEL benchmarking datasets. In the scientific domain, we achieve SOTA even without task-specific supervision. With substantial improvement over various domain-specific pretrained MLMs such as BioBERT, SciBERTand and PubMedBERT, our pretraining scheme proves to be both effective and robust.",
}
Join our Telegram