【问题标题】:In the estimator of Tensorflow, how does it work when model_fn is called multiple times?在Tensorflow的estimator中,model_fn被多次调用时是如何工作的?
【发布时间】:2017-10-22 03:23:28
【问题描述】:
def model_fn(features, labels, mode, params):
  """Model function for Estimator."""

  # Connect the first hidden layer to input layer
  # (features["x"]) with relu activation
  first_hidden_layer = tf.layers.dense(features["x"], 10, activation=tf.nn.relu)

  # Connect the second hidden layer to first hidden layer with relu
  second_hidden_layer = tf.layers.dense(
      first_hidden_layer, 10, activation=tf.nn.relu)

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

  # Reshape output layer to 1-dim Tensor to return predictions
  predictions = tf.reshape(output_layer, [-1])

  # Provide an estimator spec for `ModeKeys.PREDICT`.
  if mode == tf.estimator.ModeKeys.PREDICT:
    return tf.estimator.EstimatorSpec(
        mode=mode,
        predictions={"ages": predictions})

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

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

  optimizer = tf.train.GradientDescentOptimizer(
      learning_rate=params["learning_rate"])
  train_op = optimizer.minimize(
      loss=loss, global_step=tf.train.get_global_step())

  # Provide an estimator spec for `ModeKeys.EVAL` and `ModeKeys.TRAIN` modes.
  return tf.estimator.EstimatorSpec(
      mode=mode,
      loss=loss,
      train_op=train_op,
      eval_metric_ops=eval_metric_ops)

以上是Tensorflow的Estimator使用的model_fn示例。

正如教程中提到的,这个 model_fn 可以在不同的上下文中调用(训练、预测、评估)。不过,我有点困惑,因为每次调用model_fn时,并没有重用现有的图,而是创建了一个新的图(或在图中创建了新的节点)

例如,我首先在 TRAIN 模式下调用 model_fn,然后在 PREDICT 模式下调用 model_fn。如何确保 PREDICT 重用训练值的权重?

【问题讨论】:

    标签: tensorflow tensorflow-datasets


    【解决方案1】:

    看到这个帖子:https://github.com/tensorflow/tensorflow/issues/13895

    每次都会重新构建图表,并从检查点加载数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多