2016-09-19 86 views
0

變量Test在這兩種情況下是共享的嗎?整個範圍路徑是否需要匹配TensorFlow中的變量重用?

with tf.name_scope("ns1"): 
    with tf.variable_scope("vs1"): 
    var = tf.get_variable("Test", [1,2,3]) 

with tf.name_scope("ns2"): 
    with tf.variable_scope("vs1", reuse=True): 
    var = tf.get_variable("Test", [1,2,3]) 

with tf.name_scope("ns1"): 
    with tf.variable_scope("vs1"): 
    var = tf.get_variable("Test", [1,2,3]) 

with tf.variable_scope("vs1", reuse=True): 
    var = tf.get_variable("Test", [1,2,3]) 

回答

2

是,該變量被共享。一般來說,name_scope確實影響變量名稱,不是,而是,只有variable_scope(但是,變量_scopes的整個前綴必須匹配)。我認爲儘量不要使用name_scope是合理的,當與variable_scope混合時可能會引起混淆。還要注意,你設置了reuse = True - 如果變量沒有共享,你會得到一個錯誤。這就是爲什麼它在那裏,所以你可以確定它是共享的。

+0

謝謝!僅在變量的第一個實例化後使用'reuse = True'嗎? –