Chapter 11
基于Transformers的命名实体识别
Notebooktransformers36 cells
基于Transformers的命名实体识别
Step1 导入相关包
In [ ]python · cell 3
python
import evaluate
from datasets import load_dataset
from transformers import AutoTokenizer, AutoModelForTokenClassification, TrainingArguments, Trainer, DataCollatorForTokenClassificationStep2 加载数据集
In [ ]python · cell 5
python
# 如果可以联网,直接使用load_dataset进行加载
#ner_datasets = load_dataset("peoples_daily_ner", cache_dir="./data")
# 如果无法联网,则使用下面的方式加载数据集
from datasets import DatasetDict
ner_datasets = DatasetDict.load_from_disk("ner_data")
ner_datasetsIn [ ]python · cell 6
python
ner_datasets["train"][0]In [ ]python · cell 7
python
ner_datasets["train"].featuresIn [ ]python · cell 8
python
label_list = ner_datasets["train"].features["ner_tags"].feature.names
label_listStep3 数据集预处理
In [ ]python · cell 10
python
tokenizer = AutoTokenizer.from_pretrained("hfl/chinese-macbert-base")In [ ]python · cell 11
python
tokenizer(ner_datasets["train"][0]["tokens"], is_split_into_words=True) # 对于已经做好tokenize的数据,要指定is_split_into_words参数为TrueIn [ ]python · cell 12
python
res = tokenizer("interesting word")
resIn [ ]python · cell 13
python
res.word_ids()In [ ]python · cell 14
python
# 借助word_ids 实现标签映射
def process_function(examples):
tokenized_exmaples = tokenizer(examples["tokens"], max_length=128, truncation=True, is_split_into_words=True)
labels = []
for i, label in enumerate(examples["ner_tags"]):
word_ids = tokenized_exmaples.word_ids(batch_index=i)
label_ids = []
for word_id in word_ids:
if word_id is None:
label_ids.append(-100)
else:
label_ids.append(label[word_id])
labels.append(label_ids)
tokenized_exmaples["labels"] = labels
return tokenized_exmaplesIn [ ]python · cell 15
python
tokenized_datasets = ner_datasets.map(process_function, batched=True)
tokenized_datasetsIn [ ]python · cell 16
python
print(tokenized_datasets["train"][0])Step4 创建模型
In [ ]python · cell 18
python
# 对于所有的非二分类任务,切记要指定num_labels,否则就会device错误
model = AutoModelForTokenClassification.from_pretrained("hfl/chinese-macbert-base", num_labels=len(label_list))In [ ]python · cell 19
python
model.config.num_labelsStep5 创建评估函数
In [ ]python · cell 21
python
# 这里方便大家加载,替换成了本地的加载方式,无需额外下载
seqeval = evaluate.load("seqeval_metric.py")
seqevalIn [ ]python · cell 22
python
import numpy as np
def eval_metric(pred):
predictions, labels = pred
predictions = np.argmax(predictions, axis=-1)
# 将id转换为原始的字符串类型的标签
true_predictions = [
[label_list[p] for p, l in zip(prediction, label) if l != -100]
for prediction, label in zip(predictions, labels)
]
true_labels = [
[label_list[l] for p, l in zip(prediction, label) if l != -100]
for prediction, label in zip(predictions, labels)
]
result = seqeval.compute(predictions=true_predictions, references=true_labels, mode="strict", scheme="IOB2")
return {
"f1": result["overall_f1"]
}
Step6 配置训练参数
In [ ]python · cell 24
python
args = TrainingArguments(
output_dir="models_for_ner",
per_device_train_batch_size=64,
per_device_eval_batch_size=128,
eval_strategy="epoch",
save_strategy="epoch",
metric_for_best_model="f1",
load_best_model_at_end=True,
logging_steps=50,
num_train_epochs=1
)Step7 创建训练器
In [ ]python · cell 26
python
trainer = Trainer(
model=model,
args=args,
tokenizer=tokenizer,
train_dataset=tokenized_datasets["train"],
eval_dataset=tokenized_datasets["validation"],
compute_metrics=eval_metric,
data_collator=DataCollatorForTokenClassification(tokenizer=tokenizer)
)Step8 模型训练
In [ ]python · cell 28
python
trainer.train()In [ ]python · cell 29
python
trainer.evaluate(eval_dataset=tokenized_datasets["test"])Step9 模型预测
In [ ]python · cell 31
python
from transformers import pipelineIn [ ]python · cell 32
python
# 使用pipeline进行推理,要指定id2label
model.config.id2label = {idx: label for idx, label in enumerate(label_list)}
model.configIn [ ]python · cell 33
python
# 如果模型是基于GPU训练的,那么推理时要指定device
# 对于NER任务,可以指定aggregation_strategy为simple,得到具体的实体的结果,而不是token的结果
ner_pipe = pipeline("token-classification", model=model, tokenizer=tokenizer, device=0, aggregation_strategy="simple")In [ ]python · cell 34
python
res = ner_pipe("小明在北京上班")
resIn [ ]python · cell 35
python
# 根据start和end取实际的结果
ner_result = {}
x = "小明在北京上班"
for r in res:
if r["entity_group"] not in ner_result:
ner_result[r["entity_group"]] = []
ner_result[r["entity_group"]].append(x[r["start"]: r["end"]])
ner_resultIn [ ]python · cell 36
python
