在 GCMLE 上从 learn_runner.run 更新到 tf.estimator.train_and_evaluate

Update from learn_runner.run to tf.estimator.train_and_evaluate on GCMLE

我试图确保在更新到 tf.estimator.train_and_evaluate() 而不是 learn_runner.run() 时涵盖所有内容。

我正在寻找这个 GCMLE 自定义估算器样本的基础,它曾经是:

learn_runner.run(
      generate_experiment_fn(
          min_eval_frequency=args.min_eval_frequency,
          eval_delay_secs=args.eval_delay_secs,
          train_steps=args.train_steps,
          eval_steps=args.eval_steps,
          export_strategies=[saved_model_export_utils.make_export_strategy(
              model.SERVING_FUNCTIONS[args.export_format],
              exports_to_keep=1
          )]
      ),
      run_config=tf.contrib.learn.RunConfig(model_dir=args.job_dir),
      hparams=hparam.HParams(**args.__dict__)
  )

export_strategies:

以前,export_strategies 会将最终的模型二进制文件放在 $job_dir/export/Servo/$timestamp 中。但是,当尝试转换为使用 tf.estimator.train_and_evaluate 时,我看不到如何复制此行为。

按照这个较新的自定义估算器示例,我已经通过了

exporter = tf.estimator.FinalExporter('saved-model', SERVING_FUNCTIONS[hparams.export_format])

进入 EvalSpec exporters = [exporter],但它不像以前那样作为最终导出策略工作。

run_config

以前 run_config 作为附加命令与 learn_runner.run() 一起传递。现在我在 run_experiment() 函数中的方法是将 run_config() 直接传递给 tf.estimator.Estimatorconfig 参数。有什么我缺少的功能吗?

示例:

run_config = tf.estimator.RunConfig(model_dir=hparams.job_dir,
    save_checkpoints_steps=hparams.save_checkpoint_steps,
    save_summary_steps = hparams.save_summary_steps)

estimator = tf.estimator.Estimator(model_fn=model_fn,
                                   model_dir=hparams.job_dir,
                                   config = run_config,
                                   params = hparams)

新的 run_config 实施是否有我从旧实施中遗漏的内容?

问题来自 num_epochsnum_steps 的停止条件——看来 tf.estimator.Estimator 不能很好地与 num_epochs 配合使用,所以您应该停止如果您想要导出文件夹,条件为 num_steps

另外,值得注意的是,如果您在 tf.estimator.Estimator() 中直接指定 model_dir 并在 run_config = tf.estimator.RunConfig() 中指定 model_dir,则这些名称必须匹配。 TF 文档建议,如果 "model_dir: Directory to save model parameters, graph and etc. This can also be used to load checkpoints from the directory into a estimator to continue training a previously saved model. If None, the model_dir in config will be used if set. If both are set, they must be same. If both are None, a temporary directory will be used."

相等,则可以在两个地方指定 model_dir