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("cambridgeltl/SapBERT-from-PubMedBERT-fulltext")
7model = AutoModel.from_pretrained("cambridgeltl/SapBERT-from-PubMedBERT-fulltext").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][:,0,:] # use CLS representation as the embedding
24 all_embs.append(cls_rep.cpu().detach().numpy())
25
26all_embs = np.concatenate(all_embs, axis=0)1@inproceedings{liu2021learning,
2 title={Learning Domain-Specialised Representations for Cross-Lingual Biomedical Entity Linking},
3 author={Liu, Fangyu and Vuli{\'c}, Ivan and Korhonen, Anna and Collier, Nigel},
4 booktitle={Proceedings of ACL-IJCNLP 2021},
5 month = aug,
6 year={2021}
7}