【问题标题】:What's the usage of variable_scope with name_or_scope = Nonename_or_scope = None 的 variable_scope 有什么用
【发布时间】:2017-12-11 05:08:22
【问题描述】:

当我们使用 variable_scope 时,我们通常将第一个参数 (name_or_scope) 设置为 variable_scope 的名称。并且当我们将重用变量设置为“True”时,我们可以在 variable_scope 内共享该变量。 但是,我发现在tensorflow API(例如Bahdanau attention,或tf.layers.Dense)内部,有变量范围,其第一个参数(name_or_scope)设置为None,第二个参数(default_name)设置正如我们认为的范围名称。 查了tf.variable_scope的代码,发现:

如果name_or_scope 不是None,则按原样使用。如果scope 为无, 然后使用default_name。在这种情况下,如果同名 以前在同一范围内使用过,它将由 将_N 附加到它。

在这种情况下,我不能在 for 循环中使用它,因为每次调用 variable_scope 时,它​​都会创建具有不同名称的新 variable_scope

我找不到任何解释此功能的地方(我的意思是 name_or_scope 设置为 None)。

有人解释一下吗?

【问题讨论】:

标签: python variables tensorflow gpu


【解决方案1】:

你是对的,当 tf.variable_scopename_or_scope=None 调用时,default_name 参数被使用并且它是唯一的。所以在循环中调用它会创建不同的作用域。

不确定Bahdanau attention,但可以通过_scope 参数使用自定义范围创建tf.layers.Dense

layer = Dense(units, ..., _scope=name)

实际上,您可以使用指定的name 来调用tf.layers.dense,而这个名称将定义tf.variable_scope(name)。这种方式允许您在循环中创建密集层。

【讨论】:

  • 您的回答对我帮助很大。谢谢。但是,你能解释一下他们为什么要创建这样一个函数吗?我的意思是,在什么情况下应该使用 None 值 'name_or_scope = None' 的参数。
  • 一个用例是禁止变量重用,换句话说,强制唯一的范围。此外,您可能希望避免嵌套范围名称以使其在 tensorboard 中看起来更好。这是 tf 源代码库中的一个示例:github.com/tensorflow/tensorflow/blob/master/tensorflow/python/…
猜你喜欢
  • 1970-01-01
  • 2016-03-16
  • 1970-01-01
  • 1970-01-01
  • 2018-04-20
  • 2014-02-23
  • 1970-01-01
  • 1970-01-01
  • 2018-09-20
相关资源
最近更新 更多