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

2018-03-08  本文已影响0人  waitfun02

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.]

上一篇 下一篇

猜你喜欢

热点阅读