2017-05-04 1070 views
9

我正在考慮將我的代碼庫移動到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或其他東西?

回答

6

編輯: 經過測試(在v1.1.0中,也可能在以後的版本中),顯然tf.estimator.Estimator會自動爲您寫摘要。我使用OP的代碼和張量板來證實這一點。

(約R1.4一些戳使我得出結論,這個自動摘要的寫作,由於發生tf.train.MonitoredTrainingSession。)

最終,自動摘要與使用鉤子來實現,因此,如果你想定製估算器的默認彙總,你可以使用鉤子來完成。下面是原始答案的(編輯)細節。


你會想要使用掛鉤,以前稱爲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_hookestimator.train(input_fn=input_fn, steps=5, hooks=[summary_hook])。這不起作用,因爲tf.summary.merge_all()必須在與模型圖相同的上下文中調用。

+1

我嘗試了上面的代碼,但它崩潰的錯誤信息:「必須提供腳手架或summary_op的一個。」 我認爲tf.summary.merge_all()返回無。我必須將摘要添加到某個集合嗎? – monchi

+1

'tf.summary.merge_all()'超出'input_fn'內創建的度量範圍,所以這不起作用。 –

+0

好的。回答編輯。具體地說,'Estimator.train'在它自己的默認圖中調用'model_fn',所以'model_fn'之外調用的'merge_all'無法找到彙總節點,並且按照其規範返回'None'。 – jagthebeetle

1

您可以使用tf.summary.merger_all()作爲summary_op 在model_fn本身中創建SummarySaverHook。將此鉤子傳遞給model_fn中構造函數的參數EstimatorSpec

我不認爲@ jagthebeetle所說的完全適用於此。正如你轉移到estimator.train方法鉤子不能爲您在model_fn定義,因爲他們不會被添加到merge_all運算,因爲他們仍然通過model_fn

2

範圍對於我這個界限的摘要運行加入而不加添加任何掛鉤或merge_all調用。我在model_fn中添加了一些tf.summary.image(...),當我訓練模型時,它們神奇地出現在張量板上。然而,不知道確切的機制是什麼。我使用的是TensorFlow 1.4。