T-NER هي أداة Python لنماذج اللغة في حالة التعرف على الإدراك المسماة (NER) المنفذة في Pytorch ، المتاحة عبر PIP. إنه يحتوي على واجهة سهلة لنماذج Finetune واختبارها على مجموعات بيانات متعددة اللغات. يدمج T-NER حاليًا التغطية العالية لمجموعات بيانات NER المتاحة للجمهور ويمكّن التكامل السهل لمجموعات البيانات المخصصة. يمكن نشر جميع النماذج التي تم تحريكها باستخدام T-ner على تطبيق الويب الخاص بنا للتصور. تم قبول ورقتنا التي توضح T-NER في EACL 2021. يتم مشاركة جميع النماذج ومجموعات البيانات عبر مجموعة T-NER Huggingface.
جديد (سبتمبر 2022): أصدرنا مجموعة بيانات NER جديدة استنادًا إلى Twitter tweetner7 وتم قبول الورقة في المؤتمر الرئيسي AACL 2022! نقوم بإصدار مجموعة البيانات جنبًا إلى جنب مع النماذج التي يتم ضبطها ، ويمكن العثور على مزيد من التفاصيل في صفحة الورق والمستودع ومجموعة البيانات. تم دمج طراز Twitter NER أيضًا في TweetNLP ، ويتوفر عرض تجريبي هنا.
تثبيت tner عبر PIP للبدء!
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 }تتوفر مجموعة متنوعة من مجموعات بيانات NER العامة على مجموعة Huggingface الخاصة بنا ، والتي يمكن استخدامها على النحو التالي (انظر بطاقة Dataset لقوائم مجموعات البيانات الكاملة).
from tner import get_dataset
data , label2id = get_dataset ( dataset = "tner/wnut2017" )يمكن للمستخدم تحديد مجموعات بيانات متعددة للحصول على مجموعة بيانات متسلسلة.
data , label2id = get_dataset ( dataset = [ "tner/conll2003" , "tner/ontonotes5" ])في مجموعات البيانات المتسلسلة ، نستخدم التسمية الموحدة لتوحيد ملصق الكيان. تتمثل الفكرة في مشاركة جميع مجموعات بيانات NER المتاحة على Luggingface بتنسيق موحد ، لذلك أخبرنا إذا كنت تريد إضافة أي مجموعات بيانات NER هناك!
لتجاوز مجموعات البيانات العامة ، يمكن للمستخدمين استخدام مجموعات البيانات الخاصة بهم عن طريق تنسيقها في تنسيق IOB الموضح في ورقة المهمة المشتركة Conll 2003 ، حيث تحتوي جميع ملفات البيانات على كلمة واحدة لكل سطر مع خطوط فارغة تمثل حدود الجملة. في نهاية كل سطر ، توجد علامة تنص على ما إذا كانت الكلمة الحالية داخل كيان مسمى أم لا. تُشفر العلامة أيضًا نوع الكيان المسماة. هنا مثال جملة:
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 لإظهار أنها تبدأ كيانًا آخر. يرجى أخذ نموذج بيانات مخصصة. يمكن تحميل هذه الملفات المخصصة بنفس طريقة مجموعة بيانات HuggingFace على النحو التالي.
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 حاليًا بمشاركة أكثر من 100 نموذج NER على مجموعة Huggingface ، كما هو موضح في الجدول أعلاه ، والذي يبلغ عن النماذج الرئيسية فقط ويرى 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 ]}
]]
} batch_size model.predict 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 " لتثبيت التبعيات لتشغيل تطبيق الويب ، أضف الخيار عند التثبيت.
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 إما مسار إلى دليل نقطة التفتيش النموذجية المحلية أو اسم النموذج على محولات Transformers Model.
شكر وتقدير أن واجهة التطبيق مستوحاة بشكل كبير من هذا المستودع.
يوفر T-NER واجهة برمجة تطبيقات سهلة لتشغيل النموذج اللغوي على NER مع بحث معلمة فعال كما هو موضح أعلاه. يتكون من مرحلتين: (1) صقل مع كل تكوينات ممكنة لعصر صغير وحساب مقياس التقييم (Micro F1 كإعداد افتراضي) على مجموعة التحقق من الصحة لجميع النماذج ، و (2) التقاط النماذج من أفضل K لمواصلة الضبط الدقيق حتى L . سيستمر أفضل نموذج في المرحلة الثانية في صقله حتى ينخفض مقياس التحقق من الصحة.
يمكن تحقيق هذا البحث الدقيق مع البحث عن المعلمة على مرحلتين في بضعة أسطر مع 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 : استخدم CRF أعلى تضمين الإخراجlr : معدل التعلم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-7 local_dataset تقييم نماذج NER dataset model.evaluate .
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.",
}