TF有两个scope, 一个是name_scope一个是variable_scope

第一个程序:

with tf.name_scope("hello") as name_scope:
arr1 = tf.get_variable("arr1", shape=[2,10],dtype=tf.float32) print name_scope # "hello/" 
print arr1.name # arr1:0 
print "scope_name:%s " % tf.get_variable_scope().original_name_scope # 空

第二个程序:

with tf.variable_scope("hello") as variable_scope:
arr1 = tf.get_variable("arr1", shape=[2, 10], dtype=tf.float32) print variable_scope # 返回的是一个 op对象
print variable_scope.name # 打印出变量空间名字
print arr1.name
print tf.get_variable_scope().original_name_scope
# tf.get_variable_scope() 获取的就是variable_scope with tf.variable_scope("xixi") as v_scope2:
print tf.get_variable_scope().original_name_scope
# tf.get_variable_scope() 获取的就是v _scope2

输出为:

<tensorflow.python.ops.variable_scope.VariableScope object at 0x7fbc09959210>
hello
hello/arr1:0
hello/
hello/xixi/

第三个程序:

with tf.name_scope("name1"):
with tf.variable_scope("var1"):
w = tf.get_variable("w",shape=[2])
res = tf.add(w,[3]) print w.name
print res.name 输出为:
var1/w:
name1/var1/Add:0

可以看出:variable scopename scope都会给opname加上前缀

对比三个个程序可以看出:

  • name_scope对 get_variable()创建的变量 的名字不会有任何影响,而创建的op会被加上前缀.
  • tf.get_variable_scope() 返回的只是 variable_scope,不管 name_scope.所以以后我们在使用tf.get_variable_scope().reuse_variables() 时可以无视name_scope

其它

with tf.name_scope("scope1") as scope1:
with tf.name_scope("scope2") as scope2:
print scope2
#输出:scope1/scope2/
import tensorflow as tf
with tf.variable_scope("scope1") as scope1:
with tf.variable_scope("scope2") as scope2:
print scope2.name
#输出:scope1/scope2

name_scope可以用来干什么

Background: 典型的 TensorFlow 可以有数以千计的节点,如此多而难以一下全部看到,甚至无法使用标准图表工具来展示。

Solution: 为简单起见,我们为 op/tensor名 划定范围,并且可视化把该信息用于在图表中的节点上定义一个层级。默认情况下, 只有顶层节点会显示。

下面这个例子使用tf.name_scope在hidden命名域下定义了三个操作: 【好例子】

import tensorflow as tf

with tf.name_scope('hidden') as scope:
a = tf.constant(5, name='alpha')
W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0), name='weights')
b = tf.Variable(tf.zeros([1]), name='biases')
print a.name  # hidden/alpha 
print W.name  # hidden/weights 
print b.name  # hidden/biases 

name_scope 是给op_name加前缀

variable_scope是给get_variable()创建的变量的名字加前缀。

tf.variable_scope有时也会处理命名冲突

import tensorflow as tf
def test(name=None):
with tf.variable_scope(name, default_name="scope") as scope:
w = tf.get_variable("w", shape=[2, 10]) test()
test()
ws = tf.trainable_variables()
for w in ws:
print(w.name) #scope/w:0
#scope_1/w:0
#可以看出,如果只是使用default_name这个属性来创建variable_scope
#的时候,会处理命名冲突

其它

  • tf.name_scope(None) 有清除name scope的作用
import tensorflow as tf
with tf.name_scope("hehe"):
w1 = tf.Variable(1.0)
with tf.name_scope(None):
w2 = tf.Variable(2.0)
print(w1.name)
print(w2.name) #hehe/Variable:0
#Variable:0

variable_scope可以用来干什么

variable_scope 用来管理 variable 详见variable_scope

总结

简单来看 
1. 使用tf.Variable()的时候,tf.name_scope()tf.variable_scope() 都会给 Variable 和 op 的 name属性加上前缀。 
2. 使用tf.get_variable()的时候,tf.name_scope()就不会给 tf.get_variable()创建出来的Variable加前缀。

[TensorBoard] Name & Variable scope的更多相关文章

  1. [翻译] Tensorflow中name scope和variable scope的区别是什么

    翻译自:https://stackoverflow.com/questions/35919020/whats-the-difference-of-name-scope-and-a-variable-s ...

  2. [Ruby] Ruby Variable Scope

    Scope defines where in a program a variable is accessible. Ruby has four types of variable scope, lo ...

  3. tensorflow变量作用域(variable scope)

    举例说明 TensorFlow中的变量一般就是模型的参数.当模型复杂的时候共享变量会无比复杂. 官网给了一个case,当创建两层卷积的过滤器时,每输入一次图片就会创建一次过滤器对应的变量,但是我们希望 ...

  4. PHP Variable Scope

    原文: https://phppot.com/php/variable-scope-in-php/ Last modified on March 24th, 2017 by Vincy. ------ ...

  5. tensorflow variable scope 变量命名空间和变量共享

    import tensorflow as tf def f(): var = tf.Variable(initial_value=tf.random_normal(shape=[2])) return ...

  6. Python中变量的作用域(variable scope)

    http://www.crifan.com/summary_python_variable_effective_scope/ 解释python中变量的作用域 示例: 1.代码版 #!/usr/bin/ ...

  7. JavaScript变量作用域(Variable Scope)和闭包(closure)的基础知识

    在这篇文章中,我会试图讲解JavaScript变量的作用域和声明提升,以及许多隐隐藏的陷阱.为了确保我们不会碰到不可预见的问题,我们必须真正理解这些概念. 基本定义 作用范围是个“木桶”,里面装着变量 ...

  8. Python Variable Scope

    Python中的变量的作用域有时会让像我这样的初学者很头疼. 其实只需要掌握以下两点: 1. Python能够改变变量作用域的代码段是def.class.lamda;    而if/elif/else ...

  9. python variable scope 变量作用域

    python 中变量的作用域经常让我感到很迷 In Python, on the other hand, variables declared in if-statements, for-loop b ...

随机推荐

  1. 轻松学C#----第二篇笔记

    第二篇: 分析下第一篇的代码,见下图: 2.同其他语言一样,C#语言在编写时也遵守一定的语法规范. A.标识符(identify):指为方法.变量.其他任何用户自定义项指定的名称.标识符必须遵循一定的 ...

  2. TXB0108 TXS0108E 8-Bit Bidirectional Voltage-Level Translator for Open-Drain and Push-Pull Applications

    TXS(开漏优化设计),如I2C TXB(上拉优化设计),如SPI TXS0108 has integrated pull-up resistors to save board space and c ...

  3. [Go] 理解 golang 中的 nil

    nil是什么 相信写过Golang的程序员对下面一段代码是非常非常熟悉的了: if err != nil { // do something.... } 当出现不等于nil的时候,说明出现某些错误了, ...

  4. UnityEditor研究学习之EditorWindow

    在unity使用过程中,其实我们都是在各个不同功能的Window下工作. 比如在Scene窗口中操作物体,在Inspector中操作物体属性,在Game视窗中观察游戏状态. 所以窗口是Unity的灵魂 ...

  5. 290.单词模式。给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循相同的模式。(c++方法)

    题目描述: 给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循相同的模式. 这里的遵循指完全匹配,例如, pattern 里的每个字母和字符串 str 中的每个非空单词之 ...

  6. WPF腾讯视频通话开发

    一.IntPtr.HandleC#中的IntPtr类型称为“平台特定的整数类型”,它们用于本机资源,如窗口句柄. 1.WPF窗口句柄IntPtr wnip = new System.Windows.I ...

  7. 每天一个linux命令(1):pwd命令

    1.命令简介 pwd(print work directory 打印当前目录)命令以绝对路径的方式显示用户当前工作目录. 2.用法 pwd [-LP] 3.选项 -L --logical 当目录为连接 ...

  8. JAVA常用代码

    一. 判断是否包含某个注解.    1). 声明接口 @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented ...

  9. 如何在WCF中用TcpTrace工具查看发送和接收的SOAP消息

    WCF对消息加密(只对消息加密,不考虑Authorize)其实很简单,只要在server和client端的binding加入security mode为Message(还有Transport, Tra ...

  10. 编程调节Win7/Win8系统音量的一种方法

    不得不说, 自Win7(好像是吧), Windows的音量调节功能比以前更人性化了....      但编程接口却变得更加复杂了............. 还要用到IAudioEndpointVolu ...