如何将张量板与 tf.estimator.Estimator 一起使用
How can I use tensorboard with tf.estimator.Estimator
我正在考虑将我的代码库移至 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
或其他东西的挂钩?
编辑:
经过测试(在 v1.1.0 中,可能在以后的版本中也是如此),显然 tf.estimator.Estimator
会自动为您编写摘要。我使用 OP 的代码和张量板确认了这一点。
(一些关于 r1.4 的探索让我得出结论,这种自动摘要写作的发生是由于 tf.train.MonitoredTrainingSession
。)
最终,自动汇总是通过使用 hooks 完成的,因此如果您想自定义 Estimator 的默认汇总,可以使用 hooks 来实现。以下是原始答案的(编辑过的)详细信息。
您需要使用以前称为 monitors 的钩子。 (链接是 conceptual/quickstart 指南;简而言之,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()
。
您可以创建一个 SummarySaverHook
,其中 tf.summary.merger_all()
作为 model_fn 本身 中的 summary_op 。将此挂钩传递给 model_fn 中 EstimatorSpec
构造函数的 training_hooks
参数。
我认为@jagthebeetle 所说的在这里并不完全适用。由于您转移到 estimator.train
方法的挂钩对于您在 model_fn 中定义的摘要不能是 运行,因为它们不会被添加到 merge_all
操作因为它们仍然受 model_fn
范围的限制
对我来说,这 没有 添加任何挂钩或 merge_all
调用。我刚刚在 model_fn
中添加了一些 tf.summary.image(...)
,当我训练模型时,它们神奇地出现在 tensorboard 中。但是,不确定确切的机制是什么。我正在使用 TensorFlow 1.4。
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 ,可以看到图
我正在考虑将我的代码库移至 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
或其他东西的挂钩?
编辑:
经过测试(在 v1.1.0 中,可能在以后的版本中也是如此),显然 tf.estimator.Estimator
会自动为您编写摘要。我使用 OP 的代码和张量板确认了这一点。
(一些关于 r1.4 的探索让我得出结论,这种自动摘要写作的发生是由于 tf.train.MonitoredTrainingSession
。)
最终,自动汇总是通过使用 hooks 完成的,因此如果您想自定义 Estimator 的默认汇总,可以使用 hooks 来实现。以下是原始答案的(编辑过的)详细信息。
您需要使用以前称为 monitors 的钩子。 (链接是 conceptual/quickstart 指南;简而言之,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()
。
您可以创建一个 SummarySaverHook
,其中 tf.summary.merger_all()
作为 model_fn 本身 中的 summary_op 。将此挂钩传递给 model_fn 中 EstimatorSpec
构造函数的 training_hooks
参数。
我认为@jagthebeetle 所说的在这里并不完全适用。由于您转移到 estimator.train
方法的挂钩对于您在 model_fn 中定义的摘要不能是 运行,因为它们不会被添加到 merge_all
操作因为它们仍然受 model_fn
对我来说,这 没有 添加任何挂钩或 merge_all
调用。我刚刚在 model_fn
中添加了一些 tf.summary.image(...)
,当我训练模型时,它们神奇地出现在 tensorboard 中。但是,不确定确切的机制是什么。我正在使用 TensorFlow 1.4。
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 ,可以看到图