基于BERT/ERNIE的中文命名实体识别的Pytorch实现

基于BERT的中文命名实体识别的Pytorch实现

    • ERNIE-NER-pytorch
    • TODO
    • 前言
    • Implementation
    • Experiment
      • Dataset
      • Result
      • Configuration
      • Script


ERNIE-NER-pytorch

  Github:https://github.com/ZacBi/BERT-NER-Pytorch
  出现bug或者对code内容有疑问的可以提交到issue, 最近一段时间会更改一些code中的一些实现, 欢迎讨论和指正.


TODO

  • Add Bi-LSTM-CRF
  • Format code
  • More pretrain models like XLNet and RoBertA

前言

  最近在做和NER略有关系的项目,当前的step还是停留在复现ERNIE在MSRA-NER上的SOTA(不考虑RoBertA)。复现过程中最大的坑是没有考虑到Tokenizer在tokenize后对MSRA的数据集的影响,导致我的F1-score一直在0.92左右徘徊.。这里举个例子。 在MSRA-NER中, 数据集的格式是tsv, tag格式是BIO(当我完成复现后ERNIE已经支持BIOES格式的NER数据集,但只有paddle实现)其中某一行的内容应该如下:

上\x20海\x20市\x20长\x20江\x20大\x20桥\x20于\x20本\x20月\x201\x200\x20号\x20开\x20通\x20。	B-LOC\x20I-LOC\x20I-LOC\x20I-LOC\x20I-LOC\x20I-LOC\x20O\x20O\x20O\x20O\x20O\x20O\x20O\x20O\x20O

  正确分词的格式为:

['上', '海', '市', '长', '江', '大', '桥', '于', '本', '月', '1', '0', '号', '开', '通', '。']

  但是tokenizer分词为:

['上', '海', '市', '长', '江', '大', '桥', '于', '本', '月', '10', '号', '开', '通', '。']

  tokenizer没有将10分开,即没有将数字分开,如果数字后面还有PER,ORG等tag的话,就text和labels就会完全错配,导致在train和eval中出错。pytorch_transformers应该有相应的参数控制实现,但做experiment时时间比较紧张没来得及细看。


Implementation

  待续, 暂时把实验贴出来,大概过程会在之后写出。更详细需要在代码中看comment。


Experiment

Dataset

MSRA-NER(SIGHAN2006)

Result

ERNIE

Stage F1-score Precision Recall
Dev 0.955 0.953 0.957
Test 0.957 0.955 0.959

  I use tensorboard to record important measures during training and evaluation. You can find the event file in runs/ folder and see the trend using the command below:

tensorboard --logdir=runs/

  The graph should be like:
基于BERT/ERNIE的中文命名实体识别的Pytorch实现_第1张图片

Configuration

OS Ubuntu 18.04
CPU Intel® Core™ i7-7800X CPU @ 3.50GHz × 12
GPU GeForce RTX 2080 Ti/PCIe/SSE2
CUDA 10.0
CUDNN 7.6

Script

export TASK_DATA_PATH=your_data_dir/msra_ner
export MODEL_PATH=path_to_your_model
export OUTPUT_DIR=the_dir_you_store_output
export WORKSPACE=your_warkspace

python3 ${WORKSPACE}/ner_train.py \
    --train_file ${TASK_DATA_PATH}/train.tsv \
    --predict_file ${TASK_DATA_PATH}/dev.tsv \
    --model_type bert \
    --model_name_or_path ${MODEL_PATH} \
    --output_dir ${OUTPUT_DIR}/experiments/exp3 \
    --log_dir ${OUTPUT_DIR}/experiments/exp3/runs \
    --task_name msra\
    --num_labels 7 \
    --max_seq_len 256 \
    --do_train \
    --evaluate_during_training \
    --do_lower_case \
    --per_gpu_train_batch_size 16 \
    --learning_rate 5e-5 \
    --layer_norm_eps 1e-5 \
    --weight_decay 0.01 \
    --num_train_epochs 6 \
    --eval_steps 50 \
    --save_steps 1000 \
    --seed 1

你可能感兴趣的:(NLP,NER,AI)