🚀 Flair英文命名实体识别(大模型)
本项目是一个用于英文的大型4类别命名实体识别(NER)模型,它是 Flair 库的一部分。该模型能有效识别英文文本中的命名实体,为信息提取等自然语言处理任务提供支持。
✨ 主要特性
- 高准确率:在修正后的CoNLL - 03数据集上,F1分数达到 94.36。
- 多标签预测:可预测4种标签,涵盖人物、地点、组织和其他类型的命名实体。
- 先进技术:基于文档级XLM - R嵌入和 FLERT 技术。
预测标签详情
标签 |
含义 |
PER |
人名 |
LOC |
地名 |
ORG |
组织机构名 |
MISC |
其他名称 |
🚀 快速开始
环境要求
需要安装 Flair 库,可以使用以下命令进行安装:
pip install flair
代码示例
from flair.data import Sentence
from flair.models import SequenceTagger
tagger = SequenceTagger.load("flair/ner-english-large")
sentence = Sentence("George Washington went to Washington")
tagger.predict(sentence)
print(sentence)
print('The following NER tags are found:')
for entity in sentence.get_spans('ner'):
print(entity)
输出示例
Span [1,2]: "George Washington" [− Labels: PER (1.0)]
Span [5]: "Washington" [− Labels: LOC (1.0)]
在句子 "George Washington went to Washington" 中,成功识别出实体 "George Washington"(标记为 人物)和 "Washington"(标记为 地点)。
📚 详细文档
模型训练脚本
以下是用于训练此模型的Flair脚本:
import torch
from flair.datasets import CONLL_03
corpus = CONLL_03()
tag_type = 'ner'
tag_dictionary = corpus.make_tag_dictionary(tag_type=tag_type)
from flair.embeddings import TransformerWordEmbeddings
embeddings = TransformerWordEmbeddings(
model='xlm-roberta-large',
layers="-1",
subtoken_pooling="first",
fine_tune=True,
use_context=True,
)
from flair.models import SequenceTagger
tagger = SequenceTagger(
hidden_size=256,
embeddings=embeddings,
tag_dictionary=tag_dictionary,
tag_type='ner',
use_crf=False,
use_rnn=False,
reproject_embeddings=False,
)
from flair.trainers import ModelTrainer
trainer = ModelTrainer(tagger, corpus, optimizer=torch.optim.AdamW)
from torch.optim.lr_scheduler import OneCycleLR
trainer.train('resources/taggers/ner-english-large',
learning_rate=5.0e-6,
mini_batch_size=4,
mini_batch_chunk_size=1,
max_epochs=20,
scheduler=OneCycleLR,
embeddings_storage_mode='none',
weight_decay=0.,
)
📄 许可证
文档中未提及许可证相关信息。
📖 引用
使用此模型时,请引用以下论文:
@misc{schweter2020flert,
title={FLERT: Document-Level Features for Named Entity Recognition},
author={Stefan Schweter and Alan Akbik},
year={2020},
eprint={2011.06993},
archivePrefix={arXiv},
primaryClass={cs.CL}
}
❓ 问题反馈
如果你在使用过程中遇到问题,可以在 Flair问题跟踪器 中反馈。