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

    I am using JsonPath and am able to parse my data and get the values when the path provided is correc ...

  2. 服务端:WCF服务层安全检查核心类

    using System.Data; using CSFrameworkV4_5.Common; using CSFrameworkV4_5.Core.SystemSecurity; using CS ...

  3. PHP将mysql数据表转换为excel文件

    测试代码: <?php $DB_Server = "127.0.0.1"; $DB_Username = "root"; $DB_Password = & ...

  4. 抓包工具fiddler下载配置(一):下载/安装&信任证书

    简介 Fiddler一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网之间的http通讯,设置断点,查看所有的“进出”Fiddler的数据(指cookie,html,js,css等文件 ...

  5. IDEA-Tomcat 运行报错

    我的问题是SDK版本不一致

  6. spring data jpa 多对多查询

    package com.ytkj.dao; import com.ytkj.entity.Customer; import com.ytkj.entity.Role; import org.sprin ...

  7. AttributeError: module 'requests' has no attribute 'get'的错误疑惑

    我发现文件直接用requests.get(url)会提示我AttributeError: module 'requests' has no attribute 'get' 我把问题百度了一下,解决方法 ...

  8. redis缓存架构-02-两种持久化机制(RDB和AOF)

    1.两种持久化机制的介绍 1.1 RDB 周期性的生成redis内存数据的一份完整的快照 1)根据配置的检查点,生产rdb快照文件,fork一个子线程,将数据dump到rdb快照文件中,完成rdb文件 ...

  9. js 模板引擎 -Art Template

    一个例子涵盖所有: <!doctype html> <html> <head> <meta charset="UTF-8"> < ...

  10. Codeforces 1203F (贪心, DP)

    题意:有n个任务,你的初始rating是m, 这n个任务有两个指标:完成这项任务所需的最低rating(a[i]),以及完成这项任务后rating的变化(可能为负)(b[i]).rating不能为负. ...