模型:
SciBERT 是基于BERT的预训练语言模型,由 Allen Institute for AI 在 Semantic Scholar 的论文语料库上进行训练。语料库的大小是1.14M篇论文,3.1B个标记。SciBERT有自己的词汇表(scivocab),旨在最佳匹配训练语料库。
allenai/scibert_scivocab_cased 已经针对命名实体识别(NER)下游任务进行了微调。训练NER的代码可以在 here 找到。
用于微调NER的语料库是 BioNLP / JNLPBA shared task 。
下面显示了训练和评估数据集中类(单词级别)及其分布(每个类的示例数):
| Class Label | # training examples | # evaluation examples |
|---|---|---|
| O | 382,963 | 81,647 |
| B-protein | 30,269 | 5,067 |
| I-protein | 24,848 | 4,774 |
| B-cell_type | 6,718 | 1,921 |
| I-cell_type | 8,748 | 2,991 |
| B-DNA | 9,533 | 1,056 |
| I-DNA | 15,774 | 1,789 |
| B-cell_line | 3,830 | 500 |
| I-cell_line | 7,387 | 9,89 |
| B-RNA | 951 | 118 |
| I-RNA | 1,530 | 187 |
进行了详尽的超参数搜索。提供最佳结果的超参数是:
使用的学习率为5e-5,采用递减线性调度。在训练开始时使用了预热,预热步骤的比例为总训练步骤的0.1。
选择了F1分数最佳的模型,即第5个epoch的模型。
以下表格显示了在span/entity级别计算的评估指标:
| precision | recall | f1-score | |
|---|---|---|---|
| cell_line | 0.5205 | 0.7100 | 0.6007 |
| cell_type | 0.7736 | 0.7422 | 0.7576 |
| protein | 0.6953 | 0.8459 | 0.7633 |
| DNA | 0.6997 | 0.7894 | 0.7419 |
| RNA | 0.6985 | 0.8051 | 0.7480 |
| micro avg | 0.6984 | 0.8076 | 0.7490 |
| macro avg | 0.7032 | 0.8076 | 0.7498 |
宏平均F1分数为0.7498,与Allen Institute for AI在其 paper 提供的值相比,它为0.7728。性能下降的原因可能有多种,但一个假设是作者使用了额外的条件随机场,而该模型使用了SciBERT模型上的常规分类层和softmax激活。
在单词级别上,该模型实现了0.7742的精确率、0.8536的召回率和0.8093的F1分数。
使用如下流水线:
from transformers import pipeline
text = "Mouse thymus was used as a source of glucocorticoid receptor from normal CS lymphocytes."
nlp_ner = pipeline("ner",
model='fran-martinez/scibert_scivocab_cased_ner_jnlpba',
tokenizer='fran-martinez/scibert_scivocab_cased_ner_jnlpba')
nlp_ner(text)
"""
Output:
---------------------------
[
{'word': 'glucocorticoid',
'score': 0.9894881248474121,
'entity': 'B-protein'},
{'word': 'receptor',
'score': 0.989505410194397,
'entity': 'I-protein'},
{'word': 'normal',
'score': 0.7680378556251526,
'entity': 'B-cell_type'},
{'word': 'cs',
'score': 0.5176806449890137,
'entity': 'I-cell_type'},
{'word': 'lymphocytes',
'score': 0.9898491501808167,
'entity': 'I-cell_type'}
]
"""
或按以下方式加载模型和标记器:
import torch
from transformers import AutoTokenizer, AutoModelForTokenClassification
# Example
text = "Mouse thymus was used as a source of glucocorticoid receptor from normal CS lymphocytes."
# Load model
tokenizer = AutoTokenizer.from_pretrained("fran-martinez/scibert_scivocab_cased_ner_jnlpba")
model = AutoModelForTokenClassification.from_pretrained("fran-martinez/scibert_scivocab_cased_ner_jnlpba")
# Get input for BERT
input_ids = torch.tensor(tokenizer.encode(text)).unsqueeze(0)
# Predict
with torch.no_grad():
outputs = model(input_ids)
# From the output let's take the first element of the tuple.
# Then, let's get rid of [CLS] and [SEP] tokens (first and last)
predictions = outputs[0].argmax(axis=-1)[0][1:-1]
# Map label class indexes to string labels.
for token, pred in zip(tokenizer.tokenize(text), predictions):
print(token, '->', model.config.id2label[pred.numpy().item()])
"""
Output:
---------------------------
mouse -> O
thymus -> O
was -> O
used -> O
as -> O
a -> O
source -> O
of -> O
glucocorticoid -> B-protein
receptor -> I-protein
from -> O
normal -> B-cell_type
cs -> I-cell_type
lymphocytes -> I-cell_type
. -> O
"""