基于google提出的bert模型和tensorflow实现,bilm+crf部分参考了Guillaume Genthial的代码,详细理论讲解可见 右左瓜子的知乎专栏
- google的bert实现google research bert
- google提供的汉语预训练数据 chinese-pretrain
- 1.下载以上require中的代码和文件,将
bilm_crf.py和bert_bilm_crf.py放到和google的bert中和modeling.py平级的文件夹下 - 2.运行脚本
python .\bert_bilm_crf.py --task_name=ner --do_train=true --do_eval=false --do_predict=false --data_dir=path\to\yourdata \
--vocab_file=path\to\chinese_L-12_H-768_A-12\vocab.txt --bert_config_file=path\to\chinese_L-12_H-768_A-12\bert_config.json \
--init_checkpoint=path\to\chinese_L-12_H-768_A-12\bert_model.ckpt --max_seq_length=50 --train_batch_size=32 \
--learning_rate=5e-5 --num_train_epochs=2.0 --output_dir=/tmp/ner_output/- 3.导出训练结果
运行
export.py - 4.部署在服务端
运行
export.sh - 5调用例子
clien.py
在给出的example.tsv中有两行示例数据,把格式整理成类似的即可
this is a solution to NER task base on BERT and bilm+crf, the BERT model comes from google's github, the bilm+crf part inspired from Guillaume Genthial's code, visit this page for more details
- google's
BERTmodel google research bert - a Chinese pre-trained model from googlechinese-pretrain
- 1.download codes and files mentioned above, put the
bilm_crf.pyandbert_bilm_crf.pyinto the google'sBERTdirectory, the to python file is at the same level withmodeling.py - 2.run script
python .\bert_bilm_crf.py --task_name=ner --do_train=true --do_eval=false --do_predict=false --data_dir=path\to\yourdata \
--vocab_file=path\to\chinese_L-12_H-768_A-12\vocab.txt --bert_config_file=path\to\chinese_L-12_H-768_A-12\bert_config.json \
--init_checkpoint=path\to\chinese_L-12_H-768_A-12\bert_model.ckpt --max_seq_length=50 --train_batch_size=32 \
--learning_rate=5e-5 --num_train_epochs=2.0 --output_dir=/tmp/ner_output/- 3.export model
run
export.py - 4.deploy
run
export.sh - 5.example
clien.py
there is an example data in example.tsv to show the formate, you are surpoed to transform your data into this formate, or you can modify the input_fn in bert_bilm_crf.py
更新情感分析方法:bert_senta.py,以及预测方法senta_pred.py,senta_pred.py中读取数据的方法都注释掉了使用时添加上自己的数据读取方式
预测方法使用了dataset数据流的形式,单个预测耗时10ms