Views
No views yet
1import numpy as np
2import torch
3from tqdm.auto import tqdm
4from transformers import AutoTokenizer, AutoModel
5
6tokenizer = AutoTokenizer.from_pretrained("UMCU/sap_umls_MedRoBERTa.nl_meantoken")
7model = AutoModel.from_pretrained("UMCU/sap_umls_MedRoBERTa.nl_meantoken").cuda()
8
9# replace with your own list of entity names
10all_names = ["covid-19", "Coronavirus infection", "high fever", "Tumor of posterior wall of oropharynx"]
11
12bs = 128 # batch size during inference
13all_embs = []
14for i in tqdm(np.arange(0, len(all_names), bs)):
15 toks = tokenizer.batch_encode_plus(all_names[i:i+bs],
16 padding="max_length",
17 max_length=25,
18 truncation=True,
19 return_tensors="pt")
20 toks_cuda = {}
21 for k,v in toks.items():
22 toks_cuda[k] = v.cuda()
23 cls_rep = model(**toks_cuda)[0].mean(1)
24 all_embs.append(cls_rep.cpu().detach().numpy())
25
26all_embs = np.concatenate(all_embs, axis=0)1@inproceedings{liu-etal-2021-self,
2 title = "Self-Alignment Pretraining for Biomedical Entity Representations",
3 author = "Liu, Fangyu and
4 Shareghi, Ehsan and
5 Meng, Zaiqiao and
6 Basaldella, Marco and
7 Collier, Nigel",
8 booktitle = "Proceedings of the 2021 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies",
9 month = jun,
10 year = "2021",
11 address = "Online",
12 publisher = "Association for Computational Linguistics",
13 url = "https://www.aclweb.org/anthology/2021.naacl-main.334",
14 pages = "4228--4238",
15 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.",
16}