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?

Vlad Firoiu
  • 987
  • 1
  • 8
  • 17

2 Answers2

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.

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
user728291
  • 4,138
  • 1
  • 23
  • 26
  • 18
    `tf.GraphKeys.VARIABLES` is deprecated in v0.12 (as I learned from this answer: http://stackoverflow.com/a/40918792/1827383). Use `tf.GraphKeys.GLOBAL_VARIABLES` instead. – Matt Cooper Dec 02 '16 at 05: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? – Charlie Parker Jan 26 '17 at 03: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 '17 at 01: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 '17 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 '18 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.

jkschin
  • 5,776
  • 6
  • 35
  • 62
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
  • 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 09 '18 at 18:33
  • How would you use this paired with session.run() to get the list of variables ? – monolith Mar 24 '18 at 17:50