当前第2页 返回上一页
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config = config)
v1 = tf.Variable([ 11.0 , 16.3 ], name = "v1" )
v2 = tf.Variable( 33.5 , name = "v2" )
saver = tf.train.Saver()
ckpt_path = './ckpt/test-model.ckpt'
saver.restore(sess, ckpt_path + '-' + str ( 1 ))
print ( "Model restored." )
print sess.run(v1)
print sess.run(v2)
|
INFO:tensorflow:Restoring parameters from ./ckpt/test-model.ckpt-1
Model restored.
[ 1. 2.29999995]
55.5
导入模型之前,必须重新再定义一遍变量。
但是并不需要全部变量都重新进行定义,只定义我们需要的变量就行了。
也就是说,你所定义的变量一定要在 checkpoint 中存在;但不是所有在checkpoint中的变量,你都要重新定义。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config = config)
v1 = tf.Variable([ 11.0 , 16.3 ], name = "v1" )
saver = tf.train.Saver()
ckpt_path = './ckpt/test-model.ckpt'
saver.restore(sess, ckpt_path + '-' + str ( 1 ))
print ( "Model restored." )
print sess.run(v1)
|
INFO:tensorflow:Restoring parameters from ./ckpt/test-model.ckpt-1
Model restored.
[ 1. 2.29999995]
tf.Saver([tensors_to_be_saved]) 中可以传入一个 list,把要保存的 tensors 传入,如果没有给定这个list的话,他会默认保存当前所有的 tensors。一般来说,tf.Saver 可以和 tf.variable_scope() 巧妙搭配,可以参考: 【迁移学习】往一个已经保存好的模型添加新的变量并进行微调
相关推荐:
关于Tensorflow中的tf.train.batch函数
以上就是TensorFlow入门使用 tf.train.Saver()保存模型的详细内容,更多文章请关注木庄网络博客!!
返回前面的内容
相关阅读 >>
面向对象进阶
Python为什么适合人工智能
Python range函数怎么用
Python语言及其特点简介
function是什么意思?
Python的方法是什么
Python实现计算列表元素之和
Python实现在两个字典中寻找相同点的方法(附代码)
十分钟利用Python制作属于你自己的个性logo
Python如何判断闰年
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » TensorFlow入门使用 tf.train.Saver()保存模型