如何在 Tensorflow r12 中按文件名恢复模型? [英] How to restore a model by filename in Tensorflow r12?

查看:42
本文介绍了如何在 Tensorflow r12 中按文件名恢复模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经运行了分布式 mnist 示例:https://github.com/tensorflow/tensorflow/blob/r0.12/tensorflow/tools/dist_test/python/mnist_replica.py

I have run the distributed mnist example: https://github.com/tensorflow/tensorflow/blob/r0.12/tensorflow/tools/dist_test/python/mnist_replica.py

虽然我已经设置了

saver = tf.train.Saver(max_to_keep=0)

在以前的版本中,如 r11,我能够运行每个检查点模型并评估模型的精度.这给了我一个精度与全局步骤(或迭代)的进度图.

In previous release, like r11, I was able to run over each check point model and evaluate the precision of the model. This gave me a plot of the progress of the precision versus global steps (or iterations).

在 r12 之前,tensorflow 检查点模型保存在两个文件中,model.ckpt-1234model-ckpt-1234.meta.可以通过传递 model.ckpt-1234 文件名来恢复模型,就像 saver.restore(sess,'model.ckpt-1234') 一样.

Prior to r12, tensorflow checkpoint models were saved in two files, model.ckpt-1234 and model-ckpt-1234.meta. One could restore a model by passing the model.ckpt-1234 filename like so saver.restore(sess,'model.ckpt-1234').

但是,我注意到在 r12 中,现在有三个输出文件 model.ckpt-1234.data-00000-of-000001, model.ckpt-1234.indexmodel.ckpt-1234.meta.

However, I've noticed that in r12, there are now three output files model.ckpt-1234.data-00000-of-000001, model.ckpt-1234.index, and model.ckpt-1234.meta.

我看到恢复文档说应该使用诸如 /train/path/model.ckpt 之类的路径而不是文件名来进行恢复.有没有办法一次加载一个检查点文件来评估它?我尝试通过 model.ckpt-1234.data-00000-of-000001model.ckpt-1234.indexmodel.ckpt-1234.meta 文件,但出现如下错误:

I see that the the restore documentation says that a path such as /train/path/model.ckpt should be given to restore instead of a filename. Is there any way to load one checkpoint file at a time to evaluate it? I have tried passing the model.ckpt-1234.data-00000-of-000001, model.ckpt-1234.index, and model.ckpt-1234.meta files, but get errors like below:

W tensorflow/core/util/tensor_slice_reader.cc:95] 无法打开 logdir/2016-12-08-13-54/mo​​del.ckpt-0.data-00000-of-00001: 数据丢失: not an sstable (bad magic number): 也许你的文件是不同的文件格式,你需要使用不同的恢复运算符?

NotFoundError(回溯见上文):在检查点文件中找不到张量名称hid_b" logdir/2016-12-08-13-54/mo​​del.ckpt-0.index[[节点:save/RestoreV2_1 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save/Const_0, save/RestoreV2_1/tensor_names, save/恢复V2_1/shape_and_slices)]]

W tensorflow/core/util/tensor_slice_reader.cc:95] 无法打开 logdir/2016-12-08-13-54/mo​​del.ckpt-0.meta: Data loss: not an sstable (bad幻数):也许你的文件是不同的文件格式,你需要使用不同的恢复运算符?

我在 OSX Sierra 上运行,通过 pip 安装了 tensorflow r12.

I'm running on OSX Sierra with tensorflow r12 installed via pip.

任何指导都会有所帮助.

Any guidance would be helpful.

谢谢.

推荐答案

我也使用了 Tensorlfow r0.12,我认为保存和恢复模型没有任何问题.下面是一段简单的代码,你可以试试:

I also used Tensorlfow r0.12 and I didn't think there is any issue for saving and restoring model. The following is a simple code that you can have a try:

import tensorflow as tf

# Create some variables.
v1 = tf.Variable(tf.random_normal([784, 200], stddev=0.35), name="v1")
v2 = tf.Variable(tf.random_normal([784, 200], stddev=0.35), name="v2")

# Add an op to initialize the variables.
init_op = tf.global_variables_initializer()

# Add ops to save and restore all the variables.
saver = tf.train.Saver()

# Later, launch the model, initialize the variables, do some work, save the
# variables to disk.
with tf.Session() as sess:
  sess.run(init_op)
  # Do some work with the model.

  # Save the variables to disk.
  save_path = saver.save(sess, "/tmp/model.ckpt")
  print("Model saved in file: %s" % save_path)

# Later, launch the model, use the saver to restore variables from disk, and
# do some work with the model.
with tf.Session() as sess:
  # Restore variables from disk.
  saver.restore(sess, "/tmp/model.ckpt")
  print("Model restored.")
  # Do some work with the model

尽管在 r0.12 中,检查点存储在多个文件中,但您可以使用通用前缀(在您的情况下为model.ckpt")来恢复它.

although in r0.12, the checkpoint is stored in multiple files, you can restore it by using the common prefix, which is 'model.ckpt' in your case.

这篇关于如何在 Tensorflow r12 中按文件名恢复模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆