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/mirrorbert_MedRoBERTa.nl_clstoken")
7model = AutoModel.from_pretrained("UMCU/mirrorbert_MedRoBERTa.nl_clstoken").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,:]
24 all_embs.append(cls_rep.cpu().detach().numpy())
25
26all_embs = np.concatenate(all_embs, axis=0)1@inproceedings{liu-etal-2021-fast,
2 title = "Fast, Effective, and Self-Supervised: Transforming Masked Language Models into Universal Lexical and Sentence Encoders",
3 author = "Liu, Fangyu and
4 Vuli{'c}, Ivan and
5 Korhonen, Anna and
6 Collier, Nigel",
7 booktitle = "Proceedings of the 2021 Conference on Empirical Methods in Natural Language Processing",
8 month = nov,
9 year = "2021",
10 address = "Online and Punta Cana, Dominican Republic",
11 publisher = "Association for Computational Linguistics",
12 url = "https://aclanthology.org/2021.emnlp-main.109",
13 pages = "1442--1459",
14}