2017-10-22 228 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類的普通實例。