Matt Cooper
Matt Cooper

Reputation: 2062

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

Upvotes: 6

Views: 4232

Answers (1)

mrry
mrry

Reputation: 126154

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

Upvotes: 11

Related Questions