tf.name_scope()和tf.variable_scope() (转)
网络层中变量存在两个问题:
- 随着层数的增多,导致变量名的增多;
- 在调用函数的时候,会重复生成变量,但他们存储的都是一样的变量。
tf.variable不能解决这个问题。
变量作用域使用tf.variable_scope和tf.get_variable完美解决了上边的这个问题。
- 网络层数很多,但一般结构就那么几种。我们使用tf.get_variable方法,变量会在前边加上作用域,类似于文件系统中的“/”。
- 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() (转)的更多相关文章
- 通俗理解tf.name_scope()、tf.variable_scope()
前言:最近做一个实验,遇到TensorFlow变量作用域问题,对tf.name_scope().tf.variable_scope()等进行了较为深刻的比较,记录相关笔记:tf.name_scope( ...
- tf.variable和tf.get_Variable以及tf.name_scope和tf.variable_scope的区别
在训练深度网络时,为了减少需要训练参数的个数(比如具有simase结构的LSTM模型).或是多机多卡并行化训练大数据大模型(比如数据并行化)等情况时,往往需要共享变量.另外一方面是当一个深度学习模型变 ...
- tf.name_scope()和tf.variable_scope()
https://blog.csdn.net/gqixf/article/details/80191918 https://blog.csdn.net/uestc_c2_403/article/deta ...
- tf.name_scope() 和 tf.variable_scope() 的用法和玄机
https://my.oschina.net/liusicong/blog/1593467
- TF.VARIABLE、TF.GET_VARIABLE、TF.VARIABLE_SCOPE以及TF.NAME_SCOPE关系
1. tf.Variable与tf.get_variable tensorflow提供了通过变量名称来创建或者获取一个变量的机制.通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要 ...
- 理解 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基础笔记(13) tf.name_scope tf.variable_scope学习
转载http://blog.csdn.net/jerr__y/article/details/60877873 1. 首先看看比较简单的 tf.name_scope(‘scope_name’). tf ...
- tf.name_scope tf.variable_scope学习
1. 首先看看比较简单的 tf.name_scope(‘scope_name’). tf.name_scope 主要结合 tf.Variable() 来使用,方便参数命名管理. ''' Signatu ...
随机推荐
- Noi.ac #309. Mas的童年(贪心)
/* 用所谓的加法拆分操作得到 x + y = (x ^ y) + 2 * (x & y) 那么我们这两段异或相当于前缀和 + 2 * 分段使左右两块&最大 记当前前缀异或和为S, 那 ...
- 安全测试5_服务端的安全漏洞(SQL注入、命令注入、文件操作类)
前面大致讲解了下客户端的安全漏洞,现在来讲解下服务端的安全漏洞. 1.SQL注入(SQL Injection),是一种常见的Web安全漏洞,攻击者利用这个漏洞,可以访问或修改数据,或者利用潜在的数据库 ...
- 最小化安装CentOS 7后,图形界面的安装(GNOME、KDE等)
安装图形化界面: 1.首先安装X(X Window System),命令为 yum groupinstall "X Window System" 2.检查一下我们已经安装的软件以及 ...
- android 开发 写一个RecyclerView布局的聊天室,并且添加RecyclerView的点击事件
实现思维顺序: 1.首先我们需要准备2张.9的png图片(一张图片为左边聊天泡泡,一个图片为右边的聊天泡泡),可以使用draw9patch.bat工具制作,任何图片导入到drawable中. 2.需要 ...
- 白鹭引擎 - 文本类型 ( TextField, )
1, 普通文本 class Main extends egret.DisplayObjectContainer { public constructor() { super(); this.addEv ...
- k8s资料转载
K8S入门(二) kubeadmin单机部署 (kubernetes)k8s入门.yum单机版安装.kuberctl指令.k8s服务实例. kubernetes---CentOS7安装kubernet ...
- 页面适应电脑和手机屏幕initial-scale 1:0 user-scalable=yes
2017年09月25日 11:30:27 Goddess_liangyanli 阅读数:7324 标签: 手机 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...
- [转]J-Link Commander的妙用
转自http://maker.zlgmcu.com/portal.php?mod=view&aid=3685 J-Link Commander作为J-Link驱动软件包的一个工具之一,为工程师 ...
- CRC16-CCITT C语言代码
代码如下,使用空间换时间的方法 #define CRC16_CCITT_SEED 0xFFFF // 该位称为预置值,使用人工算法(长除法)时 需要将除数多项式先与该与职位 异或 ,才能得到最后的除数 ...
- nginx 和 php超时设置
nginx.conf --- http节: keepalive_timeout 600; #客户端浏览器超时时间fastcgi_connect_timeout 600; #php-fpm连接超时时间 ...