【发布时间】:2019-05-31 08:28:59
【问题描述】:
当我在EvalSpec 中使用tf.estimator.train_and_evaluate 和BestExporter 时,最后的返回值可能不包括export_result,因为最终的评估调用不一定会导致导出。例如,如果您的最后一个检查点不会导致评估集的损失较低,就会发生这种情况。
如何访问导致从BestExporter 导出的最后一个export_result?理想情况下,我希望在 train_and_evaluate 的末尾有每个 (metrics, export_results) 的 list,而不仅仅是最后一个。
对于任何急需解决方法的人,您可以使用这样的 python 内置函数访问该目录。
estimator = tf.estimator.Estimator(...)
best_exporter = tf.estimator.BestExporter(...)
# Add best_exporter to your eval_spec
# Make train_spec
metrics, export_results = tf.estimator.train_and_evaluate(...)
best_export_dir = os.path.join(estimator.model_dir, 'export', best_exporter.name)
savedmodels = os.listdir(best_export_dir)
best_model = savedmodels[-1]
显然,最好有更好的方法。我在此处描述的特定问题是 export_results 可能只是 [None],因为即使有较早的导出,上一个检查点也不会导致导出。
对于任何关心这些的人来说,这些是来自 tensorflow r1.13 的相关代码位,跟踪 export_results 从调用到值的生命,
tf.estimator.train_and_evaluate471
_TrainingExecutor.run_local703
_NewCheckpointListenerForEvaluate.after_save517
_NewCheckpointListenerForEvaluate._evaluate536
【问题讨论】:
-
好问题,我刚刚发现 BestExporter 可以解决我的一个问题,但我根本无法获得一些类似 dict 的结构,从而为每个导出结果提供评估指标。如果您不受 Estimator 的约束,请考虑查看 TF2,他们使用 Keras API 以更好的方式处理此问题。
标签: python tensorflow tensorflow-estimator