Saver类--变量的保存和恢复
来源:互联网 发布:剑灵捏脸数据导入 编辑:程序博客网 时间:2024/05/29 11:44
保存和恢复变量
我们经常在训练完一个模型之后希望保存训练的结果,这些结果指的是模型的参数,以便下次迭代的训练或者用作测试。Tensorflow针对这一需求提供了Saver类。
- Saver类提供了向checkpoints文件保存和从checkpoints文件中恢复变量的相关方法。Checkpoints文件是一个二进制文件,它把变量名映射到对应的tensor值 。
- 只要提供一个计数器,当计数器触发时,Saver类可以自动的生成checkpoint文件。这让我们可以在训练过程中保存多个中间结果。例如,我们可以保存每一步训练的结果。
- 为了避免填满整个磁盘,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
选择存储和恢复哪些变量
(这里先大概写一些简单的东西,以后碰到更高级的应用再回来补充,历史总是螺旋形的上升嘛,学习也一样,不能指望一口吃成胖子)
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变量初始了了一次吧。
阅读全文
0 0
- Saver类--变量的保存和恢复
- tensorflow saver 保存和恢复指定 tensor
- tensorflow 模型的保存与恢复(Saver)
- tensorflow训练模型保存saver和恢复restore
- tf.train.Saver函数的用法之保存全部变量和模型
- tf.train.Saver函数的用法之保存全部变量和模型
- tensorflow 1.0 学习:模型的保存与恢复(Saver)
- IDl保存和恢复变量的方法
- Tensorflow的模型保存和读取tf.train.Saver
- Tensorflow学习(6)模型的保存与恢复(saver)
- TensorFlow利用saver保存和提取参数
- TensorFlow saver之指定变量的存取
- Saver保存提取
- RNN,LSTM手写数组的识别,saver保存以及加载。
- 保存和恢复activity的状态数据
- 保存和恢复activity的状态数据
- Windows Phone 数据的保存和恢复
- 退出应用数据的保存和恢复
- 远程连接服务器
- IndiaHacks 2nd Elimination 2017 A
- Source Insight 4.0显示Tab键、空格以及Tab键转为4个空格
- 设计模式初探-策略模式
- Apache Kafka:大数据的实时处理时代
- Saver类--变量的保存和恢复
- android pcm编解码
- logback通过kafka接入ELK详解
- Zaqar Data Flow(Newton 版)
- mvn详解
- 欢迎使用CSDN-markdown编辑器
- Java就业企业面试问题-ssm框架
- replace用法
- maven 上传jar 包 到nexus 私服