本文介绍了如何在 tf.estimator.Estimator 中使用张量板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑将我的代码库移至 tf.estimator.Estimator,但我找不到有关如何将其与张量板摘要结合使用的示例.

I am considering to move my code base to tf.estimator.Estimator, but I cannot find an example on how to use it in combination with tensorboard summaries.

MWE:

import numpy as np
import tensorflow as tf

tf.logging.set_verbosity(tf.logging.INFO)

# Declare list of features, we only have one real-valued feature
def model(features, labels, mode):
    # Build a linear model and predict values
    W = tf.get_variable("W", [1], dtype=tf.float64)
    b = tf.get_variable("b", [1], dtype=tf.float64)
    y = W*features['x'] + b
    loss = tf.reduce_sum(tf.square(y - labels))

    # Summaries to display for TRAINING and TESTING
    tf.summary.scalar("loss", loss)    
    tf.summary.image("X", tf.reshape(tf.random_normal([10, 10]), [-1, 10, 10, 1])) # dummy, my inputs are images

    # Training sub-graph
    global_step = tf.train.get_global_step()
    optimizer = tf.train.GradientDescentOptimizer(0.01)
    train = tf.group(optimizer.minimize(loss), tf.assign_add(global_step, 1))

    return tf.estimator.EstimatorSpec(mode=mode, predictions=y,loss= loss,train_op=train)

estimator = tf.estimator.Estimator(model_fn=model, model_dir='/tmp/tf')
# define our data set
x=np.array([1., 2., 3., 4.])
y=np.array([0., -1., -2., -3.])
input_fn = tf.contrib.learn.io.numpy_input_fn({"x": x}, y, 4, num_epochs=1000)

for epoch in range(10):
    # train
    estimator.train(input_fn=input_fn, steps=100)
    # evaluate our model
    estimator.evaluate(input_fn=input_fn, steps=10)

如何在张量板中显示我的两个摘要?我是否必须注册一个使用 tf.summary.FileWriter 或其他东西的钩子?

How can I display my two summaries in tensorboard? Do I have to register a hook in which I use a tf.summary.FileWriter or something else?

推荐答案

经过测试(在 v1.1.0 中,也可能在更高版本中),很明显 tf.estimator.Estimator 会自动为您编写摘要.我使用 OP 的代码和张量板确认了这一点.

Upon testing (in v1.1.0, and probably in later versions as well), it is apparent that tf.estimator.Estimator will automatically write summaries for you. I confirmed this using OP's code and tensorboard.

(一些关于 r1.4 的探索让我得出结论,这种自动摘要写作的发生是由于 tf.train.MonitoredTrainingSession.)

(Some poking around r1.4 leads me to conclude that this automatic summary writing occurs due to tf.train.MonitoredTrainingSession.)

最终,自动汇总是通过使用钩子完成的,因此如果您想自定义估算器的默认汇总,您可以使用钩子来实现.以下是原始答案的(已编辑)详细信息.

Ultimately, the automatic summarizing is accomplished with the use of hooks, so if you wanted to customize the Estimator's default summarizing, you could do so using hooks. Below are the (edited) details from the original answer.

您需要使用钩子,以前称为 监视器.(Linked 是一个概念/快速入门指南;简而言之,Estimator API 中内置了挂钩/监控培训的概念.虽然有点令人困惑,但似乎并没有真正弃用挂钩监控器除了在实际源代码中的弃用注释中记录...)

You'll want to use hooks, formerly known as monitors. (Linked is a conceptual/quickstart guide; the short of it is that the notion of hooking into / monitoring training is built into the Estimator API. A bit confusingly, though, it doesn't seem like the deprecation of monitors for hooks is really documented except in a deprecation annotation in the actual source code...)

根据您的使用情况,它看起来像 r1.2 的 SummarySaverHook 符合您的要求.

Based on your usage, it looks like r1.2's SummarySaverHook fits your bill.

summary_hook = tf.train.SummarySaverHook(
    SAVE_EVERY_N_STEPS,
    output_dir='/tmp/tf',
    summary_op=tf.summary.merge_all())

您可能希望自定义钩子的初始化参数,例如通过提供显式的 SummaryWriter 或每 N 秒而不是 N 步编写一次.

You may want to customize the hook's initialization parameters, as by providing an explicity SummaryWriter or writing every N seconds instead of N steps.

如果您将其传递到 EstimatorSpec,您将获得自定义的摘要行为:

If you pass this into the EstimatorSpec, you'll get your customized Summary behavior:

return tf.estimator.EstimatorSpec(mode=mode, predictions=y,loss=loss,
                                  train_op=train,
                                  training_hooks=[summary_hook])

编辑注意:此答案的先前版本建议将 summary_hook 传递到 estimator.train(input_fn=input_fn, steps=5, hooks=[summary_hook]).这不起作用,因为 tf.summary.merge_all() 必须在与模型图相同的上下文中调用.

EDIT NOTE:A previous version of this answer suggested passing the summary_hook into estimator.train(input_fn=input_fn, steps=5, hooks=[summary_hook]). This does not work because tf.summary.merge_all() has to be called in the same context as your model graph.

这篇关于如何在 tf.estimator.Estimator 中使用张量板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 02:36