Tesnsorflow命名空间与变量管理参数reuse
一.TensorFlow中变量管理reuse参数的使用
1.TensorFlow用于变量管理的函数主要有两个:
(1)tf.get_variable:用于创建或获取变量的值
(2)tf.variable_scope():用于生成上下文管理器,创建命名空间,命名空间可以嵌套
2.函数tf.get_variable()既可以创建变量也可以获取变量。控制创建还是获取的开关来自函数tf.variable.scope()中的参数reuse为“True”还是"False",分两种情况进行说明:
(1)设置reuse=False时,函数get_variable()表示创建变量
with tf.variable_scope("foo",reuse=False):
v=tf.get_variable("v",[],initializer=tf.constant_initializer(1.0))
#在tf.variable_scope()函数中,设置reuse=False时,在其命名空间"foo"中执行函数get_variable()时,表示创建变量"v"
(2)若在该命名空间中已经有了变量"v",则在创建时会报错,如下面的例子
import tensorflow as tf
with tf.variable_scope("foo"):
v=tf.get_variable("v",[],initializer=tf.constant_initializer(1.0))
v1=tf.get_variable("v",[])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input--eaed46cad84f> in <module>()
with tf.variable_scope("foo"):
v=tf.get_variable("v",[],initializer=tf.constant_initializer(1.0))
----> v1=tf.get_variable("v",[])
ValueError: Variable foo/v already exists, disallowed.
Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope?
(3)设置reuse=True时,函数get_variable()表示获取变量
import tensorflow as tf
with tf.variable_scope("foo"):
v=tf.get_variable("v",[],initializer=tf.constant_initializer(1.0))
with tf.variable_scope("foo",reuse=True):
v1=tf.get_variable("v",[])
print(v1==v)
运行结果为:
True
(4)在tf.variable_scope()函数中,设置reuse=True时,在其命名空间"foo"中执行函数get_variable()时,表示获取变量"v"。若在该命名空间中还没有该变量,则在获取时会报错,如下面的例子
import tensorflow as tf
with tf.variable_scope("foo",reuse=True):
v1=tf.get_variable("v",[])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input--019a05c4b9a4> in <module>()
with tf.variable_scope("foo",reuse=True):
----> v1=tf.get_variable("v",[])
ValueError: Variable foo/v does not exist, or was not created with tf.get_variable().
Did you mean to set reuse=tf.AUTO_REUSE in VarScope?
二.Tensorflow中命名空间与变量命名问题
1. tf.Variable:创建变量;自动检测命名冲突并且处理;
import tensorflow as tf
a1 = tf.Variable(tf.constant(1.0, shape=[]),name="a")
a2 = tf.Variable(tf.constant(1.0, shape=[]),name="a")
print(a1) #创建变量,命名为a
print(a2)#自动检测命名冲突并且处理,命名为a_1
print(a1==a2)
运行结果:
<tf.Variable 'a:0' shape=(,) dtype=float32_ref>
<tf.Variable 'a_1:0' shape=(,) dtype=float32_ref>
False
2. tf.get_variable创建与获取变量;在没有设置命名空间reuse的情况下变量命名冲突时报错
import tensorflow as tf
a3 = tf.get_variable("a", shape=[], initializer=tf.constant_initializer(1.0))
a4 = tf.get_variable("a", shape=[], initializer=tf.constant_initializer(1.0)) 运行结果:
ValueError: Variable a already exists, disallowed.
Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope?
3.tf.name_scope没有reuse功能,tf.get_variable命名不受它影响,并且命名冲突时报错;tf.Variable命名受它影响
import tensorflow as tf
a = tf.Variable(tf.constant(1.0, shape=[1]),name="a")
with tf.name_scope('layer2'):
a1 = tf.Variable(tf.constant(1.0, shape=[1]),name="a")
a2 = tf.Variable(tf.constant(1.0, shape=[1]),name="a")
a3 = tf.get_variable("b", shape=[1], initializer=tf.constant_initializer(1.0))
# a4 = tf.get_variable("b", shape=[1], initializer=tf.constant_initializer(1.0)) 该句会报错
print(a)
print(a1)
print(a2)
print(a3)
print(a1==a2)
运行结果:
<tf.Variable 'a_2:0' shape=(1,) dtype=float32_ref>
<tf.Variable 'layer2_1/a:0' shape=(1,) dtype=float32_ref>
<tf.Variable 'layer2_1/a_1:0' shape=(1,) dtype=float32_ref>
<tf.Variable 'b:0' shape=(1,) dtype=float32_ref>
False
4.tf.variable_scope可以配tf.get_variable实现变量共享;reuse默认为None,有False/True/tf.AUTO_REUSE可选:
- 设置reuse = None/False时tf.get_variable创建新变量,变量存在则报错
- 设置reuse = True时tf.get_variable只获取已存在的变量,变量不存在时报错
- 设置reuse = tf.AUTO_REUSE时tf.get_variable在变量已存在则自动复用,不存在则创建(!!!我的tensorflow好像不能用,报错说找不到这个模块)
(1) reuse=True的例子:
import tensorflow as tf
with tf.variable_scope('layer1'):
a3 = tf.get_variable("b", shape=[], initializer=tf.constant_initializer(1.0))
with tf.variable_scope('layer1',reuse=True):
a1 = tf.Variable(tf.constant(1.0, shape=[]),name="a")
a2 = tf.Variable(tf.constant(1.0, shape=[]),name="a")
a4 = tf.get_variable("b", shape=[], initializer=tf.constant_initializer(1.0))
print(a1)
print(a2)
print(a1==a2)
print()
print(a3)
print(a4)
print(a3==a4)
运行结果:
<tf.Variable 'layer1_1/a:0' shape=(,) dtype=float32_ref>
<tf.Variable 'layer1_1/a_1:0' shape=(,) dtype=float32_ref>
False
<tf.Variable 'layer1/b:0' shape=(,) dtype=float32_ref>
<tf.Variable 'layer1/b:0' shape=(,) dtype=float32_ref>
True
(2) reuse=None/False的例子:
import tensorflow as tf
with tf.variable_scope('layer1'):
a3 = tf.get_variable("b", shape=[], initializer=tf.constant_initializer(1.0))
with tf.variable_scope('layer1'): #reuse默认为None
a1 = tf.Variable(tf.constant(1.0, shape=[]),name="a")
a2 = tf.Variable(tf.constant(1.0, shape=[]),name="a")
a4 = tf.get_variable("b", shape=[], initializer=tf.constant_initializer(1.0)) #a4创建新变量b(而b已经存在了,a3已经创建),报错
print(a1)
print(a2)
print(a1==a2)
print()
print(a3)
print(a4)
print(a3==a4)

参考博客:
https://blog.csdn.net/johnboat/article/details/84846628
https://www.cnblogs.com/jfl-xx/p/9885662.html
Tesnsorflow命名空间与变量管理参数reuse的更多相关文章
- 83、Tensorflow中的变量管理
''' Created on Apr 21, 2017 @author: P0079482 ''' #如何通过tf.variable_scope函数来控制tf.ger_variable函数获取已经创建 ...
- Tensorflow 之 name/variable_scope 变量管理
name/variable_scope 的作用 充分理解 name / variable_scope TensorFlow 入门笔记 当一个神经网络比较复杂.参数比较多时,就比较需要一个比较好的方式来 ...
- 集成direnv 与docker-compose 进行环境变量管理
direnv 是一个不错的换将变量管理工具,同时日常的开发测试中我们使用docker-compose 会比较多,一般我们的玩法是 可以再docker-compose 中指定环境变量,可以通过envir ...
- tensorflow中命名空间、变量命名的问题
1.简介 对比分析tf.Variable / tf.get_variable | tf.name_scope / tf.variable_scope的异同 2.说明 tf.Variable创建变量:t ...
- Ansible_变量管理与设置
一.Ansible变量管理 1.变量概述 Ansible支持利用变量来存储值,并在Ansible项目的所有文件中重复使用这些值.这可以简化项目的创建和维护,并减少错误的数量 通过变量,可以轻松地在An ...
- 15SpringMvc_在业务控制方法中写入模型变量收集参数,且使用@InitBind来解决字符串转日期类型
之前第12篇文章中提到过在业务控制方法中写入普通变量收集参数的方式,也提到了这种凡方式的弊端(参数很多怎么办),所以这篇文章讲的是在业务控制方法中写入模型变量来收集参数.本文的案例实现的功能是,在注册 ...
- direnv 一个强大的环境变量管理工具
direnv 是一个基于golang 编写的强大的环境变量管理工具,可以帮助我们简化环境变量管理,而且 支持的平台比较多. 基本使用 下载二进制软件包 https://github.com/dir ...
- SSIS 变量、参数和表达式
动态包对象包括变量,参数和表达式.变量主要为包提供一种对象之间相互通信的方法,变量的值是可以更新的.而参数不同于变量,参数的值在包中是不能修改的,只能通过外部来设置参数.表达式可以引用变量.参数.常量 ...
- WF4.0(3)----变量与参数
已经写了两篇关于WF4.0的博客,算是基础博客,如果是WF比较熟悉就直接跳过吧,如果你对工作流不是很熟悉,或者想了解一下基础的东西,本文还是比较适合你的.工作流中变量,参数,表达式属于数据模型中概念, ...
随机推荐
- 多任务3(协程)--yield完成多任务交替执行
协程是并发,单线程,一次执行一个 来回切换 代码: import time def task_1(): while True: print("-----1-----") time. ...
- 根据参数显示类别(三级联动,需要JSON数据)
根据参数显示类别(三级联动,需要JSON数据) Scripts/Category.js 调用方法: $(function () { BindCategory(); //默认绑定文本框中的值 BindC ...
- selenium之python源码解读-expected_conditions
一.expected_conditions 之前在 selenium之python源码解读-WebDriverWait 中说到,until方法中method参数,需要传入一个function对象,如果 ...
- 51nod 1565 模糊搜索 FFT
这...好强啊\(QwQ\) 思路:卷积?\(FFT\)? 提交:\(5\)次 错因:一开始的预处理写错了(竟然只错了最后几个大点)闹得我以为\(FFT\)写挂了\(QwQ\) 题解: 对四种字符分开 ...
- jQuery的ajax()方法提交数组问题
http://blog.csdn.net/thc1987/article/details/7278269 解决办法是添加一个属性 traditional:true $.ajax({ type: ...
- BZOJ 2038: [2009国家集训队]小Z的袜子
二次联通门 : BZOJ 2038: [2009国家集训队]小Z的袜子 /* BZOJ 2038: [2009国家集训队]小Z的袜子 莫队经典题 但是我并不认为此题适合入门.. Answer = ∑ ...
- opencv 学习一安装环境vs2015+opencv3
参考博客 http://www.cnblogs.com/skyfsm/p/6840202.html 针对 模块计算机类型“X64”与目标计算机类型“X86”这个问题,我使用cmake 对环境的工程进行 ...
- Luogu5072 [Ynoi2015]盼君勿忘 【莫队】
题目描述:对于一个长度为\(n\)的序列,\(m\)次询问\(l,r,p\),计算\([l,r]\)的所有子序列的不同数之和\(\mathrm{mod} \ p\). 数据范围:\(n,m,a_i\l ...
- SDN与IXP
IXP 互联网交换中心(IXP)在互联网生态系统中发挥着关键作用.在全球范围内,100多个国家/地区有超过400个IXP,其中最大的IXP具有接近10 Tbps的峰值数据速率并连接数百个网络.IXP提 ...
- LG5492 [PKUWC2018]随机算法
题意 有一种贪心求最大独立集的算法: 随机一个排列 按顺序加入独立集,如果一个点能加入,就加入\({S}\) 给出一张图,问得出正确答案的概率. \(n \leq 20\) 传送门 思路 用 \(dp ...