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


Posted in Python onApril 24, 2018

关于模型保存的一点心得

saver = tf.train.Saver(max_to_keep=3)

在定义 saver 的时候一般会定义最多保存模型的数量,一般来说,如果模型本身很大,我们需要考虑到硬盘大小。如果你需要在当前训练好的模型的基础上进行 fine-tune,那么尽可能多的保存模型,后继 fine-tune 不一定从最好的 ckpt 进行,因为有可能一下子就过拟合了。但是如果保存太多,硬盘也有压力呀。如果只想保留最好的模型,方法就是每次迭代到一定步数就在验证集上计算一次 accuracy 或者 f1 值,如果本次结果比上次好才保存新的模型,否则没必要保存。

如果你想用不同 epoch 保存下来的模型进行融合的话,3到5 个模型已经足够了,假设这各融合的模型成为 M,而最好的一个单模型称为 m_best, 这样融合的话对于M 确实可以比 m_best 更好。但是如果拿这个模型和其他结构的模型再做融合的话,M 的效果并没有 m_best 好,因为M 相当于做了平均操作,减少了该模型的“特性”。

但是又有一种新的融合方式,就是利用调整学习率来获取多个局部最优点,就是当 loss 降不下了,保存一个 ckpt, 然后开大学习率继续寻找下一个局部最优点,然后用这些 ckpt 来做融合,还没试过,单模型肯定是有提高的,就是不知道还会不会出现上面再与其他模型融合就没提高的情况。

如何使用 tf.train.Saver() 来保存模型

之前一直出错,主要是因为坑爹的编码问题。所以要注意文件的路径绝对不不要出现什么中文呀。

import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)

# Create some variables.
v1 = tf.Variable([1.0, 2.3], name="v1")
v2 = tf.Variable(55.5, name="v2")

# Add an op to initialize the variables.
init_op = tf.global_variables_initializer()

# Add ops to save and restore all the variables.
saver = tf.train.Saver()

ckpt_path = './ckpt/test-model.ckpt'
# Later, launch the model, initialize the variables, do some work, save the
# variables to disk.
sess.run(init_op)
save_path = saver.save(sess, ckpt_path, global_step=1)
print("Model saved in file: %s" % save_path)

Model saved in file: ./ckpt/test-model.ckpt-1

注意,在上面保存完了模型之后。应该把 kernel restart 之后才能使用下面的模型导入。否则会因为两次命名 “v1” 而导致名字错误。

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中的变量,你都要重新定义。

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() 巧妙搭配,可以参考: 【迁移学习】往一个已经保存好的模型添加新的变量并进行微调

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python相似模块用例
Mar 04 Python
python 网络编程详解及简单实例
Apr 25 Python
Python多进程multiprocessing用法实例分析
Aug 18 Python
Python+OpenCV+pyQt5录制双目摄像头视频的实例
Jun 28 Python
python tkinter实现屏保程序
Jul 30 Python
基于Python新建用户并产生随机密码过程解析
Oct 08 Python
Python基于类路径字符串获取静态属性
Mar 12 Python
django 数据库返回queryset实现封装为字典
May 19 Python
python实现PDF中表格转化为Excel的方法
Jun 16 Python
Python使用文件操作实现一个XX信息管理系统的示例
Jul 02 Python
ffmpeg+Python实现B站MP4格式音频与视频的合并示例代码
Oct 21 Python
python实现手机推送 代码也就10行左右
Apr 12 Python
Python使用 Beanstalkd 做异步任务处理的方法
Apr 24 #Python
Windows上使用Python增加或删除权限的方法
Apr 24 #Python
python编写暴力破解zip文档程序的实例讲解
Apr 24 #Python
解决python删除文件的权限错误问题
Apr 24 #Python
python3+PyQt5实现自定义流体混合窗口部件
Apr 24 #Python
python3+PyQt5实现拖放功能
Apr 24 #Python
python3+PyQt5使用数据库表视图
Apr 24 #Python
You might like
php环境配置 php5 mysql5 apache2 phpmyadmin安装与配置
2006/11/17 PHP
把1316这个数表示成两个数的和,其中一个为13的倍数,另一个是11的倍数,求这两个数。
2011/06/24 PHP
php中return的用法实例分析
2015/02/28 PHP
详细解读php的命名空间(一)
2018/02/21 PHP
php支付宝APP支付功能
2020/07/29 PHP
真正的JQuery.ajax传递中文参数的解决方法
2011/05/28 Javascript
js实例属性和原型属性示例详解
2014/11/23 Javascript
浅析JS异步加载进度条
2016/05/05 Javascript
浅谈js中同名函数和同名变量的执行问题
2017/02/12 Javascript
nodejs中Express与Koa2对比分析
2018/02/06 NodeJs
Vue递归实现树形菜单方法实例
2018/11/06 Javascript
小程序实现日历左右滑动效果
2019/10/21 Javascript
python实现跨文件全局变量的方法
2014/07/07 Python
python操作ie登陆土豆网的方法
2015/05/09 Python
python实现的简单FTP上传下载文件实例
2015/06/30 Python
Python实现简易端口扫描器代码实例
2017/03/15 Python
Python2与Python3的区别实例总结
2019/04/17 Python
Python控制台输出时刷新当前行内容而不是输出新行的实现
2020/02/21 Python
Django 解决由save方法引发的错误
2020/05/21 Python
Python如何安装第三方模块
2020/05/28 Python
有关HTML5 Video对象的ontimeupdate事件(Chrome上无效)的问题
2013/07/19 HTML / CSS
Gina Bacconi官网:吉娜贝康尼连衣裙和礼服
2018/04/24 全球购物
英国排名第一的礼品体验公司:Red Letter Days
2018/08/16 全球购物
三星新西兰官网:Samsung新西兰
2019/03/05 全球购物
请介绍一下Ant
2016/07/22 面试题
英文自荐信
2013/12/19 职场文书
巧克力蛋糕店创业计划书
2014/01/14 职场文书
成语的广告词
2014/03/19 职场文书
中学生演讲稿
2014/04/26 职场文书
2015年元旦活动总结
2014/05/09 职场文书
大型演出策划方案
2014/05/28 职场文书
化工工艺设计求职信
2014/06/25 职场文书
中国梦团日活动总结
2014/07/07 职场文书
2014物价局民主生活会对照检查材料思想汇报
2014/09/24 职场文书
学习作风建设心得体会
2014/10/22 职场文书
Nest.js参数校验和自定义返回数据格式详解
2021/03/29 Javascript