GoingMyWay
GoingMyWay

Reputation: 17468

How to understand tf.get_collection() in TensorFlow

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!

Upvotes: 16

Views: 17133

Answers (2)

pfm
pfm

Reputation: 6328

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.

Upvotes: 1

nessuno
nessuno

Reputation: 27042

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.

Upvotes: 11

Related Questions