【发布时间】:2017-10-22 04:36:21
【问题描述】:
Tensorflow 中默认的全局 variable_scope 是什么?如何检查对象?有人对此有想法吗?
【问题讨论】:
标签: variables tensorflow machine-learning scope
Tensorflow 中默认的全局 variable_scope 是什么?如何检查对象?有人对此有想法吗?
【问题讨论】:
标签: variables tensorflow machine-learning scope
从技术上讲,所有变量都没有全局变量范围。如果你运行
x = tf.Variable(0.0, name='x')
从脚本的顶层开始,将在默认图表中创建一个没有变量范围的新变量x。
不过,tf.get_variable() 函数的情况有点不同:
x = tf.get_variable(name='x')
它做的第一件事是调用tf.get_variable_scope()函数,它返回当前变量作用域,然后从本地堆栈中查找作用域:
def get_variable_scope():
"""Returns the current variable scope."""
scope = ops.get_collection(_VARSCOPE_KEY)
if scope: # This collection has at most 1 element, the default scope at [0].
return scope[0]
scope = VariableScope(False)
ops.add_to_collection(_VARSCOPE_KEY, scope)
return scope
请注意,此堆栈可以为空,在这种情况下,只需创建一个新范围并将其推送到堆栈顶部。
如果this是你需要的对象,你可以通过调用来访问它:
scope = tf.get_variable_scope()
从顶层开始,或者如果您已经在范围内,则直接转到ops.get_collection(_VARSCOPE_KEY)。这正是通过调用tf.get_variable() 函数获得的新变量的范围。这是tf.VariableScope 类的一个普通实例,您可以轻松检查。
【讨论】: