colinfang
colinfang

Reputation: 21727

How do I force tf.variable_scope to reuse name_scope?

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') 

I entered the tf.variable_scope('aa') twice, and generated 2 tensors y1, y2.

However, y2.name == 'aa_1/cc/cc:0'. (y1.name == 'aa/bb/bb:0')

Is it possible to make y2.name == 'aa/cc/cc:0' instead?

Upvotes: 1

Views: 840

Answers (1)

Conchylicultor
Conchylicultor

Reputation: 5729

It's a little late, but try to add a / at the end of the namescope to explicitly indicate you want to reuse the scope. Otherwise, as you noticed, it add a _1. I had a similar problem with name_scope but I think the solution works with variable_scope too.

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

Upvotes: 2

Related Questions