TensorFlow入门使用 tf.train.Saver()保存模型


当前第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)

 

# Create some variables.

v1 = tf.Variable([11.0, 16.3], name="v1")

v2 = tf.Variable(33.5, name="v2")

 

# Add ops to save and restore all the variables.

saver = tf.train.Saver()

 

# Later, launch the model, use the saver to restore variables from disk, and

# do some work with the model.

# Restore variables from disk.

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)

 

# Create some variables.

v1 = tf.Variable([11.0, 16.3], name="v1")

 

# Add ops to save and restore all the variables.

saver = tf.train.Saver()

 

# Later, launch the model, use the saver to restore variables from disk, and

# do some work with the model.

# Restore variables from disk.

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》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...