提前停止使用tf.estimator,怎么办?

发布于 2021-01-29 17:04:27

tf.estimator在TensorFlow
1.4中使用过,tf.estimator.train_and_evaluate虽然很好,但我需要尽早停止。首选的添加方式是什么?

我认为这有些tf.train.SessionRunHook地方。我看到有一个旧的contrib程序包,ValidationMonitor它似乎已经提前停止,但在1.4中似乎不再存在。还是将来首选的方法是依靠tf.keras(真的很容易尽早停止)而不是依靠tf.estimator/tf.layers/tf.data

关注者
0
被浏览
189
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    好消息!tf.estimator现在已经在master上提供了早期停止支持,并且看起来将在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))
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看