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 range函数怎么用

Python语言及其特点简介

function是什么意思?

Python的方法是什么

Python实现计算列表元素之和

Python实现在两个字典中寻找相同点的方法(附代码)

十分钟利用Python制作属于你自己的个性logo

Python如何判断闰年

更多相关阅读请进入《Python》频道 >>




打赏

取消

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

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

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

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

评论

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