from google.colab import drive |
Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).
import os |
import os |
#加载处理完的npz数据集 |
数据从npz格式加载到pandas,标签用数字替换,以便输入模型
#转换为dataframe格式 |
words | labels0 | |
---|---|---|
0 | [彭, 小, 军, 认, 为, ,, 国, 内, 银, 行, 现, 在, 走, 的, 是, ... | [7, 17, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0... |
1 | [温, 格, 的, 球, 队, 终, 于, 又, 踢, 了, 一, 场, 经, 典, 的, ... | [7, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,... |
2 | [突, 袭, 黑, 暗, 雅, 典, 娜, 》, 中, R, i, d, d, i, c, ... | [4, 14, 14, 14, 14, 14, 14, 14, 0, 7, 17, 17, ... |
3 | [郑, 阿, 姨, 就, 赶, 到, 文, 汇, 路, 排, 队, 拿, 钱, ,, 希, ... | [0, 0, 0, 0, 0, 0, 1, 11, 11, 0, 0, 0, 0, 0, 0... |
4 | [我, 想, 站, 在, 雪, 山, 脚, 下, 你, 会, 被, 那, 巍, 峨, 的, ... | [0, 0, 0, 0, 10, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0... |
... | ... | ... |
1338 | [在, 这, 个, 非, 常, 喜, 庆, 的, 日, 子, 里, ,, 我, 们, 首, ... | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... |
1339 | [姜, 哲, 中, :, 公, 共, 之, 敌, 1, -, 1, 》, 、, 《, 神, ... | [6, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16... |
1340 | [目, 前, ,, 日, 本, 松, 山, 海, 上, 保, 安, 部, 正, 在, 就, ... | [0, 0, 0, 5, 15, 15, 15, 15, 15, 15, 15, 15, 0... |
1341 | [也, 就, 是, 说, 英, 国, 人, 在, 世, 博, 会, 上, 的, 英, 国, ... | [0, 0, 0, 0, 0, 0, 0, 0, 10, 20, 20, 0, 0, 0, ... |
1342 | [另, 外, 意, 大, 利, 的, P, l, a, y, G, e, n, e, r, ... | [0, 0, 0, 0, 0, 0, 2, 12, 12, 12, 12, 12, 12, ... |
1343 rows × 2 columns
word_ids可以将每一个subtokens位置对应一个word的下标。并且特殊字符对应了None。有了这个list,我们就能将subtokens和words还有标注的labels对齐啦,并将[cls]和[sep]位置的标签用-100填充。-100经过softmax会被忽略。
""" |
from datasets import Dataset |
#加载模型 |
解读BertForTokenClassification任务头(说明代码,不需要运行)
计算loss时忽略padding部分(即只算attention_mask==1部分)步骤为:
sequence_output=outputs[0]#bert输出取第一维,即每个tokens的隐向量 |
label2id = { |
设置seqeval评测方法,需要注意以下几点
- 选择预测分类最大概率的下标
- 将数字下标转化为BIOS格式的label,因为seqeval除了总的指标,还可以查看各个类别的指标。如果只是数字,运行时会有异常提示(但正常运行)
- 忽略-100所在地方,即特殊tokens的位置
- 这步和token分类任务头合起来,就将pad部位和特殊tokens部分都忽略loss计算了。
from datasets import load_metric |
Downloading: 0%| | 0.00/2.48k [00:00<?, ?B/s]
batch_size=32 |
#进行训练 |
The following columns in the training set don't have a corresponding argument in `BertForTokenClassification.forward` and have been ignored: words, labels0, __index_level_0__.
***** Running training *****
Num examples = 10748
Num Epochs = 8
Instantaneous batch size per device = 32
Total train batch size (w. parallel, distributed & accumulation) = 32
Gradient Accumulation steps = 1
Total optimization steps = 2688
<div>
<progress value='2688' max='2688' style='width:300px; height:20px; vertical-align: middle;'></progress>
[2688/2688 1:34:02, Epoch 8/8]
</div>
<table border="1" class="dataframe">
Epoch
Training Loss
Validation Loss
Precision
Recall
F1
Accuracy
</table>
The following columns in the evaluation set don't have a corresponding argument in `BertForTokenClassification.forward` and have been ignored: words, labels0.
***** Running Evaluation *****
Num examples = 1343
Batch size = 32
Saving model checkpoint to bert_softmax/checkpoint-336
Configuration saved in bert_softmax/checkpoint-336/config.json
Model weights saved in bert_softmax/checkpoint-336/pytorch_model.bin
tokenizer config file saved in bert_softmax/checkpoint-336/tokenizer_config.json
Special tokens file saved in bert_softmax/checkpoint-336/special_tokens_map.json
The following columns in the evaluation set don't have a corresponding argument in `BertForTokenClassification.forward` and have been ignored: words, labels0.
***** Running Evaluation *****
Num examples = 1343
Batch size = 32
Saving model checkpoint to bert_softmax/checkpoint-672
Configuration saved in bert_softmax/checkpoint-672/config.json
Model weights saved in bert_softmax/checkpoint-672/pytorch_model.bin
tokenizer config file saved in bert_softmax/checkpoint-672/tokenizer_config.json
Special tokens file saved in bert_softmax/checkpoint-672/special_tokens_map.json
The following columns in the evaluation set don't have a corresponding argument in `BertForTokenClassification.forward` and have been ignored: words, labels0.
***** Running Evaluation *****
Num examples = 1343
Batch size = 32
Saving model checkpoint to bert_softmax/checkpoint-1008
Configuration saved in bert_softmax/checkpoint-1008/config.json
Model weights saved in bert_softmax/checkpoint-1008/pytorch_model.bin
tokenizer config file saved in bert_softmax/checkpoint-1008/tokenizer_config.json
Special tokens file saved in bert_softmax/checkpoint-1008/special_tokens_map.json
The following columns in the evaluation set don't have a corresponding argument in `BertForTokenClassification.forward` and have been ignored: words, labels0.
***** Running Evaluation *****
Num examples = 1343
Batch size = 32
Saving model checkpoint to bert_softmax/checkpoint-1344
Configuration saved in bert_softmax/checkpoint-1344/config.json
Model weights saved in bert_softmax/checkpoint-1344/pytorch_model.bin
tokenizer config file saved in bert_softmax/checkpoint-1344/tokenizer_config.json
Special tokens file saved in bert_softmax/checkpoint-1344/special_tokens_map.json
The following columns in the evaluation set don't have a corresponding argument in `BertForTokenClassification.forward` and have been ignored: words, labels0.
***** Running Evaluation *****
Num examples = 1343
Batch size = 32
Saving model checkpoint to bert_softmax/checkpoint-1680
Configuration saved in bert_softmax/checkpoint-1680/config.json
Model weights saved in bert_softmax/checkpoint-1680/pytorch_model.bin
tokenizer config file saved in bert_softmax/checkpoint-1680/tokenizer_config.json
Special tokens file saved in bert_softmax/checkpoint-1680/special_tokens_map.json
The following columns in the evaluation set don't have a corresponding argument in `BertForTokenClassification.forward` and have been ignored: words, labels0.
***** Running Evaluation *****
Num examples = 1343
Batch size = 32
Saving model checkpoint to bert_softmax/checkpoint-2016
Configuration saved in bert_softmax/checkpoint-2016/config.json
Model weights saved in bert_softmax/checkpoint-2016/pytorch_model.bin
tokenizer config file saved in bert_softmax/checkpoint-2016/tokenizer_config.json
Special tokens file saved in bert_softmax/checkpoint-2016/special_tokens_map.json
The following columns in the evaluation set don't have a corresponding argument in `BertForTokenClassification.forward` and have been ignored: words, labels0.
***** Running Evaluation *****
Num examples = 1343
Batch size = 32
Saving model checkpoint to bert_softmax/checkpoint-2352
Configuration saved in bert_softmax/checkpoint-2352/config.json
Model weights saved in bert_softmax/checkpoint-2352/pytorch_model.bin
tokenizer config file saved in bert_softmax/checkpoint-2352/tokenizer_config.json
Special tokens file saved in bert_softmax/checkpoint-2352/special_tokens_map.json
The following columns in the evaluation set don't have a corresponding argument in `BertForTokenClassification.forward` and have been ignored: words, labels0.
***** Running Evaluation *****
Num examples = 1343
Batch size = 32
Saving model checkpoint to bert_softmax/checkpoint-2688
Configuration saved in bert_softmax/checkpoint-2688/config.json
Model weights saved in bert_softmax/checkpoint-2688/pytorch_model.bin
tokenizer config file saved in bert_softmax/checkpoint-2688/tokenizer_config.json
Special tokens file saved in bert_softmax/checkpoint-2688/special_tokens_map.json
Training completed. Do not forget to share your model on huggingface.co/models =)
TrainOutput(global_step=2688, training_loss=0.09796489925966376, metrics={'train_runtime': 5645.1208, 'train_samples_per_second': 15.232, 'train_steps_per_second': 0.476, 'total_flos': 8072824637823936.0, 'train_loss': 0.09796489925966376, 'epoch': 8.0})
如果想要得到单个类别的precision/recall/f1,我们直接将结果输入相同的评估函数即可:
#进行评估 |
import torch |
predictions,labels,loss=trainer.predict(tokenized_val_ds) |
#将结果排序查看 |
precision | recall | f1 | number | |
---|---|---|---|---|
address | 0.556627 | 0.619303 | 0.586294 | 373.000000 |
scene | 0.684211 | 0.746411 | 0.713959 | 209.000000 |
overall_precision | 0.741374 | 0.741374 | 0.741374 | 0.741374 |
organization | 0.713592 | 0.801090 | 0.754814 | 367.000000 |
book | 0.743902 | 0.792208 | 0.767296 | 154.000000 |
overall_f1 | 0.771585 | 0.771585 | 0.771585 | 0.771585 |
position | 0.753813 | 0.799076 | 0.775785 | 433.000000 |
company | 0.752427 | 0.820106 | 0.784810 | 378.000000 |
government | 0.738516 | 0.846154 | 0.788679 | 247.000000 |
overall_recall | 0.804362 | 0.804362 | 0.804362 | 0.804362 |
game | 0.808050 | 0.884746 | 0.844660 | 295.000000 |
movie | 0.858108 | 0.841060 | 0.849498 | 151.000000 |
name | 0.848671 | 0.892473 | 0.870021 | 465.000000 |
overall_accuracy | 0.941882 | 0.941882 | 0.941882 | 0.941882 |
#预测验证集结果并对比标签 |
The following columns in the test set don't have a corresponding argument in `BertForTokenClassification.forward` and have been ignored: words, labels0.
***** Running Prediction *****
Num examples = 1343
Batch size = 32
[42/42 31:38]
words | labels0 | preds | |
---|---|---|---|
0 | [彭, 小, 军, 认, 为, ,, 国, 内, 银, 行, 现, 在, 走, 的, 是, ... | [7, 17, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0... | [0, 7, 17, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0... |
1 | [温, 格, 的, 球, 队, 终, 于, 又, 踢, 了, 一, 场, 经, 典, 的, ... | [7, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,... | [0, 7, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,... |
2 | [突, 袭, 黑, 暗, 雅, 典, 娜, 》, 中, R, i, d, d, i, c, ... | [4, 14, 14, 14, 14, 14, 14, 14, 0, 7, 17, 17, ... | [0, 4, 14, 14, 14, 14, 14, 14, 14, 0, 7, 17, 1... |
3 | [郑, 阿, 姨, 就, 赶, 到, 文, 汇, 路, 排, 队, 拿, 钱, ,, 希, ... | [0, 0, 0, 0, 0, 0, 1, 11, 11, 0, 0, 0, 0, 0, 0... | [0, 0, 0, 0, 0, 0, 0, 1, 11, 11, 0, 0, 0, 0, 0... |
4 | [我, 想, 站, 在, 雪, 山, 脚, 下, 你, 会, 被, 那, 巍, 峨, 的, ... | [0, 0, 0, 0, 10, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0... | [0, 0, 0, 0, 0, 10, 20, 0, 0, 0, 0, 0, 0, 0, 0... |
... | ... | ... | ... |
1338 | [在, 这, 个, 非, 常, 喜, 庆, 的, 日, 子, 里, ,, 我, 们, 首, ... | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... |
1339 | [姜, 哲, 中, :, 公, 共, 之, 敌, 1, -, 1, 》, 、, 《, 神, ... | [6, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16... | [0, 7, 17, 17, 16, 16, 16, 16, 16, 16, 16, 16,... |
1340 | [目, 前, ,, 日, 本, 松, 山, 海, 上, 保, 安, 部, 正, 在, 就, ... | [0, 0, 0, 5, 15, 15, 15, 15, 15, 15, 15, 15, 0... | [0, 0, 0, 0, 5, 15, 15, 15, 15, 15, 15, 15, 15... |
1341 | [也, 就, 是, 说, 英, 国, 人, 在, 世, 博, 会, 上, 的, 英, 国, ... | [0, 0, 0, 0, 0, 0, 0, 0, 10, 20, 20, 0, 0, 0, ... | [0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 18, 18, 0, 0, 0... |
1342 | [另, 外, 意, 大, 利, 的, P, l, a, y, G, e, n, e, r, ... | [0, 0, 0, 0, 0, 0, 2, 12, 12, 12, 12, 12, 12, ... | [0, 0, 0, 0, 0, 0, 0, 2, 12, 12, 12, 12, 12, 1... |
1343 rows × 3 columns
#测试预测的结果,这部分不需要运行。 |
[[ 0 8 0 ... 0 0 0]
[ 0 5 15 ... 15 15 0]
[ 0 0 0 ... 0 14 0]
...
[ 0 0 0 ... 12 0 12]
[ 0 0 0 ... 16 0 0]
[ 0 8 18 ... 0 0 0]]
0 [29, 12, 1, 1, 42, 1, 1, 42, 1, 1, 23, 14, 4, ...
1 [28, 13, 17, 17, 1, 1, 17, 13, 17, 7, 13, 14, ...
2 [3, 3, 18, 6, 6, 6, 6, 5, 6, 6, 6, 4, 16, 16, ...
3 [5, 13, 22, 45, 39, 45, 10, 10, 24, 40, 10, 14...
4 [32, 20, 14, 20, 14, 20, 14, 14, 41, 33, 20, 2...
...
1340 [28, 43, 12, 24, 3, 31, 4, 31, 31, 24, 43, 32,...
1341 [22, 7, 33, 3, 10, 10, 46, 33, 10, 10, 23, 8, ...
1342 [26, 39, 18, 18, 45, 40, 18, 14, 18, 3, 3, 44,...
1343 [2, 23, 46, 46, 46, 40, 46, 40, 40, 10, 46, 24...
1344 [32, 10, 41, 33, 41, 34, 41, 41, 1, 41, 33, 13...
Length: 1345, dtype: object
#用trainer预测结果并保存 |