Dims
Dims

Reputation: 51009

How to get or create variable in root scope in tensorflow?

I am writing functions, which creates some blocks of neural network. Each of this function starts from

with tf.variable_scope(name):

so it creates all of it's nodes withing some named scope.

But sometimes I need a variable in the root scope, like is_training variable, to use it from time to time in different block.

So, how to access/create this variable being inside some nested scopes?

Upvotes: 1

Views: 811

Answers (2)

xiang0x48
xiang0x48

Reputation: 651

I'm facing the same problem, and currently using one 'dirty' solution to it.

The with tf.variable_scope(name_or_scope) function accepts not only name with type str, but also scope with type VariableScope.

The following code shows the trick:

root_scope = tf.get_variable_scope()

with tf.variable_scope(root_scope):
    x0 = tf.get_variable('x', [])
with tf.variable_scope(root_scope, reuse=True):
    x1 = tf.get_variable('x', [])
with tf.variable_scope('scope_1'):
    x2 = tf.get_variable('x', [])
    with tf.variable_scope(root_scope):
        y = tf.get_variable('y', [])
print('x0:', x0)
print('x1:', x1)
print('x2:', x2)
print('y:', y)

outputs are:

x0: <tf.Variable 'x:0' shape=() dtype=float32_ref>
x1: <tf.Variable 'x:0' shape=() dtype=float32_ref>
x2: <tf.Variable 'scope_1/x:0' shape=() dtype=float32_ref>
y: <tf.Variable 'y:0' shape=() dtype=float32_ref>

In this way you can share variables of root scope (x0 and x1) and create an variable of root scope within other nested scopes (like y).

If we use a module level global variable to store root_scope, and init it near the entry of the program, we can easily access it every where.

However this method requires using global variable, which may not be a good choice. I'm still wondering if there is better solution.

Upvotes: 2

Vamsi Sistla
Vamsi Sistla

Reputation: 136

Here is one approach to handle this. You can initialize all your variable that you want to use in other scopes in one place - like dictionary of variables.

According to Tensorflow website

A common way to share variables is to create them in a separate piece of code and pass them to functions that use them. For example by using a dictionary:

variables_dict = {
     "conv1_weights": tf.Variable(tf.random_normal([5, 5, 32, 32]),
      name="conv1_weights")
     "conv1_biases": tf.Variable(tf.zeros([32]), name="conv1_biases")
     ... etc. ...
}

.... ....

result1 = my_image_filter(image1, variables_dict)
result2 = my_image_filter(image2, variables_dict)

There might be other ways as well (like creating classes, etc) but this should address your basic problem.

Upvotes: 0

Related Questions