关于模型保存的一点心得
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() 巧妙搭配,可以参考: 【迁移学习】往一个已经保存好的模型添加新的变量并进行微调
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
- 敢达争锋对决全新10月超限定机体上线
- 上限最高的四大英雄,无一不是天花板!
- 小乔全面教学,一闪必须会,前期可以凶
- 机关百炼1-3关速通攻略,选对英雄和武器
- 《暗喻幻想:ReFantazio》最新TVCM公开!10月发售
- 爆料:《刺客信条:影》跳票原因是开发近况不佳
- 《暗喻幻想》Xbox版M站92分:Atlus新的巅峰
- 《黑神话》新一批角色原画欣赏!白毛寅虎似将军!
- Steam好评如潮!《寂静岭2RE》普通版现已解锁
- 2024国庆档总票房20.97亿 《志愿军:存亡之战》超8亿夺冠
- 《原神》部族龙形武士讨伐路线推荐 部族龙形武士位置一览
- 《原神》5.0浪沫羽鳃高效采集路线推荐 浪沫羽鳃位置一览
- 《原神》5.0深渊12层配队推荐
- 曾淑勤.2019-爱在山谷回荡【种子音乐】【WAV+CUE】
- 后弦.2006-9公主(EP)【东升世纪】【WAV+CUE】