132、TensorFlow加载模型

# The tf.train.Saver对象不仅保存变量到checkpoint文件
# 它也恢复变量,当你恢复变量的时候,你就不必须要提前初始化他们
# 列如如下的代码片段解释了如何去调用tf.train.Saver.restore方法,来从checkpoint文件中恢复变量
import tensorflow as tf
tf.reset_default_graph()

# Create some variables
v1 = tf.get_variable("v1", shape=[3])
v2 = tf.get_variable("v2", shape=[5])

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

# 之后,加载模型,使用saver从磁盘加载变量
# 并且使用加载后的模型来进行一些计算
with tf.Session() as sess:
    # 从磁盘加载变量
    saver.restore(sess, "tmp/model.ckpt")
    print("Model   restored.")
    # 检查变量的值
    print("v1:%s" % v1.eval())
    print("v2:%s" % v2.eval())
#没有物理文件叫做/tmp/model.ckpt
#model.ckpt只是物理文件的前缀,我们只通过前缀来操作保存的模型

结果如下所示:

2018-02-17 11:23:12.682982: I C:	f_jenkinsworkspace
el-winMwindowsPY35	ensorflowcoreplatformcpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
Model   restored.
v1:[ 1.  1.  1.]
v2:[-1. -1. -1. -1. -1.]
原文地址:https://www.cnblogs.com/weizhen/p/8451511.html