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基于BeautifulSoup实现抓取网页指定内容的方法
Jul 09 Python
Python使用wxPython实现计算器
Jan 30 Python
python修改list中所有元素类型的三种方法
Apr 09 Python
python爬虫之urllib库常用方法用法总结大全
Nov 14 Python
浅谈django url请求与数据库连接池的共享问题
Aug 29 Python
pygame实现烟雨蒙蒙下彩虹雨
Nov 11 Python
Django密码存储策略分析
Jan 09 Python
tensorflow mnist 数据加载实现并画图效果
Feb 05 Python
Python 分布式缓存之Reids数据类型操作详解
Jun 24 Python
Python pip使用超时问题解决方案
Aug 03 Python
python链表类中获取元素实例方法
Feb 23 Python
python基础之模块的导入
Oct 24 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采用XML-RPC构造Web Service实例教程
2014/07/16 PHP
php封装一个异常的处理类
2017/06/08 PHP
实现png图片和png背景透明(支持多浏览器)的方法
2009/09/08 Javascript
JavaScript使用setInterval()函数实现简单轮询操作的方法
2015/02/02 Javascript
javascript制作的滑动图片菜单
2015/05/15 Javascript
javascript实现的猜数小游戏完整实例代码
2016/05/10 Javascript
Vue写一个简单的倒计时按钮功能
2018/04/20 Javascript
vue router动态路由下让每个子路由都是独立组件的解决方案
2018/04/24 Javascript
vue组件中iview的modal组件爬坑问题之modal的显示与否应该是使用v-show
2019/04/12 Javascript
vue项目中监听手机物理返回键的实现
2020/01/18 Javascript
react ant Design手动设置表单的值操作
2020/10/31 Javascript
使用Vant完成Dialog弹框案例
2020/11/11 Javascript
浅谈Vue开发人员的7个最好的VSCode扩展
2021/01/20 Vue.js
[56:41]2018DOTA2亚洲邀请赛 3.31 小组赛 A组 Newbee vs OG
2018/04/01 DOTA
基于循环神经网络(RNN)实现影评情感分类
2018/03/26 Python
Python3自定义http/https请求拦截mitmproxy脚本实例
2020/05/11 Python
Python xlrd模块导入过程及常用操作
2020/06/10 Python
python如何实现读取并显示图片(不需要图形界面)
2020/07/08 Python
PyCharm Ctrl+Shift+F 失灵的简单有效解决操作
2021/01/15 Python
详解H5本地储存Web Storage
2017/07/03 HTML / CSS
美国最大的船只买卖在线市场:Boat Trader
2018/08/04 全球购物
请编写一个 C 函数,该函数在给定的内存区域搜索给定的字符,并返回该字符所在位置索引值
2014/09/15 面试题
专业毕业生个性的自我评价
2013/10/03 职场文书
国贸专业个人求职信范文
2014/01/08 职场文书
员工趣味活动方案
2014/08/27 职场文书
民主评议政风行风整改方案
2014/09/17 职场文书
领导班子“四风问题”“整改方案
2014/10/02 职场文书
党的群众路线教育实践活动总结
2014/10/30 职场文书
婚礼新人答谢词
2015/01/04 职场文书
2015年师德师风承诺书
2015/01/22 职场文书
活动主持人开场白
2015/05/28 职场文书
2016年先进教师个人事迹材料
2016/02/26 职场文书
七年级作文之《我和我的祖国》观后感作文
2019/10/18 职场文书
Redis Cluster集群动态扩容的实现
2021/07/15 Redis
vue ref如何获取子组件属性值
2022/03/31 Vue.js
vue实现可以快进后退的跑马灯组件
2022/04/08 Vue.js