Tensorflow的最佳实践

1、变量管理

  Tensorflow提供了变量管理机制,可直接通过变量的名字获取变量,无需通过传参数传递数据。方式如下:

#以下为两种创建变量的方法
v=tf.get_variable("v",shape=[1],initializer=tf.constant_initializer(1.0))#变量名必填
v=tf.Variable(tf.constant(1.0,shape=[1]),name="v")#变量名可选
#7种不同的初始化函数
tf.constant_initializer#将变量初始化给定常亮 参数:常量的取值
tf.random_normal_initializer#将变量初始化给满足正态分布的随机值 参数:正太分布的均值和标准差
tf.truncated_normal_initializer#将变量初始化为满足正太分布的随机值,但若随机出来的值偏离均值超过2个标准差,那将重新随机 参数:正太分布的均值和标准差
tf.random_uniform_initializer#将变量初始化为满足平均分布的随机值 参数:最大值最小值
tf.uniform_unit_scaling_initializer#将变量初始化为满足平均分布的随机值,但不影响输出数量级的随机值 参数:factor产生随机值时乘以的系数
tf.zeros_initializer#全为0 参数:变量维度
tf.ones_initializer#全为1 参数:变量维度
#在名字为foo的命名空间内创建名字为v的变量
with tf.variable_scope("foo"):
v=tf.get_variable("v",[1],initializer=tf.constant_initializer(1.0)) #reuse设为True后,只能获取已经创建的变量
with tf.variable_scope("foo",reuse=True):
v=tf.get_variable("v",[1])
print(v) #总结:当参数reuse为True时,上下文管理器中的get_variable函数只能获取已经创建过的变量,反之,只能创建新变量若同名,则报错
#从下面例子可以看出,如果reuse为True的上下文管理器中的其他管理器的reuse一概为True,反之,其他管理器为True,则为Ture,为False,则为False,以此类推
with tf.variable_scope("foo"):
print(tf.get_variable_scope().reuse)
with tf.variable_scope('root',reuse=True):
print(tf.get_variable_scope().reuse)
with tf.variable_scope('bar'):
print(tf.get_variable_scope().reuse)
with tf.variable_scope('bar1'):
print(tf.get_variable_scope().reuse)
print(tf.get_variable_scope().reuse)
print(tf.get_variable_scope().reuse) #在命名空间内创建的变量的名称都会带上这个命名空间名做前缀
v1 = tf.get_variable("v", [1])
print(v1.name) with tf.variable_scope("foo",reuse=True):
v2 = tf.get_variable("v", [1])
print(v2.name) with tf.variable_scope("foo"):
with tf.variable_scope("bar"):
v3 = tf.get_variable("v", [1])
print(v3.name) v4 = tf.get_variable("v1", [1])
print(v4.name) #通过变量的名称来获取变量
with tf.variable_scope("",reuse=True):
v5 = tf.get_variable("foo/bar/v", [1])
print(v5 == v3)
v6 = tf.get_variable("v1", [1])
print(v6 == v4)

2、模型持久化代码实现

1、ckpt文件保存方法

1.保存模型:使用tf.train.Saver(),具体使用如下:

v1 = tf.Variable(tf.constant(1.0, shape=[1]), name = "v1")
v2 = tf.Variable(tf.constant(2.0, shape=[1]), name = "v2")
result = v1 + v2
init_op = tf.global_variables_initializer()
saver = tf.train.Saver()#获得API
with tf.Session() as sess:
sess.run(init_op)
saver.save(sess, "Saved_model/model.ckpt")#保存

注意:以上代码将Tensorflow模型保存至ckpt文件中,虽然该方法只指定一个路径,但是却创建了3个文件,分别是model.ckpt.meta,保存了计算图的结构,第二个文件是model.ckpt,该文件保存了每个变量的取值,最后一个文件是checkpoint,保存了一个目录下所有模型文件列表

2.加载模型:使用saver.restore(sess, "Saved_model/model.ckpt"),具体使用如下;

saver=tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess, "Saved_model/model.ckpt")
print sess.run(result)

3.直接加载持久化的图:tf.train.import_meta_graph("Saved_model/model.ckpt.meta"),具体使用如下:

saver = tf.train.import_meta_graph("Saved_model/model.ckpt.meta")
with tf.Session() as sess:
saver.restore(sess, "Saved_model/model.ckpt")
print sess.run(tf.get_default_graph().get_tensor_by_name("add:0"))

3.变量重命名,具体使用如下:

v1 = tf.Variable(tf.constant(1.0, shape=[1]), name = "other-v1")
v2 = tf.Variable(tf.constant(2.0, shape=[1]), name = "other-v2")
saver = tf.train.Saver({"v1": v1, "v2": v2})

2、滑动平均类的保存

1.使用滑动平均:

v = tf.Variable(0, dtype=tf.float32, name="v")
for variables in tf.global_variables(): print variables.name
ema = tf.train.ExponentialMovingAverage(0.99)
maintain_averages_op = ema.apply(tf.global_variables())
for variables in tf.global_variables(): print variables.name

2.保存滑动平均模型

saver = tf.train.Saver()
with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
sess.run(tf.assign(v, 10))
sess.run(maintain_averages_op)
# 保存的时候会将v:0  v/ExponentialMovingAverage:0这两个变量都存下来。
saver.save(sess, "Saved_model/model2.ckpt")
print sess.run([v, ema.average(v)])

3.加载滑动平均模型

v = tf.Variable(0, dtype=tf.float32, name="v")
# 通过变量重命名将原来变量v的滑动平均值直接赋值给v。
saver = tf.train.Saver({"v/ExponentialMovingAverage": v})
with tf.Session() as sess:
saver.restore(sess, "Saved_model/model2.ckpt")
print sess.run(v)

4.variables_to_restore函数的使用样例 (unsaved changes)

import tensorflow as tf
v = tf.Variable(0, dtype=tf.float32, name="v")
ema = tf.train.ExponentialMovingAverage(0.99)
print ema.variables_to_restore()
saver = tf.train.Saver(ema.variables_to_restore())
with tf.Session() as sess:
saver.restore(sess, "Saved_model/model2.ckpt")
print sess.run(v)

3、pb文件保存方法 (unsaved changes)

  当不需要存储全部信息时,可使用如下方法将计算图中的变量及其取值通过常量的方式保存。

1.保存:

import tensorflow as tf
from tensorflow.python.framework import graph_util v1 = tf.Variable(tf.constant(1.0, shape=[1]), name = "v1")
v2 = tf.Variable(tf.constant(2.0, shape=[1]), name = "v2")
result = v1 + v2 init_op = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
graph_def = tf.get_default_graph().as_graph_def()#导出当前图的GraphDef部分即可完成从输入层到输出层的计算过程
output_graph_def = graph_util.convert_variables_to_constants(sess, graph_def, ['add'])
with tf.gfile.GFile("Saved_model/combined_model.pb", "wb") as f:
f.write(output_graph_def.SerializeToString())

2.加载:

from tensorflow.python.platform import gfile
with tf.Session() as sess:
model_filename = "Saved_model/combined_model.pb"
#读取保存的模型文件
with gfile.FastGFile(model_filename, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
#将保存的图读取到当前图中,return_elements表示返回的张量的名称,张量名称为节点名称后面加上(:0)
result = tf.import_graph_def(graph_def, return_elements=["add:0"])
print sess.run(result)

3、持久化数据格式

  在文件model.ckpt.meta中存储了元图的数据,但该文件是二进制的,无法直接查看,TensorFlow有将其以json格式导出的方法:

v1 = tf.Variable(tf.constant(1.0, shape=[1]), name = "v1")
v2 = tf.Variable(tf.constant(2.0, shape=[1]), name = "v2")
result = v1 + v2
saver = tf.train.Saver()
saver.export_meta_graph("Saved_model/model.ckpt.meta.json",as_text=True)#导出元图并以json格式输出

  查看保存的变量信息:

reader=tf.train.NewCheckpointReader('Saved_model/model.ckpt')#获取所有变量列表
all_variable=reader.get_variable_to_shape_map()
for variable_name in all_variable:
print(variable_name,all_variable[variable_name])
print(1,reader.get_tensor('v1'))

Tensorflow的最佳实践的更多相关文章

  1. Tensorflow之MNIST的最佳实践思路总结

    Tensorflow之MNIST的最佳实践思路总结   在上两篇文章中已经总结出了深层神经网络常用方法和Tensorflow的最佳实践所需要的知识点,如果对这些基础不熟悉,可以返回去看一下.在< ...

  2. 学习笔记TF061:分布式TensorFlow,分布式原理、最佳实践

    分布式TensorFlow由高性能gRPC库底层技术支持.Martin Abadi.Ashish Agarwal.Paul Barham论文<TensorFlow:Large-Scale Mac ...

  3. EffectiveTensorflow:Tensorflow 教程和最佳实践

    Tensorflow和其他数字计算库(如numpy)之间最明显的区别在于Tensorflow中的操作是符号. 这是一个强大的概念,允许Tensorflow进行所有类型的事情(例如自动区分),这些命令式 ...

  4. 【机器学习】Google机器学习工程的43条最佳实践

    https://blog.csdn.net/ChenVast/article/details/81449509 本文档旨在帮助那些掌握机器学习基础知识的人从Google机器学习的最佳实践中获益.它提供 ...

  5. Kubernetes集群的监控报警策略最佳实践

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/M2l0ZgSsVc7r69eFdTj/article/details/79652064 本文为Kub ...

  6. (转载)PyTorch代码规范最佳实践和样式指南

    A PyTorch Tools, best practices & Styleguide 中文版:PyTorch代码规范最佳实践和样式指南 This is not an official st ...

  7. Docker 使用杂记 - 最佳实践尝试 - 实战

    目录 Docker 使用杂记 - 最佳实践尝试 - 实战 Docker简介 项目背景 内在原因 外在原因 基础镜像 需求 镜像维护者 工作文件夹 文件 ADD COPY 宗卷 命令 入口点 Docke ...

  8. ASP.NET跨平台最佳实践

    前言 八年的坚持敌不过领导的固执,最终还是不得不阔别已经成为我第二语言的C#,转战Java阵营.有过短暂的失落和迷茫,但技术转型真的没有想象中那么难.回头审视,其实单从语言本身来看,C#确实比Java ...

  9. 《AngularJS深度剖析与最佳实践》简介

    由于年末将至,前阵子一直忙于工作的事务,不得已暂停了微信订阅号的更新,我将会在后续的时间里尽快的继续为大家推送更多的博文.毕竟一个人的力量微薄,精力有限,希望大家能理解,仍然能一如既往的关注和支持sh ...

随机推荐

  1. March 25 2017 Week 12 Saturday

    Better master one than engage with ten. 会十事不如精一事. My colleagues think I have known a lot of things, ...

  2. 初识Git与Github

    学习和使用Git和Github的确是一件很有意义的事,通过使用Git和Github,可以让我们很方便地管理自己的各种文件,还可以帮助一名程序员更好地用于代码管理.而对于一名软件技术人员,建立自己的Gi ...

  3. ABAP和Java里关于DEFAULT(默认)机制的一些语言特性

    ABAP 740的新语法: 上图的代码相当于: DATA: ls_data LIKE LINE OF it_data. READ TABLE it_data INTO ls_data WITH KEY ...

  4. POJ 3694 无向图的桥

    Network Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 10404   Accepted: 3873 Descript ...

  5. POJ 3067 Japan 【树状数组经典】

    题目链接:POJ 3067 Japan Japan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32076   Accep ...

  6. Zookeep启动异常:Error contacting service. It is probably not running.

    异常提示: [root@hadoop bin]# ./zkServer.sh status JMX enabled by default Using config: /usr/local/zk/bin ...

  7. sqlite简单笔记

    五种约束需要注意的地方 1.自增使用:autoincrement 2.约束必须进行后面处理:unique(字段[多个可以以逗号分开]) 3.外键必须放后面:foreign key(字段引用) refe ...

  8. 一步一步部署SSIS包图解教程

    本文就SQL统计分析SSIS包的部署进行一次详细的部署图解教程,Sql Server Integration Services 提供了非常简单的部署工具,利用这些工具可以方便地将包文件(*.dtsx) ...

  9. 读取静态的json文件

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  10. vue进阶语法及生命周期函数

    1.calss和style绑定 操作元素的 class 列表和内联样式style是数据绑定的一个常见需求,它两都是属性,所以可以通过v-bind来绑定 1.1绑定HTML class 可以给v-bin ...