第二次调用 tf.estimator.train_and_evaluate 在 1 步训练后完成

问题描述 投票:0回答:1

我正在使用 tf.estimator.train_and_evaluate 函数一个接一个地运行两个张量流模型。

# The first
train_spec = tf.estimator.TrainSpec(input_fn=my_input_fn("train"), max_steps=max_steps)
eval_spec = tf.estimator.EvalSpec(input_fn=my_input_fn("valid"), steps=None)
tf.estimator.train_and_evaluate(model1, train_spec, eval_spec)

# the second
hook = [tf.estimator.StopAtStepHook(num_steps=max_steps)]
train_spec = tf.estimator.TrainSpec(input_fn=my_input_fn("train"), max_steps=None, hooks=hook)
eval_spec = tf.estimator.EvalSpec(input_fn=my_input_fn("valid"), steps=None)
tf.estimator.train_and_evaluate(model2, train_spec, eval_spec)

第一个训练可以,但第二个训练只需要一步:

...

INFO:tensorflow:保存全局步骤1的字典:LogLoss = 0.06514542,PR_AUC = 0.012231247,ROC_AUC = 0.52047175,global_step = 1,标签/平均值= 0.011529858,损失= 0.06514542,预测/平均值= 0.016 156415

...

信息:tensorflow:最后一步的损失:0.32117385。

我尝试使用 tf.estimator.train_and_evaluate 在同一数据集上顺序运行两个模型。 我希望这两次培训的进行方式相似。然而,第二次训练仅运行 1 步就结束了。

解决方案: 我在第二个 train_spec input_fn 中使用 tf.estimator.inputs.numpy_input_fn (https://docs.w3cub.com/tensorflow~1.15/estimator/inputs/numpy_input_fn),当 num_epochs=1 或不用作参数时,它提前终止。我改成num_epochs=None,提前终止问题就解决了。对于此解决方案,应在第二次训练中设置 max_steps=None 并应使用停止钩子。此外,在我的例子中,第二次训练中不应该使用早期停止钩子。然而,这会迫使第二次训练继续指定的时期数。

tensorflow2.0 tensorflow-estimator
1个回答
0
投票

解决方案:我在第二个 train_spec input_fn 中使用 tf.estimator.inputs.numpy_input_fn (https://docs.w3cub.com/tensorflow~1.15/estimator/inputs/numpy_input_fn),并且当 num_epochs=1 或不用作一场争论,它提前结束。我改成num_epochs=None,提前终止问题就解决了。对于此解决方案,应在第二次训练中设置 max_steps=None 并应使用停止钩子。此外,在我的例子中,第二次训练中不应该使用早期停止钩子。然而,这会迫使第二次训练继续指定的时期数。

© www.soinside.com 2019 - 2024. All rights reserved.