使用 tf.estimator 提前停止,如何? [英] Early stopping with tf.estimator, how?

查看:32
本文介绍了使用 tf.estimator 提前停止,如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 TensorFlow 1.4 中使用 tf.estimator 并且 tf.estimator.train_and_evaluate 很棒,但我需要提前停止.添加它的首选方式是什么?

I'm using tf.estimator in TensorFlow 1.4 and tf.estimator.train_and_evaluate is great but I need early stopping. What's the prefered way of adding that?

我假设在某处有一些 tf.train.SessionRunHook 用于此目的.我看到有一个带有 ValidationMonitor 的旧 contrib 包,它似乎提前停止,但它似乎在 1.4 中不再存在.或者未来的首选方式是依赖 tf.keras(提前停止真的很容易)而不是 tf.estimator/tf.layers/tf.data,也许?

I assume there is some tf.train.SessionRunHook somewhere for this. I saw that there was an old contrib package with a ValidationMonitor that seemed to have early stopping, but it doesn't seem to be around anymore in 1.4. Or will the preferred way in the future be to rely on tf.keras (with which early stopping is really easy) instead of tf.estimator/tf.layers/tf.data, perhaps?

推荐答案

好消息!tf.estimator 现在对 master 有提前停止支持,看起来它会在 1.10 中.

Good news! tf.estimator now has early stopping support on master and it looks like it will be in 1.10.

estimator = tf.estimator.Estimator(model_fn, model_dir)

os.makedirs(estimator.eval_dir())  # TODO This should not be expected IMO.

early_stopping = tf.contrib.estimator.stop_if_no_decrease_hook(
    estimator,
    metric_name='loss',
    max_steps_without_decrease=1000,
    min_steps=100)

tf.estimator.train_and_evaluate(
    estimator,
    train_spec=tf.estimator.TrainSpec(train_input_fn, hooks=[early_stopping]),
    eval_spec=tf.estimator.EvalSpec(eval_input_fn))

这篇关于使用 tf.estimator 提前停止,如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆