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.",
}