【问题标题】:How can I use tensorboard with tf.estimator.Estimator如何将张量板与 tf.estimator.Estimator 一起使用
【发布时间】:2017-10-02 15:02:00
【问题描述】:

我正在考虑将我的代码库移动到 tf.estimator.Estimator,但我找不到如何将它与 tensorboard 摘要结合使用的示例。

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)

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

【问题讨论】:

    标签: python-3.x tensorflow tensorboard


    【解决方案1】:

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

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

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


    您需要使用挂钩,以前称为 monitors。 (Linked 是一个概念/快速入门指南;它的简短之处在于 Estimator API 中内置了挂钩/监控训练的概念。虽然有点令人困惑,但似乎并不推荐使用挂钩监控器是真的记录在实际源代码中的弃用注释中...)

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

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

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

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

    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()

    【讨论】:

    • 我尝试了上面的代码,但它崩溃并显示错误消息:“必须提供脚手架或 summary_op 中的一个。”我认为 tf.summary.merge_all() 返回无。我必须将摘要添加到某个集合吗?
    • tf.summary.merge_all() 超出了在 input_fn 中创建的指标范围,因此这不起作用。
    • 好收获。答案已编辑。具体来说,Estimator.train 在其自己的默认图中调用model_fn,因此在model_fn 之外调用的merge_all 无法找到汇总节点,并根据其规范返回None
    • 我的问题是,我们能不能有两个钩子,一个你说的作为summary hook,另一个打印loss as loss hook
    【解决方案2】:

    对我来说,这没有添加任何挂钩或merge_all 调用。我刚刚在我的model_fn 中添加了一些tf.summary.image(...),当我训练模型时,它们神奇地出现在张量板上。但是,不确定确切的机制是什么。我正在使用 TensorFlow 1.4。

    【讨论】:

      【解决方案3】:

      estimator = tf.estimator.Estimator(model_fn=model, model_dir='/tmp/tf')

      代码model_dir='/tmp/tf'表示估算器将所有日志写入/tmp/tf,然后运行tensorboard --log.dir=/tmp/tf,打开浏览器url为:http://localhost"6006,可以看到图形

      【讨论】:

        【解决方案4】:

        您可以使用tf.summary.merger_all() 创建一个SummarySaverHook 作为summary_op 在model_fn 本身。将此钩子传递给 model_fn 中 EstimatorSpec 构造函数的 training_hooks 参数。

        我认为@jagthebeetle 所说的在这里并不完全适用。由于您转移到estimator.train 方法的钩子无法针对您在model_fn 中定义的摘要运行,因为它们不会被添加到merge_all 操作中,因为它们仍然受model_fn

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-10-29
          • 2021-04-18
          • 2018-06-25
          • 1970-01-01
          • 2019-07-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多