当前第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做app用什么工具
详解Python中super()函数的用法及工作原理
pyqt4实现下拉菜单可供选择并打印出来
对Python的链表数据结构讲解
Python3读取excel数据存入mysql的方法
Python的调试;print()和断言(实例解析二)
Python安装包怎么下载
Python如何另起一行?
Python中列表、字符串、字典常用操作总结
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » TensorFlow入门使用 tf.train.Saver()保存模型