2016-09-19 46 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'吗? –