【NLP】BERT中文实战踩坑

栏目: 编程工具 · 发布时间: 6年前

内容简介:终于用上了bert,踩了一些坑,和大家分享一下。我主要参考了奇点机智的文章,用bert做了两个中文任务:文本分类和相似度计算。这两个任务都是直接用封装好的run_classifer,py,另外两个没有仔细看,用到了再补充。Step1:写好自己的processor,照着例子写就可以,一定要

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

<-未完待续->


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Math Adventures with Python

Math Adventures with Python

Peter Farrell / No Starch Press / 2018-11-13 / GBP 24.99

Learn math by getting creative with code! Use the Python programming language to transform learning high school-level math topics like algebra, geometry, trigonometry, and calculus! In Math Adventu......一起来看看 《Math Adventures with Python》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

MD5 加密
MD5 加密

MD5 加密工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具