Tensorflow的最佳实践
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的最佳实践的更多相关文章
- Tensorflow之MNIST的最佳实践思路总结
Tensorflow之MNIST的最佳实践思路总结 在上两篇文章中已经总结出了深层神经网络常用方法和Tensorflow的最佳实践所需要的知识点,如果对这些基础不熟悉,可以返回去看一下.在< ...
- 学习笔记TF061:分布式TensorFlow,分布式原理、最佳实践
分布式TensorFlow由高性能gRPC库底层技术支持.Martin Abadi.Ashish Agarwal.Paul Barham论文<TensorFlow:Large-Scale Mac ...
- EffectiveTensorflow:Tensorflow 教程和最佳实践
Tensorflow和其他数字计算库(如numpy)之间最明显的区别在于Tensorflow中的操作是符号. 这是一个强大的概念,允许Tensorflow进行所有类型的事情(例如自动区分),这些命令式 ...
- 【机器学习】Google机器学习工程的43条最佳实践
https://blog.csdn.net/ChenVast/article/details/81449509 本文档旨在帮助那些掌握机器学习基础知识的人从Google机器学习的最佳实践中获益.它提供 ...
- Kubernetes集群的监控报警策略最佳实践
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/M2l0ZgSsVc7r69eFdTj/article/details/79652064 本文为Kub ...
- (转载)PyTorch代码规范最佳实践和样式指南
A PyTorch Tools, best practices & Styleguide 中文版:PyTorch代码规范最佳实践和样式指南 This is not an official st ...
- Docker 使用杂记 - 最佳实践尝试 - 实战
目录 Docker 使用杂记 - 最佳实践尝试 - 实战 Docker简介 项目背景 内在原因 外在原因 基础镜像 需求 镜像维护者 工作文件夹 文件 ADD COPY 宗卷 命令 入口点 Docke ...
- ASP.NET跨平台最佳实践
前言 八年的坚持敌不过领导的固执,最终还是不得不阔别已经成为我第二语言的C#,转战Java阵营.有过短暂的失落和迷茫,但技术转型真的没有想象中那么难.回头审视,其实单从语言本身来看,C#确实比Java ...
- 《AngularJS深度剖析与最佳实践》简介
由于年末将至,前阵子一直忙于工作的事务,不得已暂停了微信订阅号的更新,我将会在后续的时间里尽快的继续为大家推送更多的博文.毕竟一个人的力量微薄,精力有限,希望大家能理解,仍然能一如既往的关注和支持sh ...
随机推荐
- HCNA多区域OSPF配置
1.拓扑图 2.各路由器配置角本 ospf 多区域配置 #R5配置 sys sysname AR5 interface s2// ip add 10.0.35.5 255.255.255.0 inte ...
- oracle_set_autocommit
preface 1.centos operating system. 2.database is oracle 11g. 3.oracle account is scott. step 1.e ...
- Python——追加学习笔记(三)
错误与异常 AttributeError:尝试访问未知的对象属性 eg. >>> class myClass(object): ... pass ... >>> m ...
- CentOS 7.3 下 Mysql(mariadb)的安装
LNMP的安装中 Nginx的安装很简单,我一般去Nginx官方网站上下载对应版本的rpm包后,上传到终端rpm安装.再此不多赘述. 但是在CentOS7中安装最新的mysql(mariadb)却经常 ...
- March 25 2017 Week 12 Saturday
Better master one than engage with ten. 会十事不如精一事. My colleagues think I have known a lot of things, ...
- Python3基本数据类型(五、字典)
一.定义 字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号中: dic = {key: ...
- ubuntu16.4 修改菜单到下方 错误:GLib-GIO-Message: Using the 'memory' GSettings backend. Your settings will not be saved or shared with other applications.
1.修改命令 #在终端输入 gsettings set com.canonical.Unity.Launcher launcher-position Bottom 2.如果遇错 GLib-GIO-Me ...
- codeforces 597C - Subsequences
枚举子序列的末尾,递推. 方案数:f[i = 以i结尾][k =子序列长度] = sum(f[j][k-1]),j < i. 转移就建立k个BIT. #include<bits/stdc+ ...
- What is Thread
A thread is a fundamental unit of CPU utilization –a thread ID –a program counter –a register set –a ...
- 【[SDOI2010]粟粟的书架】
第一问的做法好像不太一样 首先第二问非常简单,直接在主席树上二分就好了,单次查询的复杂度\(O(logn)\) 第一问并没有想到有二分这种神仙操作,依旧用的是主席树 我们可以对矩阵建出主席树,也就是像 ...