TensorFlow⼊门使⽤tf.train.Saver()保存模型
关于模型保存的⼀点⼼得
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'
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.
tensorflow入门教程
# Restore variables from disk.
ckpt_path = './ckpt/test-model.ckpt'
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() 巧妙搭配,可以参考:【迁移学习】
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。