2017-10-22 224 views

回答

1

从技术上讲,对于所有变量没有全局变量作用域。如果您从脚本的顶层运行

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 

注意这堆可以是空的,在这种情况下,简单地创建了一个新的机会和压入堆栈的顶部。从顶层,或将ops.get_collection(_VARSCOPE_KEY)直接,如果你是一个范围内已经

scope = tf.get_variable_scope() 

如果是你需要的对象,你可以通过调用访问它。通过调用tf.get_variable()函数,这正是新变量的范围。这是您可以轻松检查的tf.VariableScope类的普通实例。