0

I've been investigating the tensorflow docs for some way to retrieve a variable using absolute name, rather than relative name to the existing scope

Something like get_variable_absolute that would receive an absolute path to the var (i.e: h1/Weights rather than Weights inside a h1 variable scope)

This question is motivated by extreme frustration with this problem.

Community
  • 1
  • 1
diffeomorphism
  • 991
  • 2
  • 10
  • 27

1 Answers1

4

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.

Olivier Moindrot
  • 27,908
  • 11
  • 92
  • 91