网络层中变量存在两个问题:

  1. 随着层数的增多,导致变量名的增多;
  2. 在调用函数的时候,会重复生成变量,但他们存储的都是一样的变量。

 

tf.variable不能解决这个问题。

变量作用域使用tf.variable_scope和tf.get_variable完美解决了上边的这个问题。

  1. 网络层数很多,但一般结构就那么几种。我们使用tf.get_variable方法,变量会在前边加上作用域,类似于文件系统中的“/”。
  2. tf.get_variable在第二次使用某个变量时,可以用reuse=True来共享之前定义过的变量。

------总结自《深入理解tensorflow架构设计与实现原理》

tf.name_scope()和tf.variable_scope()是两个作用域,一般与两个创建/调用变量的函数tf.variable() 和tf.get_variable()搭配使用。

tf.name_scope和 variable_scope也是个作为上下文管理器的角色,下文管理器:意思就是,在这个管理器下做的事情,会被这个管理器管着。

一.name_scope 和 variable_scope的用途:
name_scope 和 variable_scope 主要是因为 变量共享 的需求。

变量共享主要涉及两个函数:tf.variable() 和tf.get_variable();即就是必须要在tf.variable_scope的作用域下使用tf.get_variable()函数。这里用tf.get_variable(
) 而不用tf.Variable(
),是因为前者拥有一个变量检查机制,会检测已经存在的变量是否设置为共享变量,如果已经存在的变量没有设置为共享变量,TensorFlow
运行到第二个拥有相同名字的变量的时候,就会报错。

两个创建变量的方式。如果使用tf.Variable() 的话每次都会新建变量。但是大多数时候我们是希望重用一些变量,所以就用到了get_variable(),它会去搜索变量名,有就直接用,没有再新建。名字域。既然用到变量名了,就涉及到了名字域的概念。通过不同的域来区别变量名,毕竟让我们给所有变量都取不同名字还是很辛苦。这就是为什么会有scope 的概念。name_scope 作用于操作,variable_scope 可以通过设置reuse 标志以及初始化方式来影响域下的变量,因为想要达到变量共享的效果,
就要在 tf.variable_scope()的作用域下使用 tf.get_variable() 这种方式产生和提取变量. 不像
tf.Variable() 每次都会产生新的变量, tf.get_variable() 如果遇到了已经存在名字的变量时,
它会单纯的提取这个同样名字的变量,如果不存在名字的变量再创建.

举例:

with tf.variable_scope('V1',reuse=True):
a1 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))
a2 = tf.Variable(tf.random_normal(shape=[2,3], mean=0, stddev=1), name='a2')
with tf.variable_scope('V2',reuse=True):
a3 = tf.get_variable(name='a1', shape=[1],initializer=tf.constant_initializer(1))
a4 = tf.Variable(tf.random_normal(shape=[2,3], mean=0, stddev=1), name='a2') with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print (a1.name)
print (a2.name)
print (a3.name)
print (a4.name)

输出:

V1/a1:0
V1_14/a2:0
V2/a1:0
V2_2/a2:0 在tf.name_scope()中则没有resuse这个参数,无法实现这种操作。

二.TensorFlow中name scope和variable scope区别
TF中有两种作用域类型
命名域 (name scope),通过tf.name_scope 或 tf.op_scope创建;
变量域 (variable scope),通过tf.variable_scope 或 tf.variable_op_scope创建;
这两种作用域,对于使用tf.Variable()方式创建的变量,具有相同的效果,都会在变量名称前面,加上域名称。

对于通过tf.get_variable()方式创建的变量,只有variable scope名称会加到变量名称前面,而name scope不会作为前缀。例如 print(v1.name) # var1:0

例子:

with tf.name_scope("my_name_scope"):
v1 = tf.get_variable("var1", [1], dtype=tf.float32)
v2 = tf.Variable(1, name="var2", dtype=tf.float32)
a = tf.add(v1, v2)
print(v1.name)
print(v2.name)
print(a.name)

输出:

var1:0
my_name_scope/var2:0
my_name_scope/Add:0

小结:name_scope不会作为tf.get_variable变量的前缀,但是会作为tf.Variable的前缀。

with tf.variable_scope("my_variable_scope"):
v1 = tf.get_variable("var1", [1], dtype=tf.float32)
v2 = tf.Variable(1, name="var2", dtype=tf.float32)
a = tf.add(v1, v2)
print(v1.name)
print(v2.name)
print(a.name)

输出:

my_variable_scope/var1:0
my_variable_scope/var2:0

my_variable_scope/Add:0

小结:在variable_scope的作用域下,tf.get_variable()和tf.Variable()都加了scope_name前缀。因此,在tf.variable_scope的作用域下,通过get_variable()可以使用已经创建的变量,实现了变量的共享。

原文链接:https://blog.csdn.net/weixin_38698649/article/details/80099822

tf.name_scope()和tf.variable_scope() (转)的更多相关文章

  1. 通俗理解tf.name_scope()、tf.variable_scope()

    前言:最近做一个实验,遇到TensorFlow变量作用域问题,对tf.name_scope().tf.variable_scope()等进行了较为深刻的比较,记录相关笔记:tf.name_scope( ...

  2. tf.variable和tf.get_Variable以及tf.name_scope和tf.variable_scope的区别

    在训练深度网络时,为了减少需要训练参数的个数(比如具有simase结构的LSTM模型).或是多机多卡并行化训练大数据大模型(比如数据并行化)等情况时,往往需要共享变量.另外一方面是当一个深度学习模型变 ...

  3. tf.name_scope()和tf.variable_scope()

    https://blog.csdn.net/gqixf/article/details/80191918 https://blog.csdn.net/uestc_c2_403/article/deta ...

  4. tf.name_scope() 和 tf.variable_scope() 的用法和玄机

    https://my.oschina.net/liusicong/blog/1593467

  5. TF.VARIABLE、TF.GET_VARIABLE、TF.VARIABLE_SCOPE以及TF.NAME_SCOPE关系

    1. tf.Variable与tf.get_variable tensorflow提供了通过变量名称来创建或者获取一个变量的机制.通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要 ...

  6. 理解 tf.Variable、tf.get_variable以及范围命名方法tf.variable_scope、tf.name_scope

    tensorflow提供了通过变量名称来创建或者获取一个变量的机制.通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要将变量通过参数的形式到处传递. 1. tf.Variable( ...

  7. 彻底弄懂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提 ...

  8. TensorFlow基础笔记(13) tf.name_scope tf.variable_scope学习

    转载http://blog.csdn.net/jerr__y/article/details/60877873 1. 首先看看比较简单的 tf.name_scope(‘scope_name’). tf ...

  9. tf.name_scope tf.variable_scope学习

    1. 首先看看比较简单的 tf.name_scope(‘scope_name’). tf.name_scope 主要结合 tf.Variable() 来使用,方便参数命名管理. ''' Signatu ...

随机推荐

  1. solr学习之域的管理与中文分析器配置

    该文使用  Centos6.5 64 位    solr4.10.3   IK-Analyzer中文分析器 一.solr域 在solr中域的概念与lucene中域的概念相同,数据库的一条记录或者一个文 ...

  2. SPOJ COT3 - Combat on a tree

    /* 考虑直接使用暴力来算的话 SG[i]表示以i为根的子树的SG值, 然后考虑枚举删除那个子树节点, 然后求拆成的树的sg异或值, 求mex即可 复杂度三次方 然后考虑尝试 整体来做 发现对于每次子 ...

  3. day4作业(基本运算流程if for)

    #coding:utf-8'''默写: 循环嵌套 必做: 1. 求1-100的所有数的和 2. 输出 1-100 内的所有奇数 3. 输出 1-100 内的所有偶数 5. 求1-2+3-4+5 ... ...

  4. jsfiddle修改个人头像

    找了半天终于知道修改jsfiddle头像的方法了~ JsFiddle将Gravatar - 全球认可的头像用于个人资料图片.必须在这里改变你的头像,它也会在jsFiddle中自动更新. 注意,两者的注 ...

  5. 《算法导论》——MaximumSubArray

    今天我们讨论的算法是最大子数组问题. 首先我定义了一个类用来保存最大子数组的开始位置索引.结束位置索引和该数组的和.代码如下: class MaximumSubArray { private: int ...

  6. 查看mysql 表大小

    select round(sum(DATA_LENGTH / 1024 / 1024 ), 2) from `information_schema`.`tables` where table_sche ...

  7. JavaScript进行简单的随即验证码生成(适合和我一样刚入门一本完整的教材书都没看完的弟弟)

    (感谢橙旭园给我打开了前端世界的大门) var checkcode = document.getElementById("code"); var btn = document.ge ...

  8. a标签自执行点击事件

    //html <a href='http://www.baidu.com' ><button id='sss'>百度</button></a> //原生 ...

  9. HTML5 Canvas 小例子 简易画板

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. java数组实现队列

    数组队列 用数组实现的队列,也叫循环队列.就是定义一个数组,用两个下标head,tail表示队头和队尾.当队头和队尾相等时,队列为空.当队尾+1等于队头时,队列为满. 注意tail的值,当插入一个元素 ...