BERT和Transformer理解及测试(三)

FINETUNNNG---分类&SQuAD的问题

Google fine-tuning with bert

Bert-large的结果用12-16GB显存的GPU是无法复现结果的

Bert-base的结果最少12GB的GPU显存可以满足finetunning的需求

Out-of-memory

用缺省的脚本训练时,12GB titanx可以承受的极限batchsize和sequence_length

TensorFlow1.11.0

MORE

Gradient accumulation: Thesamples in a minibatch are typically independent with respect to gradientcomputation (excluding batch normalization, which is not used here). This meansthat the gradients of multiple smaller minibatches can be accumulated beforeperforming the weight update, and this will be exactly equivalent to a singlelarger update.

Gradient checkpointing: The major use ofGPU/TPU memory during DNN training is caching the intermediate activations inthe forward pass that are necessary for efficient computation in the backwardpass. "Gradient checkpointing" trades memory for compute time byre-computing the activations in an intelligent way.

文本分类

Finetuning(classify)的步骤

https://www.jianshu.com/p/3d0bb34c488a

https://www.jianshu.com/p/116bfdb9119a

(1)下载glue data和bert-base的模型;

https://blog.csdn.net/weixin_43948816/article/details/86063235

This example code fine-tunes BERT-Baseon the Microsoft Research Paraphrase Corpus (MRPC)corpus, which only contains 3,600 examples and can fine-tune in a few minuteson most GPUs.

(2)设置环境变量

exportBERT_BASE_DIR=/path/to/bert/uncased_L-12_H-768_A-12

exportGLUE_DIR=/path/to/glue

(3)运行

python run_classifier.py \

  --task_name=MRPC \

  --do_train=true \

  --do_eval=true \

  --data_dir=$GLUE_DIR/MRPC \

 --vocab_file=$BERT_BASE_DIR/vocab.txt \

 --bert_config_file=$BERT_BASE_DIR/bert_config.json \

  --init_checkpoint=$BERT_BASE_DIR/bert_model.ckpt\

  --max_seq_length=128 \

  --train_batch_size=32 \

  --learning_rate=2e-5 \

  --num_train_epochs=3.0 \

  --output_dir=/tmp/mrpc_output/

(4)Prediction from classifier

Once you have trained your classifier you can use it in inferencemode by using the --do_predict=true command. You need to have a file namedtest.tsv in the input folder. Output will be created in file calledtest_results.tsv in the output folder. Each line will contain output for eachsample, columns are the class probabilities.

exportBERT_BASE_DIR=/path/to/bert/uncased_L-12_H-768_A-12

exportGLUE_DIR=/path/to/glue

exportTRAINED_CLASSIFIER=/path/to/fine/tuned/classifier

pythonrun_classifier.py \

  --task_name=MRPC \

  --do_predict=true \

  --data_dir=$GLUE_DIR/MRPC \

  --vocab_file=$BERT_BASE_DIR/vocab.txt \

  --bert_config_file=$BERT_BASE_DIR/bert_config.json \

  --init_checkpoint=$TRAINED_CLASSIFIER\

  --max_seq_length=128 \

  --output_dir=/tmp/mrpc_output/

1)input_ids:: [101, 7277, 2180, 5303, 4806, 1117, 1711, 117, 2292, 1119, 1270,107, 1103, 7737, 107, 117, 1104, 9938, 4267, 12223, 21811, 1117, 2554, 119,102, 11336, 6732, 3384, 1106, 1140, 1112, 1178, 107, 1103, 7737, 107, 117,7277, 2180, 5303, 4806, 1117, 1711, 1104, 9938, 4267, 12223, 21811, 1117, 2554,119, 102, 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, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0]

input_masks: : [1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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, 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,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

segment_ids: : [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, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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, 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, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

label:int 0 or 1

2)给数据打batch,用tf.data,函数是input_fn_builder

3)构建模型,输入input_id input_mask segement_id

4)embedding及embedding的后处理,得到的大小batch*seq_len*emd_size

5)经过transformer_model,得到的sequence_out大小batch*seq_len*hidden_size,如果是句子级别的分类等任务输出可以选择pool_out,大小batch*hidden_size

6)使用pool_out,然后将pool_out去乘以num_hidden*num_labels

7)后面的loss求取就是简单的求取分类的loss和图像那种是一样的猫狗分类

Finetuning(classify)的速度

NF5288M5:python run_classifier.py NF5288M5 :106~108examples/s

Finetuning(classify)的理解

1)与pretraining存在一些不一致的地方,但是总体的结构上是一致的所以训练速度也差不多,操作稍微少了一点点,其实也没算少,有了更多的预处理。

2)参数(task_name/max_seq_length/data_dir)

3)数据预处理(构建MrpcProcessor类,用类中的方法获取在csv文件中的样本数据,返回格式是一个样本类,将样本类转化成样本特征类(三个输入一个输出),并写入tfrecord文件)

4)模型构建,构建的过程和预训练很相似,在构建万BERT主体后,loss求取方式是存在差别的。

阅读理解

Finetuning(squad)的步骤

https://www.jianshu.com/p/3d0bb34c488a

https://www.jianshu.com/p/116bfdb9119a

(1)下载SQuAD和bert-base的模型;

https://links.jianshu.com/go?to=https%3A%2F%2Frajpurkar.github.io%2FSQuAD-explorer%2Fdataset%2Ftrain-v1.1.json

https://links.jianshu.com/go?to=https%3A%2F%2Frajpurkar.github.io%2FSQuAD-explorer%2Fdataset%2Fdev-v1.1.json

https://links.jianshu.com/go?to=https%3A%2F%2Fgithub.com%2Fallenai%2Fbi-att-flow%2Fblob%2Fmaster%2Fsquad%2Fevaluate-v1.1.py

(2)设置环境变量

exportBERT_BASE_DIR=/path/to/bert/uncased_L-12_H-768_A-12

exportSQUAD_DIR=/path/to/squad

(3)运行

python run_squad.py \

 --vocab_file=$BERT_BASE_DIR/vocab.txt \

 --bert_config_file=$BERT_BASE_DIR/bert_config.json \

 --init_checkpoint=$BERT_BASE_DIR/bert_model.ckpt \

  --do_train=True \

 --train_file=$SQUAD_DIR/train-v1.1.json \

  --do_predict=True \

 --predict_file=$SQUAD_DIR/dev-v1.1.json \

  --train_batch_size=12\

  --learning_rate=3e-5\

 --num_train_epochs=2.0 \

  --max_seq_length=384\

  --doc_stride=128 \

 --output_dir=/tmp/squad_base/

斯坦福的问答数据集,阅读理解QA类的任务。

Example:


qas_id 样本ID,question_text问题文本,doc_tokens阅读材料[word0,word1, ...]的形式,orig_answer_text 原始答案的文本,start_position答案在文本中开始的位置,end_position答案在文本中结束的位置,is_impossible在SQuAD2里才会用到的字段这里可以不用关心。

将example转化为feature并将feature存入tfrecord。


构建输入的pipline函数。

Model build:

模型返回的是start logits和 end logits,中间利用的是最后一层sequence_output

Finetuning(squad)的速度

NF5288M5:python run_squad.py NF5288M5 :31~33examples/s

Finetuning(squad)的理解

1)与classify存在一些不一致的地方,但是总体的结构上是一致的,BERT模型架构是一致的,使用的是sequence——out。

2)Loss是答案的开始点与结束点的两个loss之和。

3)数据预处理(构建MrpcProcessor类,用类中的方法获取在json文件中的样本数据,返回格式是一个样本类,将样本类转化成样本特征类(输入输出很多),并写入tfrecord文件)

4)模型构建,构建的过程和预训练很相似,在构建万BERT主体后,loss求取方式是存在差别的。

***squad里有很多细节特别是sample到feature的转换过程,比较复杂,但因为时间有限我们不做具体介绍,感兴趣的同学可以自己深入阅读一下。

你可能感兴趣的:(BERT和Transformer理解及测试(三))