tensorflow 中 name_scope 及 variable_scope 的异同
Let's begin by a short introduction to variable sharing. It is a mechanism in TensorFlow that allows for sharing variables accessed in different parts of the code without passing references to the variable around. The method tf.get_variable
can be used with the name of the variable as argument to either create a new variable with such name or retrieve the one that was created before. This is different from using the tf.Variable
constructor which will create a new variable every time it is called (and potentially add a suffix to the variable name if a variable with such name already exists). It is for the purpose of the variable sharing mechanism that a separate type of scope (variable scope) was introduced.
As a result, we end up having two different types of scopes:
- name scope, created using
tf.name_scope
ortf.op_scope
- variable scope, created using
tf.variable_scope
ortf.variable_op_scope
Both scopes have the same effect on all operations as well as variables created using tf.Variable
, i.e. the scope will be added as a prefix to the operation or variable name.
However, name scope is ignored by tf.get_variable
. We can see that in the following example:
with tf.name_scope("my_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) # var1:0
print(v2.name) # my_scope/var2:0
print(a.name) # my_scope/Add:0
The only way to place a variable accessed using tf.get_variable
in a scope is to use variable scope, as in the following example:
with tf.variable_scope("my_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) # my_scope/var1:0
print(v2.name) # my_scope/var2:0
print(a.name) # my_scope/Add:0
Finally, let's look at the difference between the different methods for creating scopes. We can group them in two categories:
tf.name_scope(name)
(for name scope) andtf.variable_scope(name_or_scope, ...)
(for variable scope) create a scope with the name specified as argumenttf.op_scope(values, name, default_name=None)
(for name scope) andtf.variable_op_scope(values, name_or_scope, default_name=None, ...)
(for variable scope) create a scope, just like the functions above, but besides the scopename
, they accept an argumentdefault_name
which is used instead ofname
when it is set toNone
. Moreover, they accept a list of tensors (values
) in order to check if all the tensors are from the same, default graph. This is useful when creating new operations, for example, see the implementation oftf.histogram_summary
.
大意是说 name_scope及variable_scope的作用都是为了不传引用而访问跨代码区域变量的一种方式,其内部功能是在其代码块内显式创建的变量都会带上scope前缀(如上面例子中的a),这一点它们几乎一样。而它们的差别是,在其作用域中获取变量,它们对 tf.get_variable() 函数的作用是一个会自动添加前缀,一个不会添加前缀。
tensorflow 中 name_scope 及 variable_scope 的异同的更多相关文章
- tensorflow 中 name_scope和variable_scope
import tensorflow as tf with tf.name_scope("hello") as name_scope: arr1 = tf.get_variable( ...
- 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中命名空间、变量命名的问题
1.简介 对比分析tf.Variable / tf.get_variable | tf.name_scope / tf.variable_scope的异同 2.说明 tf.Variable创建变量:t ...
- Tensorflow中的name_scope和variable_scope
Tensorflow是一个编程模型,几乎成为了一种编程语言(里面有变量.有操作......). Tensorflow编程分为两个阶段:构图阶段+运行时. Tensorflow构图阶段其实就是在对图进行 ...
- TensorFlow学习笔记(1):variable与get_variable, name_scope()和variable_scope()
Variable tensorflow中有两个关于variable的op,tf.Variable()与tf.get_variable()下面介绍这两个的区别 使用tf.Variable时,如果检测到命 ...
- TensorFlow中的L2正则化函数:tf.nn.l2_loss()与tf.contrib.layers.l2_regularizerd()的用法与异同
tf.nn.l2_loss()与tf.contrib.layers.l2_regularizerd()都是TensorFlow中的L2正则化函数,tf.contrib.layers.l2_regula ...
- [翻译] Tensorflow中name scope和variable scope的区别是什么
翻译自:https://stackoverflow.com/questions/35919020/whats-the-difference-of-name-scope-and-a-variable-s ...
- TensorFlow中的变量命名以及命名空间.
What: 在Tensorflow中, 为了区别不同的变量(例如TensorBoard显示中), 会需要命名空间对不同的变量进行命名. 其中常用的两个函数为: tf.variable_scope, t ...
- tensorflow中slim模块api介绍
tensorflow中slim模块api介绍 翻译 2017年08月29日 20:13:35 http://blog.csdn.net/guvcolie/article/details/77686 ...
随机推荐
- Action方法调用
一.Action访问路径 Action的访问路径是由struts.xml文件中配置的Action所在包的命名空间,Action的名字和常struts.action.extension共同决定的 例如: ...
- hdu4106 区间k覆盖问题(连续m个数,最多选k个数) 最小费用最大流 建图巧妙
/** 题目:hdu4106 区间k覆盖问题(连续m个数,最多选k个数) 最小费用最大流 建图巧妙 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4106 ...
- Flume示例
建议参考官方文档:http://flume.apache.org/FlumeUserGuide.html 示例一:用tail命令获取数据,下沉到hdfs 类似场景: 创建目录: mkdir /home ...
- Linux下vi命令小结
进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi n filename :打开文件,并将光标置于第n行首 vi filename :打 ...
- FOUC - Flash Of Unstyled Content 文档样式闪烁
问题描述 偶然间看到FOUC这个单词,在Google里找了半天终于发现了它的含义:Flash Of Unstyled Content.它指的是在某些情况下,IE在加载网页时会出现短暂的CSS样式失 ...
- (转)使用.NET Reflector 查看Unity引擎里面的DLL文件
当你查看unity里面API的时候,是不是有时候追踪了一两步就碰到DLL文件走不下去了呢?很是不爽吧. 这种问题我也是经常碰到.这是人家商业引擎不想让你看到底层代码啦,所以着急不得. 不过,今天我终于 ...
- VC++ Debug产生异常时中断程序执行Break on Exception
It is possible to instruct the debugger to break when an exception occurs, before a handler is invok ...
- 配置管理之PackageProvider接口
PackageProvider的开始 从前面几章中我们了解到了一点:想知道如何加载相关配置文件就必须去找StrutsXmlConfigurationProvider类和XmlConfiguratio ...
- sublime text 3 并列显示
alt+shift+1:显示一列 alt+shift+2:显示二列 alt+shift+3:显示三列 ......
- 搭建一个简单的基于web的网络流量监控可视化系统
本文转载于我的个人博客,转载请标明出处. 初衷 在腾讯云的学生认证申请提交上去n天之后,终于得到了审批,所以迫不及待的想玩玩腾讯云,作为一个搞网络的,自然有一些关于网络应用的小玩意,所以把以前部署过的 ...