Coder Social home page Coder Social logo

pytorch_bert_bilstm_crf_ner's Introduction

pytorch_bert_bilstm_crf_ner

基于pytorch的bert_bilstm_crf中文命名实体识别
要预先下载好预训练的bert模型,放在和该项目同级下的model_hub文件夹下,即:
model_hub/bert-base-chinese/
相关下载地址:bert-base-chinese
需要的是vocab.txt、config.json、pytorch_model.bin
你也可以使用我已经训练好的模型,将其放在checkpoints下:
链接:https://pan.baidu.com/s/1yIGnQ9I_4HAfQSqHod-hMQ
提取码:4j47
里面有四种模型对应的model.pt

目录结构

--checkpoints:模型保存的位置
--data:数据位置
--|--cner:数据集名称
--|--|--raw_data:原始数据存储位置,里面有个process.py用于转换文本+标签json
--|--|--mid_data:保存处理之后的json文件,标签等;
--|--|--final_data:存储处理好之后可用的pickle文件
--logs:日志存储位置
--utils:辅助函数存储位置,包含了解码、评价指标、设置随机种子、设置日志等
--config.py:配置文件
--dataset.py:数据转换为pytorch的DataSet
--main.py:主运行程序
--main.sh:运行命令
--bert_base_model.py:Bert模型
--bert_ner_modelpy:利用Bert进行Ner的模型
--preprocess.py:预处理,主要是处理数据然后转换成DataSet

依赖

python==3.6
pytorch==1.6.0
pytorch-crf==0.7.2

运行命令

python main.py \
--bert_dir="../model_hub/bert-base-chinese/" \
--data_dir="./data/cner/" \
--log_dir="./logs/" \
--output_dir="./checkpoints/" \
--num_tags=33 \
--seed=123 \
--gpu_ids="0" \
--max_seq_len=150 \
--lr=3e-5 \
--crf_lr=3e-2 \
--other_lr=3e-4 \
--train_batch_size=32 \
--train_epochs=3 \
--eval_batch_size=32 \
--max_grad_norm=1 \
--warmup_proportion=0.1 \
--adam_epsilon=1e-8 \
--weight_decay=0.01 \
--lstm_hidden=128 \
--num_layers=1 \
--use_lstm='True' \
--use_crf='False' \
--dropout_prob=0.3 \
--dropout=0.3 \

我们可以通过控制--use_lstm和--use_crf来切换使用bilstm或crf。

结果

训练、验证、测试和预测

由于忘记保存其它测试和预测了,这里就只展示bert的。

2021-08-05 16:19:12,787 - INFO - main.py - train - 52 -trainepoch:2 359/360 loss:0.0398
2021-08-05 16:19:14,717 - INFO - main.py - train - 56 - [eval] loss:1.8444 precision=0.9484 recall=0.8732 f1_score=0.9093
2021-08-05 16:32:20,751 - INFO - main.py - test - 130 -           
             precision    recall  f1-score   support

     PRO       0.86      0.63      0.73        19
     ORG       0.94      0.91      0.92       543
    CONT       1.00      1.00      1.00        33
    RACE       1.00      0.93      0.97        15
    NAME       0.99      0.93      0.96       110
     EDU       0.98      0.94      0.96       109
     LOC       0.00      0.00      0.00         2
   TITLE       0.95      0.84      0.89       770

micro-f1       0.95      0.88      0.91      1601

2021-08-05 16:32:20,752 - INFO - main.py - <module> - 218 - 虞兔良先生1963年12月出生汉族**国籍无境外永久居留权浙江绍兴人**党员MBA经济师2021-08-05 16:32:22,892 - INFO - trainUtils.py - load_model_and_parallel - 96 - Load ckpt from ./checkpoints/bert/model.pt
2021-08-05 16:32:23,205 - INFO - trainUtils.py - load_model_and_parallel - 106 - Use single gpu in: ['0']
2021-08-05 16:32:23,239 - INFO - main.py - predict - 156 - {'NAME': [('虞兔良', 0)], 'RACE': [('汉族', 17)], 'CONT': [('**国籍', 20)], 'TITLE': [('**党员', 40), ('经济师', 49)], 'EDU': [('MBA', 45)]}

验证集上对比

models loss precision recall f1_score
bert 1.8444 0.9484 0.8732 0.9093
bert_bilstm 2.0856 0.9540 0.8670 0.9084
bert_crf 26.9665 0.9385 0.8957 0.9166
bert_bilstm_crf 30.8463 0.9382 0.8919 0.9145

以上训练的都是3个epoch。

pytorch_bert_bilstm_crf_ner's People

Contributors

taishan1994 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.