# tf.train.Saver类提供了保存和恢复模型的方法
# tf.train.Saver的构造函数 提供了save和恢复的参数选项
# Saver对象提供了方法来运行这些计算节点,制定了写和读的路径
# Saver会恢复所有在你模型当中已经定义的变量
# 如果你加载一个模型没有通知如果构建该模型的计算图
# TensorFlow 在二进制检查点文件中保存变量, 粗略地说, 将变量名映射到张量值
# Saving variable
# 创建一个Saver使用tf.train.Saver()来管理所有的变量在模型中
# 列如下面的代码块解释了如何调用tf.train.Saver.save方法来保存变量到checkpoint文件中
import tensorflow as tf
# Create some variables
v1 = tf.get_variable("v1", shape=[3], initializer=tf.zeros_initializer)
v2 = tf.get_variable("v2", shape=[5], initializer=tf.zeros_initializer)
inc_v1 = v1.assign(v1 + 1)
dec_v2 = v2.assign(v2 - 1)
# 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, and save the variables to disk
with tf.Session() as sess:
sess.run(init_op)
# Do some work with the model
inc_v1.op.run()
dec_v2.op.run()
# Save the variables to disk
save_path = saver.save(sess, "tmp/model.ckpt")
print("Model saved in path : %s " % save_path)

保存的结果如下:

2018-02-17 11:21:22.621424: I C:\tf_jenkins\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
Model saved in path : tmp/model.ckpt
05-27 10:05