【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

相关文章
相关标签/搜索