我正在考慮將我的代碼庫移動到tf.estimator.Estimator,但我找不到如何將它與tensorboard摘要結合使用的示例。如何使用tensorboard與tf.estimator.Estimator
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
或其他東西?
我嘗試了上面的代碼,但它崩潰的錯誤信息:「必須提供腳手架或summary_op的一個。」 我認爲tf.summary.merge_all()返回無。我必須將摘要添加到某個集合嗎? – monchi
'tf.summary.merge_all()'超出'input_fn'內創建的度量範圍,所以這不起作用。 –
好的。回答編輯。具體地說,'Estimator.train'在它自己的默認圖中調用'model_fn',所以'model_fn'之外調用的'merge_all'無法找到彙總節點,並且按照其規範返回'None'。 – jagthebeetle