15

I am confused by tf.get_collection() form the docs, it says that

Returns a list of values in the collection with the given name.

And an example from the Internet is here

from_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, from_scope)

Is it means that it collects variables from tf.GraphKeys.TRAINABLE_VARIABLES to from_scope?

However, how can I use this function if I want to get variables from another scope? Thank you!

GoingMyWay
  • 13,866
  • 24
  • 83
  • 122

2 Answers2

10

A collection is nothing but a named set of values.

Every value is a node of the computational graph.

Every node has its name and the name is composed by the concatenation of scopes, / and values, like: preceding/scopes/in/that/way/value

get_collection, without scope allow fetching every value in the collection without applying any filter operation.

When the scope parameter is present, every element of the collection is filtered and its returned only if the name of the node starts with the specified scope.

nessuno
  • 23,549
  • 5
  • 71
  • 70
  • I am so confused on how to use this api. I am using it for Optimize functions and it's returning "No variables to optimize". I am pretty sure my scope is wrong. But how do I know the scope of my variable. To specify scope I used tf.name_scope('CNN/Encoder') and to fetch I used 'CNN'. Please help me where I may be going wrong – rkmalaiya Dec 20 '18 at 21:52
  • `tf.name_scope` is different from `tf.variable_scope`! name scope changes the scope of your operations only, not of the variables. You were looking for `tf.variable_scope`. I suggest you read this article https://pgaleone.eu/tensorflow/go/2017/05/29/understanding-tensorflow-using-go/ in ordert to understand how variables (and tf in general) work under the hood – nessuno Dec 21 '18 at 10:54
  • note that it equal to find variable names **start with** the scope. – Neo li Feb 20 '19 at 07:59
1

As described in the string doc:

  • TRAINABLE_VARIABLES: the subset of Variable objects that will be trained by an optimizer.

and

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

So it will return the list of trainable variables in the given scope.

pfm
  • 5,440
  • 2
  • 33
  • 42