終於用上了bert,踩了一些坑,和大家分享一下。
我主要參考了奇點機智的文章,用bert做了兩個中文任務:文字分類和相似度計算。這兩個任務都是直接用封裝好的run_classifer,py,另外兩個沒有仔細看,用到了再補充。
1. DataProcessor
Step1:寫好自己的processor,照著例子寫就可以,一定要shuffle!!!
Step2:加到main函式的processors字典裡
2. Early Stopping
Step1:建一個hook
early_stopping_hook = tf.contrib.estimator.stop_if_no_decrease_hook(
estimator=estimator,
metric_name='eval_loss',
max_steps_without_decrease=FLAGS.max_steps_without_decrease,
eval_dir=None,
min_steps=0,
run_every_secs=None,
run_every_steps=FLAGS.save_checkpoints_steps)複製程式碼
Step2:加到estimator.train裡
estimator.train(input_fn=train_input_fn, max_steps=num_train_steps, hooks=[early_stopping_hook])複製程式碼
3. Train and Evaluate
需要用tensorboard檢視訓練曲線的話比較好
Step1:建立train和eval的spec,這裡需要把early stopping的hook加到trainSpec
train_spec = tf.estimator.TrainSpec(input_fn=train_input_fn, max_steps=num_train_steps,
hooks=[early_stopping_hook])
eval_spec = tf.estimator.EvalSpec(input_fn=eval_input_fn, throttle_secs=0)
tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)複製程式碼
4. Batch size
預設Eval和Predict的batch size都很小,記得改一下
<-未完待續->