Commit a2ce33be authored by hepj987's avatar hepj987
Browse files

调整为model zoo格式

parent 9ec18548
# 基于TF2框架的Bert训练 # BERT-TF2
## 论文
`BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding`
[BERT论文pdf地址](https://arxiv.org/pdf/1810.04805.pdf)
## 模型介绍 ## 模型介绍
![bert_model](bert_model.png)
``` ```
BERT的全称为Bidirectional Encoder Representation from Transformers,是一个预训练的语言表征模型。它强调了不再像以往一样采用传统的单向语言模型或者把两个单向语言模型进行浅层拼接的方法进行预训练,而是采用新的masked language model(MLM),以致能生成深度的双向语言表征。 BERT的全称为Bidirectional Encoder Representation from Transformers,是一个预训练的语言表征模型。它强调了不再像以往一样采用传统的单向语言模型或者把两个单向语言模型进行浅层拼接的方法进行预训练,而是采用新的masked language model(MLM),以致能生成深度的双向语言表征。
``` ```
## 模型结构 ## 算法原理
![bert](bert.png)
``` ```
以往的预训练模型的结构会受到单向语言模型(从左到右或者从右到左)的限制,因而也限制了模型的表征能力,使其只能获取单方向的上下文信息。而BERT利用MLM进行预训练并且采用深层的双向Transformer组件(单向的Transformer一般被称为Transformer decoder,其每一个token(符号)只会attend到目前往左的token。而双向的Transformer则被称为Transformer encoder,其每一个token会attend到所有的token)来构建整个模型,因此最终生成能融合左右上下文信息的深层双向语言表征。 以往的预训练模型的结构会受到单向语言模型(从左到右或者从右到左)的限制,因而也限制了模型的表征能力,使其只能获取单方向的上下文信息。而BERT利用MLM进行预训练并且采用深层的双向Transformer组件(单向的Transformer一般被称为Transformer decoder,其每一个token(符号)只会attend到目前往左的token。而双向的Transformer则被称为Transformer encoder,其每一个token会attend到所有的token)来构建整个模型,因此最终生成能融合左右上下文信息的深层双向语言表征。
``` ```
## 模型下载 ## 预训练模型
[bert-base-uncace(MNLI分类时使用此模型)](https://storage.googleapis.com/bert_models/2018_10_18/uncased_L-12_H-768_A-12.zip) [bert-base-uncace(MNLI分类时使用此模型)](https://storage.googleapis.com/bert_models/2018_10_18/uncased_L-12_H-768_A-12.zip)
[bert-large-uncase(squad问答使用此模型)](https://storage.googleapis.com/bert_models/2018_10_18/uncased_L-24_H-1024_A-16.zip) [bert-large-uncase(squad问答使用此模型)](https://storage.googleapis.com/bert_models/2018_10_18/uncased_L-24_H-1024_A-16.zip)
## 数据集准备 ## 数据集
MNLI分类数据集:[MNLI](https://dl.fbaipublicfiles.com/glue/data/MNLI.zip) MNLI分类数据集:[MNLI](https://dl.fbaipublicfiles.com/glue/data/MNLI.zip)
...@@ -27,6 +37,33 @@ squad问答数据集:[train-v1.1.json](https://rajpurkar.github.io/SQuAD-explo ...@@ -27,6 +37,33 @@ squad问答数据集:[train-v1.1.json](https://rajpurkar.github.io/SQuAD-explo
squad-v1.1 eval脚本:[evaluate-v1.1.py](https://github.com/allenai/bi-att-flow/blob/master/squad/evaluate-v1.1.py) squad-v1.1 eval脚本:[evaluate-v1.1.py](https://github.com/allenai/bi-att-flow/blob/master/squad/evaluate-v1.1.py)
`MNLI数据集`
```
├── original
│ ├── multinli_1.0_dev_matched.jsonl
│ ├── multinli_1.0_dev_matched.txt
│ ├── multinli_1.0_dev_mismatched.jsonl
│ ├── multinli_1.0_dev_mismatched.txt
│ ├── multinli_1.0_train.jsonl
│ └── multinli_1.0_train.txt
├── dev_matched.tsv
├── dev_mismatched.tsv
├── README.txt
├── test_matched.tsv
├── test_mismatched.tsv
└── train.tsv
```
`squadv1.1数据结构`
```
├── dev-v1.1.json
└── train-v1.1.json
```
## 环境配置 ## 环境配置
推荐使用docker方式运行,提供[光源](https://www.sourcefind.cn/#/main-page)镜像,可以dockerpull拉取 推荐使用docker方式运行,提供[光源](https://www.sourcefind.cn/#/main-page)镜像,可以dockerpull拉取
...@@ -35,17 +72,15 @@ squad-v1.1 eval脚本:[evaluate-v1.1.py](https://github.com/allenai/bi-att-flo ...@@ -35,17 +72,15 @@ squad-v1.1 eval脚本:[evaluate-v1.1.py](https://github.com/allenai/bi-att-flo
docker pull image.sourcefind.cn:5000/dcu/admin/base/tensorflow:2.7.0-centos7.6-dtk-22.10.1-py37-latest docker pull image.sourcefind.cn:5000/dcu/admin/base/tensorflow:2.7.0-centos7.6-dtk-22.10.1-py37-latest
``` ```
## 安装依赖
安装过程可能顶掉DCU版本的tensorflow,可以到[开发者社区](https://cancon.hpccube.com:65024/4/main/tensorflow/dtk22.10)下载DCU版本对应包 安装过程可能顶掉DCU版本的tensorflow,可以到[开发者社区](https://cancon.hpccube.com:65024/4/main/tensorflow/dtk22.10)下载DCU版本对应包
``` ```
pip install requirements.txt pip install requirements.txt
``` ```
# MNLI分类测试 ## MNLI分类训练
## 数据转化 ### 数据转化
TF2.0版本读取数据需要转化为tf_record格式 TF2.0版本读取数据需要转化为tf_record格式
...@@ -71,7 +106,7 @@ python create_finetuning_data.py \ ...@@ -71,7 +106,7 @@ python create_finetuning_data.py \
--classification_task_name 分类任务名 --classification_task_name 分类任务名
``` ```
## 模型转化 ### 模型转化
TF2.7.2与TF1.15.0模型存储、读取格式不同,官网给出的Bert一般是基于TF1.0的模型需要进行模型转化 TF2.7.2与TF1.15.0模型存储、读取格式不同,官网给出的Bert一般是基于TF1.0的模型需要进行模型转化
...@@ -90,7 +125,7 @@ python3 tf2_encoder_checkpoint_converter.py \ ...@@ -90,7 +125,7 @@ python3 tf2_encoder_checkpoint_converter.py \
bert_model.ckpt-1.index改为 bert_model.ckpt.index bert_model.ckpt-1.index改为 bert_model.ckpt.index
``` ```
## 单卡运行 ### 单卡运行
``` ```
sh bert_class.sh sh bert_class.sh
...@@ -111,15 +146,15 @@ sh bert_class.sh ...@@ -111,15 +146,15 @@ sh bert_class.sh
--num_gpus 使用gpu数量 --num_gpus 使用gpu数量
``` ```
## 多卡运行 ### 多卡运行
``` ```
sh bert_class_gpus.sh sh bert_class_gpus.sh
``` ```
# SQUAD1.1问答测试 ## SQUAD1.1问答训练
## 数据转化 ### 数据转化
TF2.0版本读取数据需要转化为tf_record格式 TF2.0版本读取数据需要转化为tf_record格式
...@@ -144,7 +179,7 @@ python3 create_finetuning_data.py \ ...@@ -144,7 +179,7 @@ python3 create_finetuning_data.py \
--max_seq_length 最大句子长度 --max_seq_length 最大句子长度
``` ```
## 模型转化 ### 模型转化
``` ```
python3 tf2_encoder_checkpoint_converter.py \ python3 tf2_encoder_checkpoint_converter.py \
...@@ -161,7 +196,7 @@ python3 tf2_encoder_checkpoint_converter.py \ ...@@ -161,7 +196,7 @@ python3 tf2_encoder_checkpoint_converter.py \
bert_model.ckpt-1.index改为 bert_model.ckpt.index bert_model.ckpt-1.index改为 bert_model.ckpt.index
``` ```
## 单卡运行 ### 单卡运行
``` ```
sh bert_squad.sh sh bert_squad.sh
...@@ -184,7 +219,7 @@ sh bert_squad.sh ...@@ -184,7 +219,7 @@ sh bert_squad.sh
--num_gpus 使用gpu数量 --num_gpus 使用gpu数量
``` ```
## 多卡运行 ### 多卡运行
``` ```
sh bert_squad_gpus.sh sh bert_squad_gpus.sh
...@@ -199,12 +234,22 @@ sh bert_squad_gpus.sh ...@@ -199,12 +234,22 @@ sh bert_squad_gpus.sh
| MNLI-class(单卡) | val_accuracy: 0.7387 | | MNLI-class(单卡) | val_accuracy: 0.7387 |
| squad1.1(单卡) | F1-score:0.916378 | | squad1.1(单卡) | F1-score:0.916378 |
## 应用场景
`文本分类、智能问答`
## 算法类别
`自然语言处理`
## 热点行业
`互联网`
# 源码仓库及问题反馈 ## 源码仓库及问题反馈
https://developer.hpccube.com/codes/modelzoo/bert-tf2 https://developer.hpccube.com/codes/modelzoo/bert-tf2
# 参考 ## 参考
https://github.com/tensorflow/models/tree/v2.3.0/official/nlp https://github.com/tensorflow/models/tree/v2.3.0/official/nlp
bert.png

112 KB

Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment