Saver类--变量的保存和恢复

来源:互联网 发布:剑灵捏脸数据导入 编辑:程序博客网 时间:2024/05/29 11:44

保存和恢复变量

我们经常在训练完一个模型之后希望保存训练的结果,这些结果指的是模型的参数,以便下次迭代的训练或者用作测试。Tensorflow针对这一需求提供了Saver类。

  1. Saver类提供了向checkpoints文件保存和从checkpoints文件中恢复变量的相关方法。Checkpoints文件是一个二进制文件,它把变量名映射到对应的tensor值 。
  2. 只要提供一个计数器,当计数器触发时,Saver类可以自动的生成checkpoint文件。这让我们可以在训练过程中保存多个中间结果。例如,我们可以保存每一步训练的结果。
  3. 为了避免填满整个磁盘,Saver可以自动的管理Checkpoints文件。例如,我们可以指定保存最近的N个Checkpoints文件。

saver.save(sess, 'my-model', global_step=0) ==> filename: 'my-model-0'saver.save(sess, 'my-model', global_step=1000) ==> filename: 'my-model-1000'


保存方法

import tensorflow as tf # Create some variables.v1 = tf.Variable(tf.ones(shape=[2,3]), name="v1")v2 = tf.Variable(tf.ones(shape=[2,2]), 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()# Later, launch the model, initialize the variables, do some work, save the# variables to disk.with tf.Session() as sess:  sess.run(init_op)  # Do some work with the model.  # Save the variables to disk.  save_path = saver.save(sess, "./tmp/model.ckpt",global_step=100)  print("Model saved in file:%s "%(save_path))

恢复变量用tf.train.Saver().restore(),例子,恢复的变量,不需要事先初始化

# Create some variables.import tensorflow as tfv1 = tf.Variable(tf.ones(shape=[2,3]), name="v1")v2 = tf.Variable(tf.ones(shape=[2,2]), 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.with tf.Session() as sess:  # Restore variables from disk.    saver.restore(sess, "./tmp/model.ckpt-100")    print("v1 name is%s v1=%s"%(v1.name,sess.run(v1)))    print("v2 name is%s v1=%s"%(v2.name,sess.run(v2)))  # Do some work with the model

选择存储和恢复哪些变量

(这里先大概写一些简单的东西,以后碰到更高级的应用再回来补充,历史总是螺旋形的上升嘛,学习也一样,不能指望一口吃成胖子)

先指定保存变量V2到 ./board/model.ckpt的检查点文件中

import tensorflow as tf # Create some variables.v1 = tf.Variable(tf.ones(shape=[2,3]), name="v1")v2 = tf.Variable(tf.ones(shape=[2,2]), 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({'my_v2':v2})# Later, launch the model, initialize the variables, do some work, save the# variables to disk.with tf.Session() as sess:    sess.run(init_op)    # Do some work with the model.    # Save the variables to disk.    save_path = saver.save(sess, "./board/model.ckpt",global_step=100)    print("Model saved in file:%s "%(save_path))


再从检查点文件model-100中恢复V2出来

import tensorflow as tf # Create some variables.v1 = tf.Variable(tf.zeros(shape=[2,3]), name="v1")v2 = tf.Variable(tf.zeros(shape=[2,2]), name="v2")# Add ops to save and restore all the variables.saver = tf.train.Saver({'my_v2':v2})# Later, launch the model, use the saver to restore variables from disk, and# do some work with the model.with tf.Session() as sess:  # Restore variables from disk.    sess.run(tf.global_variables_initializer())    saver.restore(sess, "./board/model.ckpt-100")#注意,这时就不用再对变量用进行初始化了    print("v2 name is%s v2=%s"%(v2.name,sess.run(v2)))    print("v1 name is%s v1=%s"%(v1.name,sess.run(v1)))

尝试了一下,先进行初始化,再恢复变量,好像对结果并没有什么影响,可能初始化V1,V2变量之后,saver.restore()又将V2变量初始了了一次吧。

原创粉丝点击