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

  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. three.js学习:初学three.js,从立方体开始

    目前three.js是浏览器展现3D效果的一个很强大的js工具,遗憾的是没有特别系统而全面的文档(threejs官方文档感觉有些缺漏,可以和WebGL中文网的threejs教程对比着看).好了,根据W ...

  2. 微信小程序自定义微信客服按钮

    微信小程序官方api中提到的微信客服,是一个固定的组件,图标样式固定,大小最多27px 很明显这个是不能满足我们各种奇葩需求的.下面提供一个野蛮的自定义方法. 比如做一个这样的按钮: 图标用自定义的, ...

  3. 【Selenium-WebDriver自学】Selenium-IDE模式匹配(六)

    ==================================================================================================== ...

  4. ES6中新增字符串方法,字符串模板

    多了两个新方法 startsWith endsWith 返回的是一个Boolean值 let str='git://www.baidu.com/2123123'; if(str.startsWith( ...

  5. SAP work process Memory allocate

    Memory allocation sequence to dialog work processes in SAP What is the memory allocation sequence to ...

  6. 精通Web Analytics 2.0 (12) 第十章:针对潜在的网站分析陷阱的最佳解决方案

    精通Web Analytics 2.0 : 用户中心科学与在线统计艺术 第十章:针对潜在的网站分析陷阱的最佳解决方案 是时候去处理网站分析中最棘手的一些问题了,然后获得属于你的黑带,这是成为分析忍者的 ...

  7. Oracle数据库基础教程

    Oracle基础 简介 数据库实例 表空间 登录身份和角色 用户和授权 数据类型 数据操作 导入数据库 一.Oracle基础: 1.简介 Oracle创建数据库不能像SQL Server那样用一个简单 ...

  8. YARN label 特性 & 指定队列及label提交任务

    以下基于 hadoop版本 hadoop-2.8.4 给各个节点打标签 yarn rmadmin -addToClusterNodeLabels fastcpu,normal # 是否独占默认是tru ...

  9. 使用windows性能计数器监控cpu使用率

    https://blog.csdn.net/yabingshi_tech/article/details/26672355 2. http://blog.51cto.com/qixue/1702557 ...

  10. 使用子查询创建表(oracle)

    转自:https://blog.csdn.net/lxh123456789asd/article/details/81164321 语句: CREATE TABLE tablename[(column ...