TF.VARIABLE、TF.GET_VARIABLE、TF.VARIABLE_SCOPE以及TF.NAME_SCOPE关系
1. tf.Variable与tf.get_variable
tensorflow提供了通过变量名称来创建或者获取一个变量的机制。通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要将变量通过参数的形式到处传递。
TensorFlow中通过变量名获取变量的机制主要是通过tf.get_variable和tf.variable_scope实现的。
当然,变量也可以通过tf.Varivale来创建。当tf.get_variable用于变量创建时,和tf.Variable的功能基本等价。
|
1
2
3
|
#以下两个定义是等价的v = tf.get_variable('v', shape=[1], initializer=tf.constant_initializer(1.0))v = tf.Variable(tf.constant(1.0, shape=[1], name='v') |
tf.get_varialbe和tf.Variable最大的区别在于:tf.Variable的变量名是一个可选项,通过name=’v’的形式给出。但是tf.get_variable必须指定变量名。
2. tf.get_variable与tf.variable_scope
上面已经提到过了:TensorFlow中通过变量名获取变量的机制主要是通过tf.get_variable和tf.variable_scope实现的。在这里,我主要解释下大家深恶痛绝的reuse问题。
其实只要记住一件事情就ok了:当reuse为False或者None时(这也是默认值),同一个tf.variable_scope下面的变量名不能相同;当reuse为True时,tf.variable_scope只能获取已经创建过的变量。
下面我们通过代码来看下:
|
1
2
3
4
5
6
|
#reuse=False时会报错的情况:with tf.variable_scope('foo'): v = tf.get_variable('v',[1],initializer=tf.constant_initializer(1.0))with tf.variable_scope('foo'): v1 = tf.get_variable('v',[1]) |
在这种情况下会报错:Variable foo/v already exists, disallowed.Did you mean to set reuse=True in Varscope?
其原因就是在命名空间foo中创建了相同的变量。如果我要在foo下创建一个变量v1,其name=‘v’,只需要将reuse设置为Ture就ok了。将上面第二部分代码修改为:
|
1
2
3
|
with tf.variable_scope('foo', reuse=True): v1 = tf.get_variable('v',[1]) print(v1.name) #结果为foo/v |
当reuse已经设置为True时,tf.variable_scope只能获取已经创建过的变量。这个时候,在命名空间bar中创建name=‘v’的变量v3,将会报错:Variable bar/v dose not exists, diallowed. Did you mean to set reuse=None in VarScope?
|
1
2
|
with tf.variable_scope('bar', reuse=True): v3 = tf.get_variable('v',[1]) |
简而言之,reuse=False时,tf.variable_scope创建变量;reuse=True时,tf.variable_scope获取变量。
3. tf.variable_scope与tf.name_scope
除了tf.variable_scope,tf.name_scope函数也提供了命名空间管理的功能。这两个函数在大部分情况下是等价的,唯一的区别是在使用tf.get_variable函数时。
tf.get_variable函数不受tf.name_scope的影响。
我们从代码看下这句话的具体意思。
首先是tf.variable_scope:
|
1
2
3
|
with tf.variable_scope('foo'): a = tf.get_variable('bar',[1]) print(a.name)#结果为foo/bar:0 |
再看tf.name_scope:
|
1
2
3
4
5
6
|
with tf.name_scope('a'): a=tf.Variable([1]) print(a.name)#结果为a/Variable:0 b=tf.get_variable('b',[1]) print(b.name)#结果为b:0 |
从这个结果中,我们能很清晰地看到,tf.get_variable创建的变量并不是a/b:0,而是b:0。这就表示了在tf.name_scope函数下,tf.get_variable不受其约束。
TF.VARIABLE、TF.GET_VARIABLE、TF.VARIABLE_SCOPE以及TF.NAME_SCOPE关系的更多相关文章
- tensorflow共享变量 the difference between tf.Variable() and get_variable()
一般这样用tf.get_variable(): v = tf.get_variable(name, shape, dtype, initializer) 下面内容来源于 http://blog.csd ...
- tf.variable和tf.get_Variable以及tf.name_scope和tf.variable_scope的区别
在训练深度网络时,为了减少需要训练参数的个数(比如具有simase结构的LSTM模型).或是多机多卡并行化训练大数据大模型(比如数据并行化)等情况时,往往需要共享变量.另外一方面是当一个深度学习模型变 ...
- 理解 tf.Variable、tf.get_variable以及范围命名方法tf.variable_scope、tf.name_scope
tensorflow提供了通过变量名称来创建或者获取一个变量的机制.通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要将变量通过参数的形式到处传递. 1. tf.Variable( ...
- 彻底弄懂tf.Variable、tf.get_variable、tf.variable_scope以及tf.name_scope异同
https://blog.csdn.net/qq_22522663/article/details/78729029 1. tf.Variable与tf.get_variabletensorflow提 ...
- TensorFlow学习笔记(1):variable与get_variable, name_scope()和variable_scope()
Variable tensorflow中有两个关于variable的op,tf.Variable()与tf.get_variable()下面介绍这两个的区别 使用tf.Variable时,如果检测到命 ...
- tensorflow 笔记12:函数区别:placeholder,variable,get_variable,参数共享
一.函数意义: 1.tf.Variable() 变量 W = tf.Variable(<initial-value>, name=<optional-name>) 用于生成一个 ...
- TensorFlow函数(二)tf.get_variable() 和 tf.Variable()
tf.Variable(<initial - value>,name=<optional - name>) 此函数用于定义图变量.生成一个初始值为initial - value ...
- tensorflow中使用tf.variable_scope和tf.get_variable的ValueError
ValueError: Variable conv1/weights1 already exists, disallowed. Did you mean to set reuse=True in Va ...
- tensorflow中使用变量作用域及tf.variable(),tf,getvariable()与tf.variable_scope()的用法
一 .tf.variable() 在模型中每次调用都会重建变量,使其存储相同变量而消耗内存,如: def repeat_value(): weight=tf.variable(tf.random_no ...
随机推荐
- 循环结构for
教程:高能:语句结构都是由关键字开头,用冒号结束! 一:语句结构 for <variable> in <sequence>: <statements>else ...
- nginx获取上游真实IP(ngx_http_realip_module)
realip模块的作用是:当本机的nginx处于一个反向代理的后端时获取到真实的用户IP,如果没有realip模块,nginx的access_log里记录的IP会是反向代理服务器的IP,PHP中$_S ...
- 通过ffi在node.js中调用动态链接库[转]
http://blog.csdn.net/zhulin2609/article/details/51474676
- Oracle 的开窗函数 rank,dense_rank,row_number
1.开窗函数和分组函数的区别 分组函数是指按照某列或者某些列分组后进行某种计算,比如计数,求和等聚合函数进行计算. 开窗函数是指基于某列或某些列让数据有序,数据行数和原始数据数相同,依然能曾现个体数据 ...
- 最简单的 nginx 负载均衡,只能演示,企业中最好不用
修改nginx.conf 配置,重启nginx即可 upstream 包名{ ip_hash; #使用此功能,权重和备份都不能使用!一台机器永远只连同一台机子 server IP:端口 weight= ...
- Raiden Charge
2017年10月22 周日 这是个元气满满 值得纪念的一天(不好意思走错片场了) 虽然有各种乱遭的客观元素 但我们队确确实实地打铁了 那些我们轻视的 野鸡(误)大学 都在我们前面 都说知耻而后勇 虽然 ...
- CMS使用对应版本当作新项目。
document.form1 整体替换document.forms[0] document.Templetslist 整体替换document.forms[0] document.f_Upload整体 ...
- 图片转web字体库,如何制作web字体库
最近项目上用到了很多svg图,设计师经常频繁改版,苦不堪言,于是就想到了把图片转成字体库来使用. 使用图片的缺点: 1. 图片加载速度慢 2. 图片大小固定,无法调节 3. 当代码重构或者图片目录位置 ...
- 分门别类总结Java中的各种锁,让你彻底记住
概念 公平锁/非公平锁 公平锁是指多个线程按照申请锁的顺序来获取锁. 非公平锁是指多个线程获取锁的顺序并不是按照申请锁的顺序,有可能后申请的线程比先申请的线程优先获取锁.有可能,会造成优先级反转或者饥 ...
- [Swift]LeetCode995. K 连续位的最小翻转次数 | Minimum Number of K Consecutive Bit Flips
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray o ...