【NLP】BERT中文实战踩坑

终于用上了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都很小,记得改一下

<-未完待续->

猜你喜欢

转载自juejin.im/post/5c093c04f265da611d667584