通过tf.train.Saver类实现保存和载入神经网络模型

import tensorflowas 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:

sess.run(tf.global_variables_initializer())

savr_path = saver.save(sess,"./model/model.ckpt")

print(savr_path)

读取

import tensorflowas 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,"./model/model.ckpt")# 注意此处路径前添加"./"

    print(sess.run(result))# [ 3.]

你可能感兴趣的:(通过tf.train.Saver类实现保存和载入神经网络模型)