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中的MySQLdb模块添加超时功能的教程
May 05 Python
Python3.2模拟实现webqq登录
Feb 15 Python
Python脚本实现自动发带图的微博
Apr 27 Python
Python使用SocketServer模块编写基本服务器程序的教程
Jul 12 Python
Python之自动获取公网IP的实例讲解
Oct 01 Python
PyCharm在win10的64位系统安装实例
Nov 26 Python
详解Python中is和==的区别
Mar 21 Python
详解python运行三种方式
May 13 Python
Python实现计算文件MD5和SHA1的方法示例
Jun 11 Python
Python3安装psycopy2以及遇到问题解决方法
Jul 03 Python
使用Pytorch训练two-head网络的操作
May 28 Python
Python 中的单分派泛函数你真的了解吗
Jun 22 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
《星际争霸2》终章已出 RTS时代宣告终结
2017/02/07 星际争霸
解析centos中Apache、php、mysql 默认安装路径
2013/06/25 PHP
学习php开源项目的源码指南
2014/12/21 PHP
详谈PHP编码转换问题
2015/07/28 PHP
PHP实现导出excel数据的类库用法示例
2016/10/15 PHP
在PHP中实现使用Guzzle执行POST和GET请求
2019/10/15 PHP
laravel-admin 实现在指定的相册下添加照片
2019/10/21 PHP
Laravel5.1 框架控制器基础用法实例分析
2020/01/04 PHP
JavaScript中的this关键字介绍与使用实例
2013/06/21 Javascript
JS基于面向对象实现的放烟花效果
2015/05/07 Javascript
JavaScript检测鼠标移动方向的方法
2015/05/22 Javascript
jQuery获取剪贴板内容的方法
2016/06/16 Javascript
微信小程序 利用css实现遮罩效果实例详解
2017/01/21 Javascript
关于webpack2和模块打包的新手指南(小结)
2017/08/07 Javascript
JavaScript设计模式之调停者模式实例详解
2018/02/03 Javascript
小程序实现五星点评效果
2018/11/03 Javascript
在SSM框架下用laypage和ajax实现分页和数据交互的方法
2019/09/27 Javascript
VUE 解决mode为history页面为空白的问题
2019/11/01 Javascript
详解vue或uni-app的跨域问题解决方案
2020/02/21 Javascript
小程序自定义导航栏兼容适配所有机型(附完整案例)
2020/04/26 Javascript
详解python单元测试框架unittest
2018/07/02 Python
Flask核心机制之上下文源码剖析
2018/12/25 Python
python django下载大的csv文件实现方法分析
2019/07/19 Python
python GUI库图形界面开发之PyQt5窗口控件QWidget详细使用方法
2020/02/26 Python
python opencv 检测移动物体并截图保存实例
2020/03/10 Python
TensorFlow固化模型的实现操作
2020/05/26 Python
详解Python直接赋值,深拷贝和浅拷贝
2020/07/09 Python
Nike英国官网:Nike.com (UK)
2017/02/13 全球购物
英文简历自荐信范文
2013/12/11 职场文书
长安大学毕业生自我鉴定
2014/01/17 职场文书
教师师德师风个人整改方案
2014/09/18 职场文书
2014年超市员工工作总结
2014/11/18 职场文书
2015元旦家电促销活动策划方案
2014/12/09 职场文书
卢旺达饭店观后感
2015/06/05 职场文书
2016年寒假家长评语
2015/10/10 职场文书
Python实现归一化算法详情
2022/03/18 Python