如何强制tf.variable_scope重用name_scope?

时间:2016-07-20 18:21:50

标签: tensorflow

with tf.variable_scope('aa') as sa:
    with tf.variable_scope('bb'):
        x =  tf.get_variable(
            'biases', (2,),
            initializer=tf.constant_initializer()
        )
        y1 = tf.identity(x, name='bb')
with tf.variable_scope(sa):
    with tf.variable_scope('cc'):
        x =  tf.get_variable(
            'biases', (2,),
            initializer=tf.constant_initializer()
        )
        y2 = tf.identity(x, name='cc') 

我输入了tf.variable_scope('aa')两次,并生成了两个张量y1y2

然而,y2.name == 'aa_1/cc/cc:0'。 (y1.name == 'aa/bb/bb:0'

是否可以改为y2.name == 'aa/cc/cc:0'

1 个答案:

答案 0 :(得分:2)

有点晚了,但是尝试在名称范围的末尾添加/以明确表示您要重用范围。否则,正如您所注意到的,它会添加_1。我在name_scope遇到了类似的问题,但我认为解决方案也适用于variable_scope

with tf.variable_scope('aa/'):
    ... # Some initialisation
with tf.variable_scope('aa/'):
    ... # Reuse the name scope