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!


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 .


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.

    链接地址: http://www.djcxy.com/p/96506.html

    上一篇: 用现有C对象初始化Cython对象

    下一篇: TensorFlow中的collection()