What's the difference of name scope and a variable scope in tensorflow?
来源:互联网 发布:淘宝断码地带是正品吗 编辑:程序博客网 时间:2024/05/22 06:40
https://stackoverflow.com/questions/35919020/whats-the-difference-of-name-scope-and-a-variable-scope-in-tensorflow
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 the 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
- variable scope, created using
tf.variable_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:0print(v2.name) # my_scope/var2:0print(a.name) # my_scope/Add:0
The only way to place a variable accessed using tf.get_variable
in a scope is to use a 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:0print(v2.name) # my_scope/var2:0print(a.name) # my_scope/Add:0
This allows us to easily share variables across different parts of the program, even within different name scopes:
with tf.name_scope("foo"): with tf.variable_scope("var_scope"): v = tf.get_variable("var", [1])with tf.name_scope("bar"): with tf.variable_scope("var_scope", reuse=True): v1 = tf.get_variable("var", [1])assert v1 == vprint(v.name) # var_scope/var:0print(v1.name) # var_scope/var:0
UPDATE: op_scope/variable_op_scope is deprecated!
As of version r0.11, op_scope
and variable_op_scope
are both deprecated and replaced by name_scope
and variable_scope
.
- What's the difference of name scope and a variable scope in tensorflow?
- What's the difference of name scope and a variable scope in tensorflow?
- tensorflow:name&variable scope
- 转载!tensorflow name scope和variable scope
- What’s the difference between a class variable and an instance variable?
- tensorflow学习笔记(十七):name&variable scope
- What's the difference between tf.placeholder and tf.Variable
- name&variable scope
- Include directive and variable scope in PHP
- What's the difference and compatibility of CGLayer and CALayer?
- What's the difference between a thread and a process?
- What’s the difference between a stack and a heap?
- What's the difference between a URI and a URL?
- In SQL, what’s the difference between a full join and an inner join?
- In C++, what’s the difference between an inline function and a macro?
- What's the difference of hash table and binary tree?
- What's the difference between LastWriteTime and ChangeTime in FILE_BASIC_INFO?
- What's the difference between Template and Table in Smartforms
- 【Leetcode】【python】Insert Interval
- CCF CSP 送货 JAVA 201512-4
- 行时类型识别(RTTI, Run-Time Type Identification)是Java中非常有用的机制 多态 类加载
- 5款语言神器,告别论文写作“中式英语”
- 写在开博之前
- What's the difference of name scope and a variable scope in tensorflow?
- 最近也许会用的可能对作图有用的工具整理
- Eclipse 高亮显示选中的相同变量
- 微服务体系下如何快速构建一个服务
- connect four / tic-tak-toe
- 055 不定积分概念与性质
- Xcode9学习笔记1
- samohyes的心情随笔(3)
- Java中的final关键字
scope
method which effectively does avariable_scope
?" – John Feb 25 at 18:02variable_scope
vsname_scope
is even needed. If one creates a variable (in any way withtf.Variable
ortf.get_variable
), it seems more natural to me that we should always be able to get it if we specify the scope or its full name. I don't understand why one ignores the scope name thing while the other doesn't. Do you understand the rational for this weird behaviour? – Charlie Parker Mar 20 at 19:31