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)的步驟
http://www.lxweimin.com/p/3d0bb34c488a
http://www.lxweimin.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)的步驟
http://www.lxweimin.com/p/3d0bb34c488a
http://www.lxweimin.com/p/116bfdb9119a
(1)下載SQuAD和bert-base的模型;
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的轉換過程,比較復雜,但因為時間有限我們不做具體介紹,感興趣的同學可以自己深入閱讀一下。