在Tensorflow的estimator中,多次调用model_fn时是如何工作的?

In the estimator of Tensorflow, how does it work when model_fn is called multiple times?

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 重用训练值的权重?

查看此线程:https://github.com/tensorflow/tensorflow/issues/13895

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