🚀 BERT for Chinese Named Entity Recognition(bert4ner) Model
中国語の固有表現抽出(NER)に特化したBERTベースのモデルで、高精度な固有表現抽出を実現します。
bert4ner-base-chinese
モデルによるPEOPLE(人民日报) テストデータの評価結果:
BERTのPEOPLEテストデータにおける全体的な性能:
|
正解率 |
再現率 |
F1値 |
BertSoftmax |
0.9425 |
0.9627 |
0.9525 |
PEOPLEのテストセットでは、ほぼSOTAレベルの性能を達成しています。
BertSoftmaxのネットワーク構造(オリジナルのBERT):

🚀 クイックスタート
このプロジェクトは固有表現抽出プロジェクト nerpy でオープンソース化されており、bert4nerモデルをサポートしています。以下のコマンドで呼び出すことができます:
>>> from nerpy import NERModel
>>> model = NERModel("bert", "shibing624/bert4ner-base-chinese")
>>> predictions, raw_outputs, entities = model.predict(["常建良,男,1963年出生,工科学士,高级工程师"], split_on_space=False)
entities: [('常建良', 'PER'), ('1963年', 'TIME')]
モデルファイルの構成:
bert4ner-base-chinese
├── config.json
├── model_args.json
├── pytorch_model.bin
├── special_tokens_map.json
├── tokenizer_config.json
└── vocab.txt
💻 使用例
基本的な使用法
nerpy を使用せずに、以下のようにモデルを使用することができます。
まず、入力をTransformerモデルに通し、次にBioタグを適用して固有表現を取得します。
パッケージのインストール:
pip install transformers seqeval
import os
import torch
from transformers import AutoTokenizer, AutoModelForTokenClassification
from seqeval.metrics.sequence_labeling import get_entities
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
tokenizer = AutoTokenizer.from_pretrained("shibing624/bert4ner-base-chinese")
model = AutoModelForTokenClassification.from_pretrained("shibing624/bert4ner-base-chinese")
label_list = ['I-ORG', 'B-LOC', 'O', 'B-ORG', 'I-LOC', 'I-PER', 'B-TIME', 'I-TIME', 'B-PER']
sentence = "王宏伟来自北京,是个警察,喜欢去王府井游玩儿。"
def get_entity(sentence):
tokens = tokenizer.tokenize(sentence)
inputs = tokenizer.encode(sentence, return_tensors="pt")
with torch.no_grad():
outputs = model(inputs).logits
predictions = torch.argmax(outputs, dim=2)
char_tags = [(token, label_list[prediction]) for token, prediction in zip(tokens, predictions[0].numpy())][1:-1]
print(sentence)
print(char_tags)
pred_labels = [i[1] for i in char_tags]
entities = []
line_entities = get_entities(pred_labels)
for i in line_entities:
word = sentence[i[1]: i[2] + 1]
entity_type = i[0]
entities.append((word, entity_type))
print("Sentence entity:")
print(entities)
get_entity(sentence)
出力結果:
王宏伟来自北京,是个警察,喜欢去王府井游玩儿。
[('王', 'B-PER'), ('宏', 'I-PER'), ('伟', 'I-PER'), ('来', 'O'), ('自', 'O'), ('北', 'B-LOC'), ('京', 'I-LOC'), (',', 'O'), ('是', 'O'), ('个', 'O'), ('警', 'O'), ('察', 'O'), (',', 'O'), ('喜', 'O'), ('欢', 'O'), ('去', 'O'), ('王', 'B-LOC'), ('府', 'I-LOC'), ('井', 'I-LOC'), ('游', 'O'), ('玩', 'O'), ('儿', 'O'), ('。', 'O')]
Sentence entity:
[('王宏伟', 'PER'), ('北京', 'LOC'), ('王府井', 'LOC')]
訓練データセット
中国語固有表現抽出データセット
データセット |
コーパス |
ダウンロードリンク |
ファイルサイズ |
CNER中国語固有表現抽出データセット |
CNER(12万字) |
CNER github |
1.1MB |
PEOPLE中国語固有表現抽出データセット |
人民日報データセット(200万字) |
PEOPLE github |
12.8MB |
CNER中国語固有表現抽出データセットのデータ形式:
美 B-LOC
国 I-LOC
的 O
华 B-PER
莱 I-PER
士 I-PER
我 O
跟 O
他 O
bert4nerモデルを訓練する場合は、https://github.com/shibing624/nerpy/tree/main/examples を参照してください。
📄 ライセンス
このプロジェクトはApache-2.0ライセンスの下で公開されています。
📚 引用
@software{nerpy,
author = {Xu Ming},
title = {nerpy: Named Entity Recognition toolkit},
year = {2022},
url = {https://github.com/shibing624/nerpy},
}