官方中文文档的网址先贴出来:https://tensorflow.google.cn/programmers_guide/saved_model

tf.train.Saver 类别提供了保存和恢复模型的方法。tf.train.Saver 构造函数针对图中所有变量或指定列表的变量将 save 和 restore op 添加到图中。Saver 对象提供了运行这些 op 的方法,指定了写入或读取检查点文件的路径。

TensorFlow 将变量保存在二进制检查点文件中,简略而言,这类文件将变量名称映射到张量值。

保存变量

使用 tf.train.Saver() 创建 Saver 来管理模型中的所有变量。例如,以下代码片段展示了如何调用 tf.train.Saver.save 方法以将变量保存到检查点文件中:

# Create some variables.
v1 = tf.get_variable("v1", shape=[3], initializer = tf.zeros_initializer)
v2 = tf.get_variable("v2", shape=[5], initializer = tf.zeros_initializer) inc_v1 = v1.assign(v1+1)
dec_v2 = v2.assign(v2-1) # 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, and save the
# variables to disk.
with tf.Session() as sess:
  sess.run(init_op)
  # Do some work with the model.
  inc_v1.op.run()
  dec_v2.op.run()
  # Save the variables to disk.
  save_path = saver.save(sess, "/tmp/model.ckpt")
  print("Model saved in path: %s" % save_path)

恢复变量

tf.train.Saver 对象不仅将变量保存到检查点文件中,还将恢复变量。请注意,当您恢复变量时,您不必事先将其初始化。例如,以下代码片段展示了如何调用 tf.train.Saver.restore 方法以从检查点文件中恢复变量:

tf.reset_default_graph()

# Create some variables.
v1 = tf.get_variable("v1", shape=[3])
v2 = tf.get_variable("v2", shape=[5]) # 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")
  print("Model restored.")
  # Check the values of the variables
  print("v1 : %s" % v1.eval())
  print("v2 : %s" % v2.eval())

选择要保存和恢复的变量(选择网络中的部分变量保存或者恢复)

如果您没有向 tf.train.Saver() 传递任何参数,则 Saver 会处理图中的所有变量。每个变量都保存在创建变量时所传递的名称下。

在检查点文件中明确指定变量名称的这种做法有时会非常有用。例如,您可能已经使用名为"weights"的变量训练了一个模型,而您想要将该变量的值恢复到名为"params"的变量中。

有时候,仅保存或恢复模型使用的变量子集也会很有裨益。例如,您可能已经训练了一个五层的神经网络,现在您想要训练一个六层的新模型,并重用该五层的现有权重。您可以使用 Saver 只恢复这前五层的权重。

您可以向 tf.train.Saver() 的构造函数传递以下任一内容来轻松指定要保存或加载的名称和变量:

  • 变量列表(将以其本身的名称保存)。
  • Python 字典,其中,键是要使用的名称,键值是要管理的变量。

继续前面所示的保存/恢复示例:

tf.reset_default_graph()
# Create some variables.
v1 = tf.get_variable("v1", [3], initializer = tf.zeros_initializer)
v2 = tf.get_variable("v2", [5], initializer = tf.zeros_initializer) # Add ops to save and restore only `v2` using the name "v2"
saver = tf.train.Saver({"v2": v2}) # Use the saver object normally after that.
with tf.Session() as sess:
  # Initialize v1 since the saver will not.
  v1.initializer.run()
  saver.restore(sess, "/tmp/model.ckpt")   print("v1 : %s" % v1.eval())
  print("v2 : %s" % v2.eval())

注意:

  • 如果需要保存和恢复模型变量的不同子集,您可以根据需要创建任意数量的 Saver 对象。同一个变量可以列在多个 Saver 对象中,变量的值只有在 Saver.restore() 方法运行时才会更改。

  • 如果您仅在会话开始时恢复模型变量的子集,则必须为其他变量运行初始化 op。有关详情,请参阅 tf.variables_initializer

  • 要检查某个检查点的变量,您可以使用 inspect_checkpoint 库,尤其是 print_tensors_in_checkpoint_file函数。

  • 默认情况下,Saver 会为每个变量使用 tf.Variable.name 属性的值。但是,当您创建一个 Saver 对象时,您可以选择为检查点文件中的变量选择名称(此为可选操作)。

检查某个检查点的变量

可以使用 inspect_checkpoint 库快速检查某个检查点的变量。

# import the inspect_checkpoint library
from tensorflow.python.tools import inspect_checkpoint as chkp # print all tensors in checkpoint file
chkp.print_tensors_in_checkpoint_file("/tmp/model.ckpt", tensor_name='', all_tensors=True) # tensor_name:  v1
# [ 1.  1.  1.]
# tensor_name:  v2
# [-1. -1. -1. -1. -1.] # print only tensor v1 in checkpoint file
chkp.print_tensors_in_checkpoint_file("/tmp/model.ckpt", tensor_name='v1', all_tensors=False) # tensor_name:  v1
# [ 1.  1.  1.] # print only tensor v2 in checkpoint file
chkp.print_tensors_in_checkpoint_file("/tmp/model.ckpt", tensor_name='v2', all_tensors=False) # tensor_name:  v2
# [-1. -1. -1. -1. -1.]

保存和恢复模型概述

如果您想保存和加载变量、图,以及图的元数据 - 简而言之,如果您想保存或恢复模型 - 我们推荐使用 SavedModel。SavedModel 是一种与语言无关,可恢复的密封式序列化格式。SavedModel 可让较高级别的系统和工具创建、使用和变换 TensorFlow 模型。TensorFlow 提供了多种与 SavedModel 交互的机制,如 tf.saved_model API、Estimator API 和 CLI。

用于构建和加载 SavedModel 的 API

 
 
 

保存与恢复变量和模型,tensorflow官方文档阅读笔记的更多相关文章

  1. [E] Shiro 官方文档阅读笔记 The Reading Notes of Shiro's Offical Docs

    官方文档: https://shiro.apache.org/reference.html https://shiro.apache.org/java-authentication-guide.htm ...

  2. python2.7官方文档阅读笔记

    官方地址:https://docs.python.org/2.7/tutorial/index.html 本笔记只记录本人不熟悉的知识点 The Python Tutorial Index 1 Whe ...

  3. 人工智能系统Google开源的TensorFlow官方文档中文版

    人工智能系统Google开源的TensorFlow官方文档中文版 2015年11月9日,Google发布人工智能系统TensorFlow并宣布开源,机器学习作为人工智能的一种类型,可以让软件根据大量的 ...

  4. tensorflow官方文档中的sub 和mul中的函数已经在API中改名了

    在照着tensorflow 官方文档和极客学院中tensorflow中文文档学习tensorflow时,遇到下面的两个问题: 1)AttributeError: module 'tensorflow' ...

  5. TensorFlow官方文档

    关于<TensorFlow官方文档> <TensorFlow官方文档>原文地址:http://devdocs.io/tensorflow~python/ ,本次经过W3Csch ...

  6. TensorFlow 官方文档中文版 --技术文档

    1.文档预览 2.文档下载 TensorFlow官方文档中文版-v1.2.pdf 提取码:pt7p

  7. TensorFlow 官方文档中文版学习

    TensorFlow 官方文档中文版 地址:http://wiki.jikexueyuan.com/project/tensorflow-zh/

  8. TensorFlow 官方文档中文版【转】

    转自:http://wiki.jikexueyuan.com/project/tensorflow-zh/ TensorFlow 官方文档中文版 你正在阅读的项目可能会比 Android 系统更加深远 ...

  9. TensorFlow 官方文档中文版

    http://wiki.jikexueyuan.com/list/deep-learning/ TensorFlow 官方文档中文版 你正在阅读的项目可能会比 Android 系统更加深远地影响着世界 ...

随机推荐

  1. 用python复制文件夹

    用python复制文件 1. 根据文件夹的名称复制 需要复制的文件夹编号文件中,每一行表示一个编号,如下所示: > cat id.txt 1 2 3 ... > 目标文件的目录结构树如下所 ...

  2. c++ vector基本函数、排序、查找用法

    vector用法目录: 1.基本用法 2.vector的删除操作 3.vector的sort排序 4.翻转vector中的所有元素 5.find()函数的用法 6.vector实战(这里写的是我在最开 ...

  3. 记一次Idea+Gradle编译Spring源码遇到的坑

    打算开始研究Spring源码,于是照着书上的指导从网上下载了Spring5.0.2版本和Gradle4.0(必须JDK1.8.60以上),本地编译的时候没有任何问题,就是导入Idea的时候一直报下面这 ...

  4. C++ 进阶 模板和STL

    C++提高编程 本阶段主要针对C++泛型编程和STL技术做详细讲解,探讨C++更深层的使用 1 模板 1.1 模板的概念 模板就是建立通用的模具,大大提高复用性 模板的特点: 模板不可以直接使用,它只 ...

  5. C#数据结构与算法系列(十):逆波兰计算器——逆波兰表达式(后缀表达式)

    1.介绍 后缀表达式又称逆波兰表达式,与前缀表达式相似,只是运算符位于操作数之后 2.举例说明 (3+4)*5-6对应的后缀表达式就是3 4 +5 * 6 - 3.示例 输入一个逆波兰表达式(后缀表达 ...

  6. Python3-随笔目录

      Python3-面向对象 标准库模块 Python3-collections模块-容器数据类型 Python3-datetime模块-日期与时间 Python3-re模块-正则表达式 Python ...

  7. 吉比特&雷霆游戏--2020春招实习

    笔试 题量较大,仅记了一些印象比较深刻的题. 题型为选择 + 填空(给C++代码填输出结果) + 编程 编程题不会太难,最难的就一道字符串的全排列(类似剑指offer第38题LeetCode链接)可以 ...

  8. Windows10系统下安装配置Tomcat 9.0.1

    Tomcat9.0.1下载:https://tomcat.apache.org/download-90.cgi 配置jdk的环境变量(略) 在系统变量里新建变量名:CATALINA_BASE,变量值: ...

  9. navicat连接vagrant中的数据库

  10. 第二部分用户交互程序开发,通过paramiko记录ssh会话记录

    需求及任务:实现一个给用户登录的界面(通过ssh登到堡垒机上,然后给它展现一个命令行的页面,然后他选择登哪台机器,一选择就连上去且把日志也记录下来). 先在admin创建几条组数据并与用户关联如下图: ...