41

I have some variables created within a certain scope like this:

with tf.variable_scope("my_scope"):
  createSomeVariables()
  ...

I then want to get the list of all the variables in "my_scope" so I can pass it to an optimizer. What is the right way to do this?

0

2 Answers 2

79

I think you want tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='my_scope'). This will get all variables in a scope.

To pass to an optimizer you do not want all variables you would just want the trainable variables. Those are also kept in a default collection, which is tf.GraphKeys.TRAINABLE_VARIABLES.

5
  • 18
    tf.GraphKeys.VARIABLES is deprecated in v0.12 (as I learned from this answer: stackoverflow.com/a/40918792/1827383). Use tf.GraphKeys.GLOBAL_VARIABLES instead. Dec 2, 2016 at 5:45
  • do you have have to create an op out of that and then run it in a session? It seems the code is incomplete, do you mind making it self contained? Jan 26, 2017 at 3:06
  • Thanks for your answer! How about this situation: there are two sub scopes tf.variable_scope(1st) and tf.variable_scope(2nd) inside a scope tf.variable_scope(main) and I want to get two lists of scopes 1st and 2nd so as to optimize separately.
    – ytutow
    Aug 24, 2017 at 1:00
  • I think this answer is wrong (tested in tensorflow 1.4). tf.get_collection gets by NAME scope, and not by VARIABLE scope. You could have a bunch of variables declared under variable_scope("foo"), and tf.get_collection(..,"foo") would return nothing.
    – Uri Merhav
    Nov 17, 2017 at 19:48
  • 1
    One might want to detect the scope name automatically: tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=tf.get_variable_scope().name)
    – borgr
    Aug 14, 2018 at 10:43
16

User correctly pointed out that you need tf.get_collection(). I will just give a simple example how to do this:

import tensorflow as tf

with tf.name_scope('some_scope1'):
    a = tf.Variable(1, 'a')
    b = tf.Variable(2, 'b')
    c = tf.Variable(3, 'c')

with tf.name_scope('some_scope2'):
    d = tf.Variable(4, 'd')
    e = tf.Variable(5, 'e')
    f = tf.Variable(6, 'f')

h = tf.Variable(8, 'h')

for i in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='some_scope'):
    print i   # i.name if you want just a name

Notice that you can provide any of the graphKeys and scope is a regular expression:

scope: (Optional.) If supplied, the resulting list is filtered to include only items whose name attribute matches using re.match. Items without a name attribute are never returned if a scope is supplied and the choice or re.match means that a scope without special tokens filters by prefix.

So if you will pass 'some_scope' you will get 6 variables.

2
  • what if I then wanted to put all other variables in a separate collection. For example, GLOBAL_VARIABLES contains a through h, and 'some_scope' ends up with a through f, but then I want to have a second operation that just gets anything that isn't in my other collection (without using the regex)
    – reese0106
    Feb 9, 2018 at 18:33
  • How would you use this paired with session.run() to get the list of variables ?
    – monolith
    Mar 24, 2018 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.