T-NER是通过PIP上实施的Python工具,用于在Pytorch中实现的命名 - 实体识别(NER)。它具有轻松的界面,可用于芬太纳模型并在跨域和多语言数据集上进行测试。 T-NER当前集成了公开可用的NER数据集的高度覆盖范围,并可以简单地集成自定义数据集。所有使用T-NER固定的模型都可以在我们的Web应用程序上部署,以进行可视化。我们的论文证明了T-NER已被EACL 2021接受。所有模型和数据集均通过T-Ner HuggingFace组共享。
New(2022年9月):我们根据Twitter tweetner7发布了新的NER数据集,该论文被AACL 2022主要会议所接受!我们将数据集与微调模型一起发布,并在论文,存储库和数据集页面上找到更多详细信息。 Twitter NER模型也已集成到TweetNLP中,并在此处提供演示。
通过PIP安装tner即可开始!
pip install tner| 描述 | 关联 |
|---|---|
| 模型登录和评估 | |
| 模型预测 | |
| 多语言NER工作流程 |
NER数据集包含每个分型的令牌和标签序列(通常是train / validation / test ),
{
'train' : {
'tokens' : [
[ '@paulwalk' , 'It' , "'s" , 'the' , 'view' , 'from' , 'where' , 'I' , "'m" , 'living' , 'for' , 'two' , 'weeks' , '.' , 'Empire' , 'State' , 'Building' , '=' , 'ESB' , '.' , 'Pretty' , 'bad' , 'storm' , 'here' , 'last' , 'evening' , '.' ],
[ 'From' , 'Green' , 'Newsfeed' , ':' , 'AHFA' , 'extends' , 'deadline' , 'for' , 'Sage' , 'Award' , 'to' , 'Nov' , '.' , '5' , 'http://tinyurl.com/24agj38' ], ...
],
'tags' : [
[ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 2 , 2 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ],
[ 0 , 0 , 0 , 0 , 3 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ], ...
]
},
'validation' : ...,
'test' : ...,
}用字典将标签映射到其索引( label2id )如下。
{ "O" : 0 , "B-ORG" : 1 , "B-MISC" : 2 , "B-PER" : 3 , "I-PER" : 4 , "B-LOC" : 5 , "I-ORG" : 6 , "I-MISC" : 7 , "I-LOC" : 8 }我们的HuggingFace组可用各种公共数据集,可以按照下面的方式使用(有关完整数据集列表,请参见数据集卡)。
from tner import get_dataset
data , label2id = get_dataset ( dataset = "tner/wnut2017" )用户可以指定多个数据集以获取串联数据集。
data , label2id = get_dataset ( dataset = [ "tner/conll2003" , "tner/ontonotes5" ])在串联数据集中,我们使用统一标签集来统一实体标签。这个想法是以统一格式在拥抱面上共享所有可用的NER数据集,因此,如果您希望在此处添加任何NER数据集!
为了超越公共数据集,用户可以通过将其格式化为Conll 2003 NER共享任务文件中所述的IOB格式来使用自己的数据集,其中所有数据文件都包含一个单词一个单词,带有代表句子边界的空行。在每行的末尾,都有一个标签,该标签说明当前单词是否在命名实体内部。标签还编码命名实体的类型。这是一个示例句子:
EU B-ORG
rejects O
German B-MISC
call O
to O
boycott O
British B-MISC
lamb O
. O
用o标记的单词在命名实体之外,i-xxx标签用于XXX类型的命名实体内部的单词。每当XXX类型的两个实体彼此隔壁时,第二个实体的第一个单词将被标记为B-XXX,以表明它启动了另一个实体。请查看示例自定义数据。这些自定义文件的加载方式与下面的拥抱面数据集相同。
from tner import get_dataset
data , label2id = get_dataset ( local_dataset = {
"train" : "examples/local_dataset_sample/train.txt" ,
"valid" : "examples/local_dataset_sample/train.txt" ,
"test" : "examples/local_dataset_sample/test.txt"
})与HuggingFace数据集相同,可以连接数据集。
data , label2id = get_dataset ( local_dataset = [
{ "train" : "..." , "valid" : "..." , "test" : "..." },
{ "train" : "..." , "valid" : "..." , "test" : "..." }
]
)如上表所示,T-NER当前在HuggingFace组上共享了100多个NER模型,该表仅报告主要模型,并查看Model_card以获取完整模型列表。所有模型都可以与tner一起使用如下。
from tner import TransformersNER
model = TransformersNER ( "tner/roberta-large-wnut2017" ) # provide model alias on huggingface
output = model . predict ([ "Jacob Collier is a Grammy awarded English artist from London" ]) # give a list of sentences (or tokenized sentence)
print ( output )
{
'prediction' : [[ 'B-person' , 'I-person' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'O' , 'B-location' ]],
'probability' : [[ 0.9967652559280396 , 0.9994561076164246 , 0.9986955523490906 , 0.9947081804275513 , 0.6129112243652344 , 0.9984312653541565 , 0.9868122935295105 , 0.9983410835266113 , 0.9995284080505371 , 0.9838910698890686 ]],
'input' : [[ 'Jacob' , 'Collier' , 'is' , 'a' , 'Grammy' , 'awarded' , 'English' , 'artist' , 'from' , 'London' ]],
'entity_prediction' : [[
{ 'type' : 'person' , 'entity' : [ 'Jacob' , 'Collier' ], 'position' : [ 0 , 1 ], 'probability' : [ 0.9967652559280396 , 0.9994561076164246 ]},
{ 'type' : 'location' , 'entity' : [ 'London' ], 'position' : [ 9 ], 'probability' : [ 0.9838910698890686 ]}
]]
} model.predict选出句子列表和批处理大小batch_size ,并通过半空间或separator指定的符号将句子归为句子,该句子在其输出对象中返回为input 。可选地,用户可以通过任何令牌(Spacy,nltk等)事先进行输入,并且预测将遵循令牌化。
output = model . predict ([[ "Jacob Collier" , "is" , "a" , "Grammy awarded" , "English artist" , "from" , "London" ]])
print ( output )
{
'prediction' : [[ 'B-person' , 'O' , 'O' , 'O' , 'O' , 'O' , 'B-location' ]],
'probability' : [[ 0.9967652559280396 , 0.9986955523490906 , 0.9947081804275513 , 0.6129112243652344 , 0.9868122935295105 , 0.9995284080505371 , 0.9838910698890686 ]],
'input' : [[ 'Jacob Collier' , 'is' , 'a' , 'Grammy awarded' , 'English artist' , 'from' , 'London' ]],
'entity_prediction' : [[
{ 'type' : 'person' , 'entity' : [ 'Jacob Collier' ], 'position' : [ 0 ], 'probability' : [ 0.9967652559280396 ]},
{ 'type' : 'location' , 'entity' : [ 'London' ], 'position' : [ 6 ], 'probability' : [ 0.9838910698890686 ]}
]]
}可以指定本地模型检查点,而不是模型别名TransformersNER("path-to-checkpoint") 。重新生产这些已发布模型的脚本在这里。
以下命令行工具可用于模型预测。
tner-predict [-h] -m MODEL
command line tool to test finetuned NER model
optional arguments:
-h, --help show this help message and exit
-m MODEL, --model MODEL
model alias of huggingface or local checkpointtner-predict -m " tner/roberta-large-wnut2017 " 要安装依赖项以运行Web应用程序,请在安装时添加选项。
pip install tner[app]然后,克隆存储库
git clone https://github.com/asahi417/tner
cd tner并启动服务器。
uvicorn app:app --reload --log-level debug --host 0.0.0.0 --port 8000准备就绪后打开浏览器http://0.0.0.0:8000。您可以指定通过环境变量NER_MODEL部署的模型,该模型默认设置为tner/roberta-large-wnut2017 。 NER_MODEL可以是您本地模型检查点目录的路径,也可以是变形金刚模型中心上的模型名称。
确认应用程序接口受此存储库的启发。
T-NER提供了一个简单的API,可以通过上述有效的参数搜索在NER上进行语言模型进行微调。它由2个阶段组成:(i)对所有模型的验证集对小时期的所有可能配置进行微调,并计算评估度量度(默认为默认值),以及(ii)拾取顶部K型号以继续进行微调直至L Epoch。第二阶段的最佳模型将继续进行微调,直到验证度量减少为止。
使用tner的几行可以在几行中实现使用两阶段参数搜索的微调。
from tner import GridSearcher
searcher = GridSearcher (
checkpoint_dir = './ckpt_tner' ,
dataset = "tner/wnut2017" , # either of `dataset` (huggingface dataset) or `local_dataset` (custom dataset) should be given
model = "roberta-large" , # language model to fine-tune
epoch = 10 , # the total epoch (`L` in the figure)
epoch_partial = 5 , # the number of epoch at 1st stage (`M` in the figure)
n_max_config = 3 , # the number of models to pass to 2nd stage (`K` in the figure)
batch_size = 16 ,
gradient_accumulation_steps = [ 4 , 8 ],
crf = [ True , False ],
lr = [ 1e-4 , 1e-5 ],
weight_decay = [ 1e-7 ],
random_seed = [ 42 ],
lr_warmup_step_ratio = [ 0.1 ],
max_grad_norm = [ 10 ]
)
searcher . train ()以下参数目前是可调的。
gradient_accumulation_steps :梯度积累的数量crf :在输出嵌入式上使用CRFlr :学习率weight_decay :重量衰减系数random_seed :随机种子lr_warmup_step_ratio :学习率的线性热身比),例如,如果是0.3,则学习率将线性热身到总步骤的30%(毕竟没有衰减)max_grad_norm :梯度剪辑的规范有关每个参数的更多信息,请参见来源。
以下命令行工具可用于微调。
tner-train-search [-h] -c CHECKPOINT_DIR [-d DATASET [DATASET ...]] [-l LOCAL_DATASET [LOCAL_DATASET ...]]
[--dataset-name DATASET_NAME [DATASET_NAME ...]] [-m MODEL] [-b BATCH_SIZE] [-e EPOCH] [--max-length MAX_LENGTH] [--use-auth-token]
[--dataset-split-train DATASET_SPLIT_TRAIN] [--dataset-split-valid DATASET_SPLIT_VALID] [--lr LR [LR ...]]
[--random-seed RANDOM_SEED [RANDOM_SEED ...]] [-g GRADIENT_ACCUMULATION_STEPS [GRADIENT_ACCUMULATION_STEPS ...]]
[--weight-decay WEIGHT_DECAY [WEIGHT_DECAY ...]] [--lr-warmup-step-ratio LR_WARMUP_STEP_RATIO [LR_WARMUP_STEP_RATIO ...]]
[--max-grad-norm MAX_GRAD_NORM [MAX_GRAD_NORM ...]] [--crf CRF [CRF ...]] [--optimizer-on-cpu] [--n-max-config N_MAX_CONFIG]
[--epoch-partial EPOCH_PARTIAL] [--max-length-eval MAX_LENGTH_EVAL]
Fine-tune transformers on NER dataset with Robust Parameter Search
optional arguments:
-h , --help show this help message and exit
-c CHECKPOINT_DIR, --checkpoint-dir CHECKPOINT_DIR
checkpoint directory
-d DATASET [DATASET ...], --dataset DATASET [DATASET ...]
dataset name (or a list of it) on huggingface tner organization eg. ' tner/conll2003 ' [ ' tner/conll2003 ' , ' tner/ontonotes5 ' ]] see
https://huggingface.co/datasets ? search = tner for full dataset list
-l LOCAL_DATASET [LOCAL_DATASET ...], --local-dataset LOCAL_DATASET [LOCAL_DATASET ...]
a dictionary (or a list) of paths to local BIO files eg.{ " train " : " examples/local_dataset_sample/train.txt " , " test " :
" examples/local_dataset_sample/test.txt " }
--dataset-name DATASET_NAME [DATASET_NAME ...]
[optional] data name of huggingface dataset (should be same length as the ` dataset ` )
-m MODEL, --model MODEL
model name of underlying language model (huggingface model)
-b BATCH_SIZE, --batch-size BATCH_SIZE
batch size
-e EPOCH, --epoch EPOCH
the number of epoch
--max-length MAX_LENGTH
max length of language model
--use-auth-token Huggingface transformers argument of ` use_auth_token `
--dataset-split-train DATASET_SPLIT_TRAIN
dataset split to be used for training ( ' train ' as default)
--dataset-split-valid DATASET_SPLIT_VALID
dataset split to be used for validation ( ' validation ' as default)
--lr LR [LR ...] learning rate
--random-seed RANDOM_SEED [RANDOM_SEED ...]
random seed
-g GRADIENT_ACCUMULATION_STEPS [GRADIENT_ACCUMULATION_STEPS ...], --gradient-accumulation-steps GRADIENT_ACCUMULATION_STEPS [GRADIENT_ACCUMULATION_STEPS ...]
the number of gradient accumulation
--weight-decay WEIGHT_DECAY [WEIGHT_DECAY ...]
coefficient of weight decay (set 0 for None)
--lr-warmup-step-ratio LR_WARMUP_STEP_RATIO [LR_WARMUP_STEP_RATIO ...]
linear warmup ratio of learning rate (no decay).eg) if it ' s 0.3, the learning rate will warmup linearly till 30% of the total step
(set 0 for None)
--max-grad-norm MAX_GRAD_NORM [MAX_GRAD_NORM ...]
norm for gradient clipping (set 0 for None)
--crf CRF [CRF ...] use CRF on top of output embedding (0 or 1)
--optimizer-on-cpu put optimizer on CPU to save memory of GPU
--n-max-config N_MAX_CONFIG
the number of configs to run 2nd phase search
--epoch-partial EPOCH_PARTIAL
the number of epoch for 1st phase search
--max-length-eval MAX_LENGTH_EVAL
max length of language model at evaluationtner-train-search -m " roberta-large " -c " ckpt " -d " tner/wnut2017 " -e 15 --epoch-partial 5 --n-max-config 3 -b 64 -g 1 2 --lr 1e-6 1e-5 --crf 0 1 --max-grad-norm 0 10 --weight-decay 0 1e-7NER模型的评估是通过model.evaluate完成的,该功能将dataset或local_dataset作为用于评估的数据集。
from tner import TransformersNER
model = TransformersNER ( "tner/roberta-large-wnut2017" ) # provide model alias on huggingface
# huggingface dataset
metric = model . evaluate ( 'tner/wnut2017' , dataset_split = 'test' )
# local dataset
metric = model . evaluate ( local_dataset = { "test" : "examples/local_dataset_sample/test.txt" }, dataset_split = 'test' )可以在此处找到输出对象metric的示例。
为了更好地理解室外准确性,我们提供了实体跨度预测管道,该管道忽略了实体类型和计算指标,仅在IOB实体位置(二进制序列标记)上。
metric = model . evaluate ( datasets = 'tner/wnut2017' , dataset_split = 'test' , span_detection_mode = True )以下命令行工具可用于模型预测。
tner-evaluate [-h] -m MODEL -e EXPORT [-d DATASET [DATASET ...]] [-l LOCAL_DATASET [LOCAL_DATASET ...]]
[--dataset-name DATASET_NAME [DATASET_NAME ...]] [--dataset-split DATASET_SPLIT] [--span-detection-mode] [--return-ci] [-b BATCH_SIZE]
Evaluate NER model
optional arguments:
-h , --help show this help message and exit
-m MODEL, --model MODEL
model alias of huggingface or local checkpoint
-e EXPORT, --export EXPORT
file to export the result
-d DATASET [DATASET ...], --dataset DATASET [DATASET ...]
dataset name (or a list of it) on huggingface tner organization eg. ' tner/conll2003 ' [ ' tner/conll2003 ' , ' tner/ontonotes5 ' ]] see
https://huggingface.co/datasets ? search = tner for full dataset list
-l LOCAL_DATASET [LOCAL_DATASET ...], --local-dataset LOCAL_DATASET [LOCAL_DATASET ...]
a dictionary (or a list) of paths to local BIO files eg.{ " train " : " examples/local_dataset_sample/train.txt " , " test " :
" examples/local_dataset_sample/test.txt " }
--dataset-name DATASET_NAME [DATASET_NAME ...]
[optional] data name of huggingface dataset (should be same length as the ` dataset ` )
--dataset-split DATASET_SPLIT
dataset split to be used for test ( ' test ' as default)
--span-detection-mode
return F1 of entity span detection (ignoring entity type error and cast as binary sequence classification as below)- NER : [ " O " ,
" B-PER " , " I-PER " , " O " , " B-LOC " , " O " , " B-ORG " ]- Entity-span detection: [ " O " , " B-ENT " , " I-ENT " , " O " , " B-ENT " , " O " , " B-ENT " ]
--return-ci return confidence interval by bootstrap
-b BATCH_SIZE, --batch-size BATCH_SIZE
batch sizetner-evaluate -m " tner/roberta-large-wnut2017 " -e " metric.json " -d " tner/conll2003 " -b " 32 " 如果您使用这些资源中的任何一个,请引用以下论文:
@inproceedings{ushio-camacho-collados-2021-ner,
title = "{T}-{NER}: An All-Round Python Library for Transformer-based Named Entity Recognition",
author = "Ushio, Asahi and
Camacho-Collados, Jose",
booktitle = "Proceedings of the 16th Conference of the European Chapter of the Association for Computational Linguistics: System Demonstrations",
month = apr,
year = "2021",
address = "Online",
publisher = "Association for Computational Linguistics",
url = "https://www.aclweb.org/anthology/2021.eacl-demos.7",
pages = "53--62",
abstract = "Language model (LM) pretraining has led to consistent improvements in many NLP downstream tasks, including named entity recognition (NER). In this paper, we present T-NER (Transformer-based Named Entity Recognition), a Python library for NER LM finetuning. In addition to its practical utility, T-NER facilitates the study and investigation of the cross-domain and cross-lingual generalization ability of LMs finetuned on NER. Our library also provides a web app where users can get model predictions interactively for arbitrary text, which facilitates qualitative model evaluation for non-expert programmers. We show the potential of the library by compiling nine public NER datasets into a unified format and evaluating the cross-domain and cross- lingual performance across the datasets. The results from our initial experiments show that in-domain performance is generally competitive across datasets. However, cross-domain generalization is challenging even with a large pretrained LM, which has nevertheless capacity to learn domain-specific features if fine- tuned on a combined dataset. To facilitate future research, we also release all our LM checkpoints via the Hugging Face model hub.",
}