如何在张量流估计过程中加入张量板

2024-04-16 13:59:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经以提供的鲍鱼为例,并确保我已经理解了。。。。我想是的。但作为我正在进行的另一个估算项目是产生总垃圾-我已经尝试添加张量板,所以我可以理解发生了什么。

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

我增加了一个会话和一个作家

    # 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字节。。。这意味着什么都没写。。。。

当我用张量板打开时-没有数据。

我做错什么了?

感谢任何意见。。。


Tags: thetolayermodellayerstfparamscontrib
2条回答

实际上,您不需要为估计器设置摘要编写器。 摘要日志将写入估计器的model_dir。

假设估计器的model_dir为'./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

相关问题 更多 >