import tensorflow as tf
# construct graph
v1 = tf.Variable([0], name='v1')
v2 = tf.Variable([0], name='v2')
# run graph
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
saver.save(sess, 'ckp')
索引文件和数据文件之间有什么关系?
.
├── checkpoint
├── ckp.data-00000-of-00001
├── ckp.index
├── ckp.meta
答案 0 :(得分:3)
此问题与TensorFlow, why there are 3 files after saving the model?类似,但不会详细讨论.index
和.data-00000-of-00001
文件的内容。
同样,What is the TensorFlow checkpoint meta file?回答了.meta
文件的作用。
.index
存储保存的变量名称和形状列表。因此,它的尺寸通常要小得多。 .data-00000-of-00001
存储保存的所有变量的实际值。因此,它的尺寸通常要大得多。
我们可以使用下面的代码测试它。但在此之前,请在tensorflow/tensorflow/examples/tutorials/mnist/fully_connected_feed.py
中运行MNIST示例以生成日志文件。
import tensorflow as tf
from tensorflow.python.training import checkpoint_utils as cp
print cp.list_variables('/tmp/tensorflow/mnist/logs/fully_connected_feed/model.ckpt-1999')
print cp.load_variable('/tmp/tensorflow/mnist/logs/fully_connected_feed/model.ckpt-1999', 'hidden1/biases')
cp.list_variables
然后打印出以下内容:
[('global_step', []), ('hidden1/biases', [128]), ('hidden1/weights', [784, 128]), ('hidden2/biases', [32]), ('hidden2/weights', [128, 32]), ('softmax_linear/biases', [10]), ('softmax_linear/weights', [32, 10])]
然后 cp.load_variable
打印出浮点值的整个向量:
[ 1.49112539e-02 2.43028291e-02 1.82662811e-02 2.32475083e-02
-7.84891471e-03 1.87947564e-02 -6.21244172e-03 9.12105478e-03
-1.70869497e-03 2.94519793e-02 6.23045377e-02 1.99174266e-02
...
1.13238255e-02 -1.11185517e-02 2.25203596e-02 -4.95722517e-04
1.22644939e-02 9.39049758e-03 3.05090044e-02 1.62753556e-02
2.32785419e-02 3.78636681e-02 2.61069946e-02 2.02859659e-02]
cp.list_variables
只能在.index
出现时运行,但cp.load_variable
要求同时运行.index
和.data-00000-of-00001
。
答案 1 :(得分:1)
可以从comments in TF source code找到。
“。index”文件是一个字符串字符串不可变表 (tensorflow ::表::表)。每个键都是张量的名称及其 value是序列化的BundleEntryProto。每个BundleEntryProto 描述张量的元数据:哪个“数据”文件包含 张量的内容,到该文件的偏移量,校验和,一些 辅助数据等