TensorFlow,为什么保存模型后有3个文件? [英] TensorFlow, why there are 3 files after saving the model?

查看:70
本文介绍了TensorFlow,为什么保存模型后有3个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读了文档后,我在TensorFlow<中保存了一个模型/code>,这是我的演示代码:

Having read the docs, I saved a model in TensorFlow, here is my demo code:

# Create some variables.
v1 = tf.Variable(..., name="v1")
v2 = tf.Variable(..., 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)

但是在那之后,我发现有3个文件

but after that, I found there are 3 files

model.ckpt.data-00000-of-00001
model.ckpt.index
model.ckpt.meta

而且我无法通过恢复 model.ckpt 文件来恢复模型,因为没有这样的文件.这是我的代码

And I can't restore the model by restore the model.ckpt file, since there is no such file. Here is my code

with tf.Session() as sess:
  # Restore variables from disk.
  saver.restore(sess, "/tmp/model.ckpt")

那么,为什么有 3 个文件?

So, why there are 3 files?

推荐答案

试试这个:

with tf.Session() as sess:
    saver = tf.train.import_meta_graph('/tmp/model.ckpt.meta')
    saver.restore(sess, "/tmp/model.ckpt")

TensorFlow save 方法保存三种文件,因为它将图形结构变量值分开存储..meta 文件描述了保存的图结构,因此需要在恢复检查点之前导入它(否则它不知道保存的检查点值对应哪些变量).

The TensorFlow save method saves three kinds of files because it stores the graph structure separately from the variable values. The .meta file describes the saved graph structure, so you need to import it before restoring the checkpoint (otherwise it doesn't know what variables the saved checkpoint values correspond to).

或者,您可以这样做:

# Recreate the EXACT SAME variables
v1 = tf.Variable(..., name="v1")
v2 = tf.Variable(..., name="v2")

...

# Now load the checkpoint variable values
with tf.Session() as sess:
    saver = tf.train.Saver()
    saver.restore(sess, "/tmp/model.ckpt")

即使没有名为 model.ckpt 的文件,您在恢复它时仍然以该名称引用保存的检查点.来自 saver.py 源代码:

Even though there is no file named model.ckpt, you still refer to the saved checkpoint by that name when restoring it. From the saver.py source code:

用户只需要与用户指定的前缀进行交互...而不是任何物理路径名.

Users only need to interact with the user-specified prefix... instead of any physical pathname.

这篇关于TensorFlow,为什么保存模型后有3个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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