提前停止使用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))