【NLP】BERT中文實戰踩坑

終於用上了bert,踩了一些坑,和你們分享一下。函數


我主要參考了奇點機智的文章,用bert作了兩個中文任務:文本分類和類似度計算。這兩個任務都是直接用封裝好的run_classifer,py,另外兩個沒有仔細看,用到了再補充。lua

1. DataProcessor

Step1:寫好本身的processor,照着例子寫就能夠,必定要shuffle!!!spa

Step2:加到main函數的processors字典裏code

2. Early Stopping

Step1:建一個hookget

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裏input

estimator.train(input_fn=train_input_fn, max_steps=num_train_steps, hooks=[early_stopping_hook])複製代碼

3. Train and Evaluate

須要用tensorboard查看訓練曲線的話比較好it

Step1:建立train和eval的spec,這裏須要把early stopping的hook加到trainSpecclass

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都很小,記得改一下sso

<-未完待續->im

相關文章
相關標籤/搜索