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)的步驟

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%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的轉換過程,比較復雜,但因為時間有限我們不做具體介紹,感興趣的同學可以自己深入閱讀一下。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,030評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,310評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,951評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,796評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,566評論 6 407
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,055評論 1 322
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,142評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,303評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,799評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,683評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,899評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,409評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,135評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,520評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,757評論 1 282
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,528評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,844評論 2 372