'''
Created on Apr 21, 2017 @author: P0079482
'''
#如何通过tf.variable_scope函数来控制tf.ger_variable函数获取已经创建过的变量
#在名字为foo的命名空间内创建名字为v的变量
import tensorflow as tf
with tf.variable_scope("foo"):
v = tf.get_variable("v",shape=[1],initializer=tf.constant_initializer(1.0))
#因为在命名空间foo中已经存在名为v的变量,所有下面的代码将会报错:
#Variable foo/v already exists,
with tf.variable_scope("foo"):
v = tf.get_variable("v",[1])
#在生成上下文管理器时,将参数reuse设置为True.这样tf.get_variable函数将直接获取已经声明的变量
with tf.variable_scope("foo",reuse=True):
v1 = tf.get_variable("v",[1])
print(v==v1) #输出为True,代表v,v1代表的是相同的Tensorflow中的变量
#将参数reuse设置为True是,tf.variable_scope将只能获取已经创建过的变量。
#因为在命名空间bar中还没有创建变量v,所以下面的代码将会报错
with tf.variable_scope("bar",reuse=True):
v = tf.get_variable("v",[1])
#如果tf.variable_scope函数使用reuse=None或者reuse=False创建上下文管理器
#tf.get_variable操作将创建新的变量。
#如果同名的变量已经存在,则tf.get_variable函数将报错
#Tensorflow中tf.variable_scope函数是可以嵌套的
with tf.variable_scope("root"):
#可以通过tf.get_variable_scope().reuse函数来获取上下文管理器中reuse参数的值
print(tf.get_variable_scope().reuse) #输出False,即最外层reuse是False with tf.variable_scope("foo",reuse=True): #新建一个嵌套的上下文管理器并指定reuse为True
print(tf.get_variable_scope().reuse) #输出True
with tf.variable_scope("bar"): #新建一个嵌套的上下文管理器,但不指定reuse,这时reuse的取值会和外面一层保持一致
print(tf.get_variable_scope().reuse) #输出True
print(tf.get_variable_scope().reuse) #输出False
#tf.variable_scope函数生成的上下文管理器也会创建一个Tensorflow中的命名空间
#在命名空间内创建的变量名称都会带上这个命名空间作为前缀
#所以tf.variable_scope函数除了可以控制tf.get_variable执行的功能之外
#这个函数也提供了一个管理命名空间的方式
v1 = tf.get_variable("v",[1])
print(v1.name)#输出v:0 "v"为变量的名称,":0"表示这个变量是生成变量这个运算的第一个结果
with tf.variable_scope("foo"):
v2 = tf.get_variable("v",[1])
print(v2.name)#输出foo/v:0 在tf.variable_scope中创建的变量,名称前面会
#加入命名空间的名称,并通过/来分隔命名空间的名称和变量的名称
with tf.variable_scope("foo"):
with tf.variable_scope("bar"):
v3 = tf.get_variable("v",[1])
print(v3.name) #输出foo/bar/v:0 命名空间可以嵌套,同时变量的名称也会加入所有命名空间的名称作为前缀 v4 = tf.get_variable("v1",[1])
print(v4.name) #输出foo/v1:0 当命名空间退出之后,变量名称也就不会再被加入其前缀了
#创建一个名称为空的命名空间,并设置reuse=True
with tf.variable_scope("",reuse=True):
v5=tf.get_variable("foo/bar/v",[1])#可以直接通过带命名空间名称的变量名来获取其他命名空间下的变量。 print(v5==v3)
v6=tf.get_variable("foo/v1",[1])
print(v6==v4)
#通过tf.variable_scope和tf.get_variable函数,以下代码对inference函数的前向传播结果做了一些改进
def inference(input_tensor,reuse=False):
#定义第一层神经网络的变量和前向传播过程
with tf.variable_scope('layer1',reuse=reuse):
#根据传进来的reuse来判断是创建新变量还是使用已经创建好了。在第一次构造网络时需要创建新的变量,
#以后每次调用这个函数都直接使用reuse=True就不需要每次将变量传进来了
weights= tf.get_variable("weights",[INPUT_NODE,LAYER1_NODE],initializer=tf.truncated_normal_initializer(stddev=0.1))
biases= tf.get_variable("biases",[LAYER1_NODE],initializer=tf.constant_initializer(0.0))
layer1 = tf.nn.relu(tf.matmul(input_tensor,weights)+biases) #类似地定义第二层神经网络的变量和前向传播过程
with tf.variable_scope('layer2',reuse=reuse):
weights=tf.get_variable("weights",[LAYER1_NODE,OUTPUT_NODE],initializer=tf.truncated_normal_initializer(stddev=0.1))
biases=tf.get_variable("biases",[OUTPUT_NODE],initializer=tf.constant_initializer(0.0))
layer2=tf.matmul(layer1,weights)+biases
#返回最后的前向传播结果
return layer2 x=tf.placeholder(tf.float32,[None,INPUT_NODE],name='x-input')
y=inference(x) #在程序中需要使用训练好的神经网络进行推倒时,可以直接调用inference(new_x,True)

83、Tensorflow中的变量管理的更多相关文章

  1. TensorFlow中的变量和常量

    1.TensorFlow中的变量和常量介绍 TensorFlow中的变量: import tensorflow as tf state = tf.Variable(0,name='counter') ...

  2. 2、Tensorflow中的变量

    2.Tensorflow中的变量注意:tf中使用 变量必须先初始化下面是一个使用变量的TF代码(含注释): # __author__ = "WSX" import tensorfl ...

  3. 深度学习原理与框架-Tensorflow基本操作-Tensorflow中的变量

    1.tf.Variable([[1, 2]])  # 创建一个变量 参数说明:[[1, 2]] 表示输入的数据,为一行二列的数据 2.tf.global_variables_initializer() ...

  4. TensorFlow中的变量命名以及命名空间.

    What: 在Tensorflow中, 为了区别不同的变量(例如TensorBoard显示中), 会需要命名空间对不同的变量进行命名. 其中常用的两个函数为: tf.variable_scope, t ...

  5. Tensorflow中的变量

    从初识tf开始,变量这个名词就一直都很重要,因为深度模型往往所要获得的就是通过参数和函数对某一或某些具体事物的抽象表达.而那些未知的数据需要通过学习而获得,在学习的过程中它们不断变化着,最终收敛达到较 ...

  6. tensorflow中使用变量作用域及tf.variable(),tf,getvariable()与tf.variable_scope()的用法

    一 .tf.variable() 在模型中每次调用都会重建变量,使其存储相同变量而消耗内存,如: def repeat_value(): weight=tf.variable(tf.random_no ...

  7. tensorflow中常量(constant)、变量(Variable)、占位符(placeholder)和张量类型转换reshape()

    常量 constant tf.constant()函数定义: def constant(value, dtype=None, shape=None, name="Const", v ...

  8. tensorflow中张量_常量_变量_占位符

    1.tensor 在tensorflow中,数据是被封装在tensor对象中的.tensor是张量的意思,即包含从0到任意维度的张量.常数是0维度的张量,向量是1维度的张量,矩阵是二维度的张量,以及还 ...

  9. tensorflow中slim模块api介绍

    tensorflow中slim模块api介绍 翻译 2017年08月29日 20:13:35   http://blog.csdn.net/guvcolie/article/details/77686 ...

随机推荐

  1. Sublime Text 3 快捷键总结(Mac)

    Command + Shift + L 光标同时定位多行 Command + Enter 在下一行插入新行.举个栗子:即使光标不在行尾,也能快速向下插入一行.

  2. xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance

    运行xcode命令报错: sh-3.2# xcodebuild xcode-select: error: tool 'xcodebuild' requires Xcode, but active de ...

  3. mybatis 查询一对一

    官方文档 Mapper接口 public interface UserMapper { User getUser(int userId); } public interface ArticleMapp ...

  4. MongoDB拥有SSD秒杀高富帅使用过程分享

    [IT168现场报道]2013年4月18-20日,第四届中国数据库技术大会(DTCC 2013)在北京福朋喜来登酒店拉开序幕.在为期三天的会议中,大会将围绕大数据应用.数据架构.数据管理(数据治理). ...

  5. 基于spark邮件自动分类

    代码放在github上:click me 一.数据说明 数据集为英文语料集,一共包含20种类别的邮件,除了类别soc.religion.christian的邮件数为997以外每个类别的邮件数都是100 ...

  6. nginx 设置http访问ftp目录内文件

    设置 nginx “403 Forbidden” 错误的原因及解决办法 原文链接 https://www.cnblogs.com/chenzc/p/4461130.html nginx 的 403 F ...

  7. IE兼容模式下样式分离错乱,求CSS高手

    IE正常模式下访问正常 兼容模式右边图片切换区域样式错乱,求CSS高手! 详细参考网址:www.javams.com

  8. C++大整数类模板

    参考 :http://172.21.85.56/oj/resource/reportdetail?report_id=1678 支持 =.abs().pow().+=.-= *=./=.%=.+.-. ...

  9. fieldset与legend,label

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 【目录】sql server 架构篇系列

    随笔分类 - sql server 架构篇系列 sql server 高可用镜像 摘要: 一.什么是数据库镜像 基本软件的高可用性解决方案 快速的故障转移恢复(3秒转移),低硬件成本 基于数据库级别的 ...