-
持久化代码
-
保存图
import tensorflow as tf
v1 = tf.Variable(tf.constant(1.0,shape=[1]),name="v1")
v2 = tf.Variable(tf.constant(2.0,shape=[1]),name="v2")
result = v1 + v2
`init_op = tf.global_variables_initializer()
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(init_op)
saver.save(sess,"F:\path\model\model.cket")``
生成文件
model.ckepy.meta 计算图结构
model.ckpt 程序中每个变量取值
checkpoint 一个目录下所有模型文件列表
- 加载已经保存的TensorFlow模型
import tensorflow as tf
v1 = tf.Variable(tf.constant(1.0,shape=[1]),name="v1")
v2 = tf.Variable(tf.constant(2.0,shape=[1]),name="v2")
result = v1 + v2
saver = tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess,"F:\path\model\model.cket")
print(sess.run(result))
- 直接加载已经持久化的图
mport tensorflow as tf
v1 = tf.Variable(tf.constant(1.0,shape=[1]),name="v1")
v2 = tf.Variable(tf.constant(2.0,shape=[1]),name="v2")
result = v1 + v2
saver = tf.train.import_meta_graph(
"F:\path\model\model.cket\model.ckept.meta")
with tf.Session() as sess:
saver.restore(sess,"F:\path\model\model.cket")
print(sess.run(tf.get_default_graph().get_tensor_by_name("add:0")))
- 使用字典加载部分变量
v1 = tf.Variable(tf.constant(1.0,shape=[1]),name="other-v1")
v2 = tf.Variable(tf.constant(2.0,shape=[1]),name="other-v2")
sever = tf.train.Saver({"v1":v1,"v2":v2})