【问题标题】:tensorflow add summary during graph executiontensorflow 在图形执行期间添加摘要
【发布时间】:2017-04-28 03:22:05
【问题描述】:

我想在 tensorflow 教程给出的 cifar10 示例中每 固定数量的步骤 输出精度,我尝试在产生错误的钩子中使用 tf.summary.scalar(..):Graph is finalized。但是,我认为我只能访问钩子中的步骤数(我正在使用 cifar10_eval.py 评估准确性,也是 tensorflow 教程给出的示例代码)。我还尝试将global_step 写入检查点,但不幸的是MointeredTrainingSession 仅支持时间间隔(save_checkpoint_secs)而不是步长间隔。有什么建议吗?

cifar10_train.py

def train():
  """Train CIFAR-10 for a number of steps."""
  with tf.Graph().as_default():
    global_step = tf.contrib.framework.get_or_create_global_step()

    # Build a Graph that trains the model with one batch of examples and
    # updates the model parameters.
    train_op = cifar10.train(loss, global_step)

    class _LoggerHook(tf.train.SessionRunHook):
      """Logs loss and runtime."""

      def begin(self):
        self._step = -1
        self._start_time = time.time()

      def before_run(self, run_context):
        self._step += 1
        return tf.train.SessionRunArgs(loss)  # Asks for loss value.

      def after_run(self, run_context, run_values):
        <output some information>

    with tf.train.MonitoredTrainingSession(
        checkpoint_dir=FLAGS.train_dir,
        hooks=[tf.train.StopAtStepHook(last_step=FLAGS.max_steps),
               tf.train.NanTensorHook(loss),
               _LoggerHook()],
        config=tf.ConfigProto(
            log_device_placement=FLAGS.log_device_placement)) as mon_sess:
      while not mon_sess.should_stop():
        mon_sess.run(train_op)

【问题讨论】:

  • @SalvadorDali,这是 tensorflow 的 github 中的示例代码。我在我的工作副本中添加了tf.summary(在print 语句的正下方),但它不起作用。
  • @SalvadorDali 是的,我理解你的观点,但我认为这是最相关的部分,因为我的问题主要是关于如何在每个固定数量的步骤中评估模型的准确性,而不是为什么会出现错误当我在钩子中添加摘要时。
  • @SalvadorDali 正如你所建议的,我已经移出了一些不相关的代码

标签: python machine-learning tensorflow conv-neural-network


【解决方案1】:

首先,需要注意的是,Tensorflow 提供的 cifar10 教程在两个单独的会话中运行训练和评估。当训练会话保存检查点时,评估会话将检索此检查点。然后加载参数并执行评估。您在此处粘贴的代码仅用于培训课程。

我的建议是,你应该明确你要写哪个摘要。因为培训和评估是两个不同的阶段。有两个摘要作者。通常,它们会为不同的摘要编写者提供不同的路径。

根据您的需要,这里有一些针对您项目的提示。

  • 您不应该向检查点写入任何内容,因为其中包含大量模型参数。
  • 请使用摘要编写器或标准 i/o 以确保记录准确性。
  • 您在尝试使用摘要编写器时遇到错误,因为应在启动会话之前添加包括标量在内的摘要的所有元素。

我猜 Tensorflow 将摘要视为默认图表的一部分。因此,您可能需要在运行会话之前配置摘要编写器。

【讨论】:

    【解决方案2】:

    我之前在使用 CycleGAN 时遇到了这个错误。 我用这两行解决了这个问题,请在初始化 tf 之前添加。

    import tensorflow as tf
    
    tf.reset_default_graph()
    tf.Graph().as_default()
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2017-06-03
      • 1970-01-01
      • 2016-09-28
      • 1970-01-01
      • 2013-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多