'''
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. 109、TensorFlow计算张量的值

    # 当计算图创建成功时 # 你就可以运行这个计算图,然后生成一个新的张量 # 并且得到这个张量指向的计算图中具体的数值 #这个功能在debug的时候非常有必要 #最简单获得张量具体值的方法是使用Ten ...

  2. nlp学习笔记

    https://mp.weixin.qq.com/s/-w4gENfBt2gKOPvghenw9w

  3. zabbix4.0部署

    1.环境检查 uname -r getenforce systemctl status firewalld.service 2.设置解析,自建yum源(可选) /etc/hosts #!/bin/ba ...

  4. HDU 5183 Negative and Positive (NP) (手写哈希)

    题目链接:HDU 5183 Problem Description When given an array \((a_0,a_1,a_2,⋯a_{n−1})\) and an integer \(K\ ...

  5. 记录js中的兼容问题及解决办法

    1.获取非行内样式的兼容问题: 2.获取事件对象的兼容问题: 3.事件冒泡的兼容: 4.keyCode的兼容问题: 5.处理默认事件的兼容问题: 6.事件的绑定兼容问题:

  6. QTP read or write XML file

    'strNodePath = "/soapenv:Envelope/soapenv:Body/getProductsResponse/transaction/queryProducts/qu ...

  7. UVA1608_Non-boring sequences

    Non-boring sequences 大致题意: 给你一个字符串,问你他的任一子串是否都包含一个唯一的字符 思路: 看似简单,实际一丁点思路都没有 后面看汝佳的讲解都看了好长时间 大概思路就是,先 ...

  8. log4j/slf4j

    log4j的使用 引入log4j.jar包 <dependency> <groupId>log4j</groupId> <artifactId>log4 ...

  9. shell编程:sed的选项

    sed [参数] [partern/commond] file 标准输出 | sed sed [参数] [partern/commond] -n :使用安静(silent)模式.在一般 sed 的用法 ...

  10. 发送xml数据