使用估算器时用 tf.train.SessionRunHook 替换验证监视器 [英] Replace Validation Monitors with tf.train.SessionRunHook when using Estimators
问题描述
我正在运行 DNNClassifier,我在训练时对其准确性进行监控.contrib/learn 中的 monitor.ValidationMonitor 运行良好,在我的实现中我定义了它:
I am running a DNNClassifier, for which I am monitoring accuracy while training. monitors.ValidationMonitor from contrib/learn has been working great, in my implementation I define it:
validation_monitor = skflow.monitors.ValidationMonitor(input_fn=lambda: input_fn(A_test, Cl2_test), eval_steps=1, every_n_steps=50)
然后使用调用来自:
clf.fit(input_fn=lambda: input_fn(A, Cl2),
steps=1000, monitors=[validation_monitor])
哪里:
clf = tensorflow.contrib.learn.DNNClassifier(...
这很好用.也就是说,验证监视器似乎已被弃用,类似的功能将被替换为 tf.train.SessionRunHook
.
This works fine. That said, validation monitors appear to be deprecated and a similar functionality to be replaced with tf.train.SessionRunHook
.
我是 TensorFlow 的新手,对我来说,这种替换实现的样子似乎并不简单.任何建议都受到高度赞赏.同样,我需要在特定数量的步骤后验证训练.非常感谢.
I am a newbie in TensorFlow, and it does not seem trivial to me how such a replacing implementation would look like. Any suggestion are highly appreciated. Again, I need to validate the training after a specific number of steps. Thanks very much in advance.
推荐答案
有一个未公开的实用工具 monitors.replace_monitors_with_hooks()
将监视器转换为钩子.该方法接受 (i) 一个可能包含监视器和钩子的列表,以及 (ii) 将使用钩子的 Estimator,然后通过在每个监视器周围包装一个 SessionRunHook 来返回一个钩子列表.
There's an undocumented utility called monitors.replace_monitors_with_hooks()
which converts monitors to hooks. The method accepts (i) a list which may contain both monitors and hooks and (ii) the Estimator for which the hooks will be used, and then returns a list of hooks by wrapping a SessionRunHook around each Monitor.
from tensorflow.contrib.learn.python.learn import monitors as monitor_lib
clf = tf.estimator.Estimator(...)
list_of_monitors_and_hooks = [tf.contrib.learn.monitors.ValidationMonitor(...)]
hooks = monitor_lib.replace_monitors_with_hooks(list_of_monitors_and_hooks, clf)
对于完全替换 ValidationMonitor 的问题,这并不是真正的真正解决方案——我们只是用一个未弃用的函数将其包装起来.但是,我可以说到目前为止这对我有用,因为它保留了我从 ValidationMonitor 中需要的所有功能(即评估每个 n 步骤,使用指标提前停止等)
This isn't really a true solution to the problem of fully replacing the ValidationMonitor—we're just wrapping it up with a non-deprecated function instead. However, I can say this has worked for me so far in that it maintained all the functionality I need from the ValidationMonitor (i.e. evaluating every n steps, early stopping using a metric, etc.)
还有一件事——要使用这个钩子,你需要从 tf.contrib.learn.Estimator
(只接受监视器)更新到更成熟和官方的 tf.estimator.Estimator
(只接受钩子).因此,您应该将分类器实例化为 tf.estimator.DNNClassifier
,并使用其方法 train()
进行训练(这只是 的重命名)fit()
):
One more thing—to use this hook you'll need to update from a tf.contrib.learn.Estimator
(which only accepts monitors) to the more full-fledged and official tf.estimator.Estimator
(which only accepts hooks). So, you should instantiate your classifier as a tf.estimator.DNNClassifier
, and train using its method train()
instead (which is just a re-naming of fit()
):
clf = tf.estimator.Estimator(...)
...
clf.train(
input_fn=...
...
hooks=hooks)
这篇关于使用估算器时用 tf.train.SessionRunHook 替换验证监视器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!