保存和恢复变量

我们经常在训练完一个模型之后希望保存训练的结果,这些结果指的是模型的参数,以便下次迭代的训练或者用作测试。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 tf
v1 = 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变量初始了了一次吧。

Saver类--变量的保存和恢复相关推荐

  1. 【Tensorflow教程笔记】常用模块 tf.train.Checkpoint :变量的保存与恢复

    基础 TensorFlow 基础 TensorFlow 模型建立与训练 基础示例:多层感知机(MLP) 卷积神经网络(CNN) 循环神经网络(RNN) 深度强化学习(DRL) Keras Pipeli ...

  2. Delphi语言如何对自定义类进行持久化保存及恢复 (性能远比json/xml高)

    Delphi的RTL自身就带有一套很好的资源持久化保存(IDE设计窗体时,保存为DFM格式及编译到EXE里面的资源文件)及恢复机制(EXE启动时对窗体资源的加载),那么应没必要再额外用xml/json ...

  3. Tensorflow |(5)模型保存与恢复、自定义命令行参数

    Tensorflow |(1)初识Tensorflow Tensorflow |(2)张量的阶和数据类型及张量操作 Tensorflow |(3)变量的的创建.初始化.保存和加载 Tensorflow ...

  4. TensorFlow - 保存和恢复

    TensorFlow - 保存和恢复 https://tensorflow.google.cn/guide/saved_model TensorFlow 指南 - TensorFlow 工作原理 ht ...

  5. TensorFlow:模型的保存与恢复(Saver)

    目录 前言 1 实例化对象 2 保存训练过程中或者训练好的, 模型图及权重参数 2.1保存训练模型 2.2 查看保存 3. 重载模型的图及权重参数(模型恢复)     前言 我们经常在训练完一个模型之 ...

  6. tensorflow 1.0 学习:模型的保存与恢复(Saver)

    将训练好的模型参数保存起来,以便以后进行验证或测试,这是我们经常要做的事情.tf里面提供模型保存的是tf.train.Saver()模块. 模型保存,先要创建一个Saver对象:如 saver=tf. ...

  7. Tensorflow模型的保存与恢复的细节

    翻译自:http://cv-tricks.com/tensorflow-tutorial/save-restore-tensorflow-models-quick-complete-tutorial/ ...

  8. 简单完整地讲解tensorflow模型的保存和恢复

    http://blog.csdn.net/liangyihuai/article/details/78515913 在本教程主要讲到: 1. 什么是Tensorflow模型? 2. 如何保存Tenso ...

  9. Tensorflow【实战Google深度学习框架】TensorFlow模型的保存与恢复加载

    我们使用TensorFlow进行模型的训练,训练好的模型需要保存,预测阶段我们需要将模型进行加载还原使用,这就涉及TensorFlow模型的保存与恢复加载. 总结一下Tensorflow常用的模型保存 ...

  10. Android Fragment使用(三) Activity, Fragment, WebView的状态保存和恢复

    Android中的状态保存和恢复 Android中的状态保存和恢复, 包括Activity和Fragment以及其中View的状态处理. Activity的状态除了其中的View和Fragment的状 ...

最新文章

  1. jsp java乱码转换_Java Web 编码问题一:jsp的编码问题
  2. python编程到底难不难_养成下面几个编程习惯,学习python并不难!
  3. python学习方向-Python方向(转载)
  4. 去掉WinLicense文件效验的方法
  5. 【C语言进阶深度学习记录】十九 #pragma使用与分析
  6. 实用的Portraiture滤镜磨皮教程
  7. (三维偏序)陌上花开
  8. 【Unity】替换场景、Prefab字体 工具类
  9. android 手机主题制作,怎么制作手机主题?
  10. SAP License:实例讲解SAP与金税接口
  11. 入侵手游服务器修改数据库,如何入侵手游服务器数据库
  12. Word文件带密码如何解除?
  13. 企业实战之部署Solarwinds Network八部众
  14. Pearl Pairing
  15. Xeam Visual Installer白金版,Xeam Visual Installer完整用户体验
  16. 清华大学五道口金融学院2023年博士生招生简章(普博+直博)
  17. font face=微软雅黑 color=DodgerBlue*IncomesESL Analy*/font
  18. adb连接夜神模拟器和连接夜神多开的方法
  19. C语言程序设计之通讯录
  20. pcs for linux7下载,centos7上实现corosync V2 + pacemaker + pcs | crmsh备忘录

热门文章

  1. Aspect Level Sentiment Classification with Deep Memory Network
  2. Windows 下使用 TFTPD32+HTTP PXE引导安装linux
  3. Android显示图片崩溃的解决办法
  4. hotmail手机端_hotmail邮箱官方版|hotmail邮箱手机版下载_v7.8.2_9ht安卓下载
  5. 计算机学院表白情书,大学各专业的表白情书!啊,我的少女心要炸了…
  6. GRE隧道封装协议及内核处理解析
  7. 离线环境下火狐浏览器Firefox完全信息迁移
  8. 如何使用vsCode+Icarus verilog+GTKwave编写并仿真verilog
  9. 基于Vue使用Arco Design组件封装一个七牛云上传图片的函数
  10. Winner-Take-All Autoencoders ( 赢者通吃自编码器)