模型:
ProdicusII/ZeroShotBioNER
该模型是拜耳制药公司与塞尔维亚人工智能研究与发展研究所的研究合作期间开发的。该模型在约25个生物医学NER类上进行训练,可以执行零样本推理,并可以通过少量示例进行进一步微调(少样本学习)。有关我们方法的更多细节,请参阅名为 "A transformer-based method for zero and few-shot biomedical named entity recognition" 的论文。该模型对应于基于BioBERT的模式,在第一个片段中训练了1(请查阅论文了解更多细节)。
模型以两个字符串作为输入。字符串1是在第二个字符串中搜索的NER标签。字符串1必须是实体的短语。字符串2是需要在其中语义上搜索字符串1的简短文本。模型将输出一个由0和1组成的列表,对应于命名实体在第二个字符串中的出现,并对应于字符串2的令牌(由Transformer分词器提供)。
from transformers import AutoTokenizer
from transformers import BertForTokenClassification
modelname = 'ProdicusII/ZeroShotBioNER' # modelpath
tokenizer = AutoTokenizer.from_pretrained(modelname) ## loading the tokenizer of that model
string1 = 'Drug'
string2 = 'No recent antibiotics or other nephrotoxins, and no symptoms of UTI with benign UA.'
encodings = tokenizer(string1, string2, is_split_into_words=False,
padding=True, truncation=True, add_special_tokens=True, return_offsets_mapping=False,
max_length=512, return_tensors='pt')
model0 = BertForTokenClassification.from_pretrained(modelname, num_labels=2)
prediction_logits = model0(**encodings)
print(prediction_logits)
为了使用少量样本对模型进行新实体的微调,需要将数据集转换为torch.utils.data.Dataset,其中包含BERT令牌和一组0和1(1表示类别为正,应作为给定NER类的成员进行预测)。创建数据集后,可以执行以下操作(有关更多细节,请参阅GitHub上的代码 https://github.com/br-ai-ns-institute/Zero-ShotNER )。
training_args = TrainingArguments(
output_dir=os.path.join('Results', class_unseen, str(j)+'Shot'), # folder for results
num_train_epochs=10, # number of epochs
per_device_train_batch_size=16, # batch size per device during training
per_device_eval_batch_size=16, # batch size for evaluation
weight_decay=0.01, # strength of weight decay
logging_dir=os.path.join('Logs', class_unseen, str(j)+'Shot'), # folder for logs
save_strategy='epoch',
evaluation_strategy='epoch',
load_best_model_at_end=True,
)
model0 = BertForTokenClassification.from_pretrained(model_path, num_labels=2)
trainer = Trainer(
model=model0, # pretrained model
args=training_args, # training artguments
train_dataset=dataset, # Object of class torch.utils.data.Dataset for training
eval_dataset=dataset_valid # Object of class torch.utils.data.Dataset for vaLidation
)
start_time = time.time()
trainer.train()
total_time = time.time()-start_time
model0_path = os.path.join('Results', class_unseen, str(j)+'Shot', 'Model')
os.makedirs(model0_path, exist_ok=True)
trainer.save_model(model0_path)
以下数据集和实体用于训练,因此它们可以用作第一部分的标签(作为第一个字符串)。请注意,多词字符串已合并。
除此之外,还可以在零样本环境中使用该模型进行其他类别的预测,也可以使用少量示例对其进行微调。
用于训练和测试模型的代码可在 https://github.com/br-ai-ns-institute/Zero-ShotNER 处获得。
如果您使用了该模型或从中受到启发,请在您的论文中引用以下文章:
Košprdić M., Prodanović N., Ljajić A., Bašaragin B., Milošević N., 2023. A transformer-based method for zero and few-shot biomedical named entity recognition. arXiv预印本arXiv:2305.04928. https://arxiv.org/abs/2305.04928
或使用bibtex格式引用:
@misc{kosprdic2023transformerbased,
title={A transformer-based method for zero and few-shot biomedical named entity recognition},
author={Miloš Košprdić and Nikola Prodanović and Adela Ljajić and Bojana Bašaragin and Nikola Milošević},
year={2023},
eprint={2305.04928},
archivePrefix={arXiv},
primaryClass={cs.CL}
}