variable_scope和name_scope有什么区别?

时间:2015-12-11 03:42:47

标签: python scope tensorflow

variable_scopename_scope之间有什么区别? variable scope tutorial谈到variable_scope隐含地打开name_scope。我还注意到,在name_scope中创建变量也会自动使用范围名称扩展其名称。那么,有什么区别?

3 个答案:

答案 0 :(得分:48)

在我尝试通过创建一个简单示例来显示所有内容之前,我在理解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()

这里我创建了一个函数,它创建了一些变量和常量,并在范围内对它们进行分组(取决于我提供的类型)。在这个函数中,我还打印所有变量的名称。之后,我执行图形以获取结果值的值并保存事件文件以在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 

如果您打开TB,您会看到类似的模式(如您所见bscope_name矩形之外): enter image description here

这为您提供了答案

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

关于Sharing variables的好文档告诉你

  

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

相同的文档提供了有关变量范围如何工作以及何时有用的更多详细信息。

答案 1 :(得分:39)

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

总之,tf.name_scope只是为在该范围内创建的所有张量添加前缀(使用tf.get_variable创建的变量除外),tf.variable_scope为使用{创建的变量添加前缀{1}}。

答案 2 :(得分:2)

tf.variable_scopetf.name_scope的改进,可以处理Variable重用。正如您所注意到的,它的作用远远超过tf.name_scope,因此没有真正的理由使用tf.name_scopea TF developper advises to just use tf.variable_scope也就不足为奇了。

我对让tf.name_scope仍然存在的理解是,这两个行为之间存在细微的不兼容,这使tf.variable_scope不能作为tf.name_scope的替代品。