簡體   English   中英

如何將Tensorboard添加到Tensorflow估算器過程中

[英]How to add Tensorboard to a Tensorflow estimator process

我已經采用了提供的鮑魚示例,並確保我已經理解了......好吧,我想我做到了。 但是作為我正在研究的另一個估算項目是生產垃圾 - 我試圖添加張量板,所以我可以理解發生了什么。

基本代碼是https://www.tensorflow.org/extend/estimators

我添加了一個Session和一個writer

    # Set model params
    model_params = {"learning_rate": 0.01}
    with  tf.Session ()   as  sess: 
        # Instantiate Estimator
        nn = tf.contrib.learn.Estimator(model_fn=model_fn, params=model_params)
        writer  =  tf.summary.FileWriter ( '/tmp/ab_tf' ,  sess.graph)
        nn.fit(x=training_set.data, y=training_set.target, steps=5000)   
        # Score accuracy
        ev = nn.evaluate(x=test_set.data, y=test_set.target, steps=1)


And added 1 line in the model_fn function so it looks like this...


def model_fn(features, targets, mode, params):
  """Model function for Estimator."""

  # Connect the first hidden layer to input layer
  # (features) with relu activation
  first_hidden_layer = tf.contrib.layers.relu(features, 49)

  # Connect the second hidden layer to first hidden layer with relu
  second_hidden_layer = tf.contrib.layers.relu(first_hidden_layer, 49)

  # Connect the output layer to second hidden layer (no activation fn)
  output_layer = tf.contrib.layers.linear(second_hidden_layer, 1)

  # Reshape output layer to 1-dim Tensor to return predictions
  predictions = tf.reshape(output_layer, [-1])
  predictions_dict = {"ages": predictions}

  # Calculate loss using mean squared error
  loss = tf.losses.mean_squared_error(targets, predictions)

  # Calculate root mean squared error as additional eval metric
  eval_metric_ops = {
      "rmse": tf.metrics.root_mean_squared_error(
          tf.cast(targets, tf.float64), predictions)
  }

  train_op = tf.contrib.layers.optimize_loss(
      loss=loss,
      global_step=tf.contrib.framework.get_global_step(),
      learning_rate=params["learning_rate"],
      optimizer="SGD")


  tf.summary.scalar('Loss',loss)

  return model_fn_lib.ModelFnOps(
      mode=mode,
      predictions=predictions_dict,
      loss=loss,
      train_op=train_op,
      eval_metric_ops=eval_metric_ops)

最后加了一個

writer.close()

當我運行代碼時......我在/ tmp / ab_tf中得到一個數據文件,這個文件是非空的。 但它的大小只有139個字節......這意味着什么都沒有被寫入....

當我用張量板打開它時 - 沒有數據。

我究竟做錯了什么 ?

感謝任何輸入......

實際上,您不需要為估算器設置摘要編寫器。 摘要日志將寫入估算器的model_dir。

假設您的model_dir for estimator是'./tmp/model',您可以使用tensorboard查看摘要--logdir =。/ tmp / model

我試圖做與你完全相同的事情。 我終於想通了你需要將model_dir作為參數傳遞給類構造函數,如下所示:

# Instantiate Estimator
nn = tf.contrib.learn.Estimator(model_fn=model_fn,
        params=model_params, 
        model_dir=FLAGS.log_dir)

您可以在此處看到TensorFlow API中記錄的內容: https//www.tensorflow.org/api_docs/python/tf/contrib/learn/Estimator

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM