Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow: tf.get_collection Not Returning Variables in Scope

I'm trying to get all the variables in a variable scope, as is explained here. However, the line tf.get_collection(tf.GraphKeys.VARIABLES, scope='my_scope') is returning an empty list even though there are variables in that scope.

Here's some example code:

import tensorflow as tf

with tf.variable_scope('my_scope'):
    a = tf.Variable(0)
print tf.get_collection(tf.GraphKeys.VARIABLES, scope='my_scope')

which prints [].

How can I get the variables declared in 'my_scope'?

like image 657
Matt Cooper Avatar asked Dec 01 '16 18:12

Matt Cooper


1 Answers

The tf.GraphKeys.VARIABLES collection name has been deprecated since TensorFlow 0.12. Using tf.GraphKeys.GLOBAL_VARIABLES will give the expected result:

with tf.variable_scope('my_scope'):
    a = tf.Variable(0)
print tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='my_scope')
# ==> '[<tensorflow.python.ops.variables.Variable object at 0x7f33f67ebbd0>]'
like image 105
mrry Avatar answered Nov 14 '22 23:11

mrry