gpt4 book ai didi

TensorFlow,为什么保存模型后有3个文件?

转载 作者:行者123 更新时间:2023-12-03 04:48:38 24 4
gpt4 key购买 nike

已阅读docs ,我在 TensorFlow 中保存了一个模型,这是我的演示代码:

# 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个文件

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

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

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

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

最佳答案

试试这个:

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

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

或者,您可以这样做:

# 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 source code :

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

关于TensorFlow,为什么保存模型后有3个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41265035/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com