【问题标题】:Tensorflow complains about missing feed_dict during graph restoreTensorflow 在图恢复期间抱怨缺少 feed_dict
【发布时间】:2017-03-02 19:12:51
【问题描述】:

我已经构建了一个用于图像分类的 CNN。在训练期间,我保存了几个检查点。数据通过 feed_dictionary 输入网络。

现在我想恢复失败的模型,但我不知道为什么。重要的代码行如下:

with tf.Graph().as_default():

....

if checkpoint_dir is not None:
    checkpoint_saver = tf.train.Saver()
    session_hooks.append(tf.train.CheckpointSaverHook(checkpoint_dir,
                                                      save_secs=flags.save_interval_secs,
                                                      saver=checkpoint_saver))
....

with tf.train.MonitoredTrainingSession(
        save_summaries_steps=flags.save_summaries_steps,
        hooks=session_hooks,
        config=tf.ConfigProto(
            log_device_placement=flags.log_device_placement)) as mon_sess:

    checkpoint = tf.train.get_checkpoint_state(checkpoint_dir)
    if checkpoint and checkpoint.model_checkpoint_path:

        # restoring from the checkpoint file
        checkpoint_saver.restore(mon_sess, checkpoint.model_checkpoint_path)

        global_step_restore = checkpoint.model_checkpoint_path.split('/')[-1].split('-')[-1]
        print("Model restored from checkpoint: global_step = %s" % global_step_restore)

“checkpoint_saver.restore”行抛出错误:

Traceback(最近一次调用最后一次): _do_call 中的文件“C:\Program Files\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py”,第 1022 行 返回 fn(*args) _run_fn 中的文件“C:\Program Files\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py”,第 1004 行 状态,运行元数据) 退出中的文件“C:\Program Files\Anaconda3\envs\tensorflow\lib\contextlib.py”,第 66 行 下一个(self.gen) 文件“C:\Program Files\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\errors_impl.py”,第 469 行,在 raise_exception_on_not_ok_status pywrap_tensorflow.TF_GetCode(状态)) tensorflow.python.framework.errors_impl.InvalidArgumentError:您必须使用 dtype float 为占位符张量“input_images”提供一个值 [[节点:input_images = Placeholderdtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]]

有谁知道如何解决这个问题?为什么我需要一个填充的 feed_dictionary 来恢复图形?

提前致谢!

更新:

这是saver对象的restore方法的代码:

  def restore(self, sess, save_path):
    """Restores previously saved variables.

    This method runs the ops added by the constructor for restoring variables.
    It requires a session in which the graph was launched.  The variables to
    restore do not have to have been initialized, as restoring is itself a way
    to initialize variables.

    The `save_path` argument is typically a value previously returned from a
    `save()` call, or a call to `latest_checkpoint()`.

    Args:
      sess: A `Session` to use to restore the parameters.
      save_path: Path where parameters were previously saved.
    """
    if self._is_empty:
      return
    sess.run(self.saver_def.restore_op_name,
             {self.saver_def.filename_tensor_name: save_path})

我不明白:为什么图表会立即执行?我使用了错误的方法吗?我只想恢复所有可训练的变量。

【问题讨论】:

  • 命名所有变量和占位符。这有帮助吗? stackoverflow.com/questions/34793978/…
  • 所有变量都被命名。我的图像张量的输入源丢失。我认为问题是由 MonitoredTrainingSession 和 feed_dict 的组合使用引起的。 MonitoredTrainingSession 旨在用于更大的设置,可能与提要字典不兼容?!?。我正在尝试为我的自定义“培训框架”构建一个测试用例。因此,我想保持示例模型的轻量化(使用 feed_dict 而不是导入队列)

标签: tensorflow feed restore


【解决方案1】:

问题是由进程日志记录的 SessionRunHook 引起的:

原始钩子:

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

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

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

  def after_run(self, run_context, run_values):
    duration = time.time() - self._start_time
    loss_value = run_values.results
    if self._step % 5 == 0:
      num_examples_per_step = FLAGS.batch_size
      examples_per_sec = num_examples_per_step / duration
      sec_per_batch = float(duration)

      format_str = ('%s: step %d, loss = %.2f (%.1f examples/sec; %.3f '
                    'sec/batch)')
      print (format_str % (datetime.now(), self._step, loss_value,
                           examples_per_sec, sec_per_batch))

修改钩子:

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

    def __init__(self, flags, loss_op):
        self._flags = flags
        self._loss_op = loss_op
        self._start_time = time.time()

    def begin(self):
        self._step = 0

    def before_run(self, run_context):
        if self._step == 0:
            run_args = None
        else:
            run_args = tf.train.SessionRunArgs(self._loss_op)

        return run_args

    def after_run(self, run_context, run_values):

        if self._step > 0:
            duration_n_steps = time.time() - self._start_time
            loss_value = run_values.results
            if self._step % self._flags.log_every_n_steps == 0:
                num_examples_per_step = self._flags.batch_size

                duration = duration_n_steps / self._flags.log_every_n_steps
                examples_per_sec = num_examples_per_step / duration
                sec_per_batch = float(duration)

                format_str = ('%s: step %d, loss = %.2f (%.1f examples/sec; %.3f '
                              'sec/batch)')
                print(format_str % (datetime.now(), self._step, loss_value,
                                    examples_per_sec, sec_per_batch))

                self._start_time = time.time()
        self._step += 1

解释:

现在跳过第一次迭代的日志记录。因此,由 Saver.restore(..) 执行的 session.run 不再需要填充的 feed 字典。

【讨论】:

    猜你喜欢
    • 2015-10-31
    • 2018-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多