python - 如何将 Tensorboard 添加到 Tensorflow 估算器进程

标签 python tensorflow tensorboard

我已经采用了所提供的鲍鱼示例,并确保我已经理解了它……好吧,我想我理解了。但作为我正在从事的另一个估算器项目正在产生总垃圾 - 我试图添加张量板,所以我可以理解发生了什么。

基本代码是 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 是“./tmp/model”, 您可以使用 tensorboard --logdir=./tmp/model 查看摘要

关于python - 如何将 Tensorboard 添加到 Tensorflow 估算器进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43908432/

相关文章:

tensorflow - 保存Tensorflow图以在Tensorboard中查看而无需摘要操作

python3 glob.glob 正则表达式只得到第一个匹配项

Python:如何将 DictReader 行添加为字典以通过更新列出

performance - Tensorflow GPU 利用率仅为 60% (GTX 1070)

python - LSTM - 一段时间后预测相同的常数值

tensorflow - 如何从源代码更新 Tensorflow

python - 如何使用 Tensorflow2 (jupyter, Win) 杀死 tensorboard

python - 在 Tensorboard 中显示更多图像 - Tensorflow 对象检测

python - appengine urllib 是否使用 SSLv3?传输安全协议(protocol)?

python - 如何矢量化(利用 pandas/numpy)而不是使用嵌套的 for 循环