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'
?
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>]'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With