Python variable_scope 和 name_scope 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34215746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:37:31  来源:igfitidea点击:

What is the difference between variable_scope and name_scope?

pythonscopetensorflow

提问by Andrzej Pronobis

What is the difference between variable_scopeand name_scope? The variable scope tutorialtalks about variable_scopeimplicitly opening name_scope. I also noticed that creating a variable in a name_scopeautomatically expands its name with the scope name as well. So, what is the difference?

variable_scope和 和有name_scope什么区别?该变量的作用域教程挂在嘴边variable_scope隐含打开name_scope。我还注意到在 a 中创建变量也会name_scope自动使用范围名称扩展其名称。那么区别是什么呢?

回答by cesarsalgado

When you create a variable with tf.get_variableinstead of tf.Variable, Tensorflow will start checking the names of the vars created with the same method to see if they collide. If they do, an exception will be raised. If you created a var with tf.get_variableand you try to change the prefix of your variable names by using the tf.name_scopecontext manager, this won't prevent the Tensorflow of raising an exception. Only tf.variable_scopecontext manager will effectively change the name of your var in this case. Or if you want to reuse the variable you should call scope.reuse_variables() before creating the var the second time.

当您使用tf.get_variable而不是创建变量时tf.Variable,Tensorflow 将开始检查使用相同方法创建的变量的名称,以查看它们是否发生冲突。如果他们这样做,将引发异常。如果您创建了一个 vartf.get_variable并尝试使用tf.name_scope上下文管理器更改变量名称的前缀,这不会阻止 Tensorflow 引发异常。tf.variable_scope在这种情况下,只有上下文管理器才能有效地更改 var 的名称。或者,如果您想重用该变量,您应该在第二次创建 var 之前调用 scope.reuse_variables()。

In summary, tf.name_scopejust add a prefix to all tensor created in that scope (except the vars created with tf.get_variable), and tf.variable_scopeadd a prefix to the variables created with tf.get_variable.

总而言之,tf.name_scope只需为在该范围内创建的所有张量添加一个前缀(除了用 创建的变量tf.get_variable),并tf.variable_scope为用创建的变量添加前缀tf.get_variable

回答by Salvador Dali

I had problems understanding the difference between variable_scopeand name_scope(they looked almost the same) before I tried to visualize everything by creating a simple example:

在我尝试通过创建一个简单的示例来可视化所有内容之前,我在理解variable_scopename_scope之间的区别时遇到了问题(它们看起来几乎相同):

import tensorflow as tf
def scoping(fn, scope1, scope2, vals):
    with fn(scope1):
        a = tf.Variable(vals[0], name='a')
        b = tf.get_variable('b', initializer=vals[1])
        c = tf.constant(vals[2], name='c')
        with fn(scope2):
            d = tf.add(a * b, c, name='res')

        print '\n  '.join([scope1, a.name, b.name, c.name, d.name]), '\n'
    return d

d1 = scoping(tf.variable_scope, 'scope_vars', 'res', [1, 2, 3])
d2 = scoping(tf.name_scope,     'scope_name', 'res', [1, 2, 3])

with tf.Session() as sess:
    writer = tf.summary.FileWriter('logs', sess.graph)
    sess.run(tf.global_variables_initializer())
    print sess.run([d1, d2])
    writer.close()

Here I create a function that creates some variables and constants and groups them in scopes (depending by the type I provided). In this function I also print the names of all the variables. After that I executes the graph to get values of the resulting values and save event-files to investigate them in tensorboard. If you run this, you will get the following:

在这里,我创建了一个函数,用于创建一些变量和常量,并将它们分组在范围内(取决于我提供的类型)。在这个函数中,我还打印了所有变量的名称。之后,我执行图形以获取结果值的值并保存事件文件以在 tensorboard 中调查它们。如果你运行这个,你会得到以下信息:

scope_vars
  scope_vars/a:0
  scope_vars/b:0
  scope_vars/c:0
  scope_vars/res/res:0 

scope_name
  scope_name/a:0
  b:0
  scope_name/c:0
  scope_name/res/res:0 

You see the similar pattern if you open TB (as you see bis outside of scope_namerectangular): enter image description here

如果您打开 TB,您会看到类似的模式(如您所见b,在scope_name矩形之外): 在此处输入图片说明



This gives you the answer:

这给了你答案

Now you see that tf.variable_scope()adds a prefix to the names of all variables (no matter how you create them), ops, constants. On the other hand tf.name_scope()ignores variables created with tf.get_variable()because it assumes that you know which variable and in which scope you wanted to use.

现在您看到,tf.variable_scope()为所有变量(无论您如何创建它们)、操作、常量的名称添加前缀。另一方面,tf.name_scope()忽略创建的变量,tf.get_variable()因为它假定您知道要使用哪个变量以及在哪个范围内。

A good documentation on Sharing variablestells you that

关于共享变量的一个很好的文档告诉你

tf.variable_scope(): Manages namespaces for names passed to tf.get_variable().

tf.variable_scope(): 管理传递给 的名称的命名空间tf.get_variable()

The same documentation provides a more details how does Variable Scope work and when it is useful.

相同的文档提供了更多详细信息 Variable Scope 如何工作以及何时有用。

回答by P-Gn

tf.variable_scopeis an evolution of tf.name_scopeto handle Variablereuse. As you noticed, it does more than tf.name_scope, so there is no real reason to use tf.name_scope: not surprisingly, a TF developper advises to just use tf.variable_scope.

tf.variable_scopetf.name_scope处理Variable重用的演变。正如您所注意到的,它的作用不仅仅是tf.name_scope,所以没有真正的理由使用tf.name_scope:毫不奇怪,TF 开发人员建议只使用tf.variable_scope.

My understanding for having tf.name_scopestill lying around is that there are subtle incompatibilities in the behavior of those two, which invalidates tf.variable_scopeas a drop-in replacement for tf.name_scope.

我对tf.name_scope仍然存在的理解是,这两者的行为存在细微的不兼容,tf.variable_scope作为tf.name_scope.