I found the answer after reading in depth the tutorial on Sharing Variables from TensorFlow.
Suppose:
- you created a variable 'Weights' in a scope 'h1'
- you are in a scope 'foo'
- you want to retrieve the variable 'h1/Weights'
To do that, you need to save the scope object created with tf.variable_scope('h1')
to use it inside the scope 'foo'.
Some code will be more eloquent:
with tf.variable_scope('h1') as h1_scope: # we save the scope object in h1_scope
w = tf.get_variable('Weights', [])
with tf.variable_scope('foo'):
with tf.variable_scope(h1_scope, reuse=True): # get h1_scope back
w2 = tf.get_variable('Weights')
assert w == w2
Conclusion: When you pass the scope with its python object, and not only its name, you can get out of the current scope.