2

I have checked the documentations of both methods but they look the same, except that get_collection can take an additional scope parameter.

In [11]: aaa = tf.get_collection_ref(tf.GraphKeys.UPDATE_OPS)
In [12]: aaaa = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
In [13]: aaa == aaaa
Out[13]: True
In [14]: aaa is aaaa
Out[14]: False

What's the difference between the two and when to use which one?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Lerner Zhang
  • 6,184
  • 2
  • 49
  • 66

2 Answers2

0

If you don't use the scope argument in tf.get_collection, those two methods return the same collection in the computational graph.

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

When the scope parameter is specified, every element of the collection is filtered by the scope.

Consider the following sample code, which returns the same as your sample code (here I used tf.GraphKeys.TRAINABLE_VARIABLES instead for the demo purposes).

with tf.variable_scope("foo1"):
    v1 = tf.get_variable("v1", [1])
with tf.variable_scope("foo2"):
    v2 = tf.get_variable("v2", [1])

aaa = tf.get_collection_ref(tf.GraphKeys.TRAINABLE_VARIABLES)
aaaa = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)

print(aaa == aaaa) #True
print(aaa is aaaa) #False

Since there is no scope specified, aaa and aaaa objects referred to by the variables are equal.

However, If you run the following sample code with the scope specified,

with tf.variable_scope("foo1"):
    v1 = tf.get_variable("v1", [1])
with tf.variable_scope("foo2"):
    v2 = tf.get_variable("v2", [1])

aaa = tf.get_collection_ref(tf.GraphKeys.TRAINABLE_VARIABLES)
aaaa = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,'foo1')

print(aaa == aaaa) # False
print(aaa is aaaa) # False

since the scope is specified, aaa and aaaa objects referred to by the variables are not equal.

Moreover, in both cases, aaa and aaaa are not pointing to the same object. Therefore, aaa is aaaa is False in both cases (Is there a difference between `==` and `is` in Python?).

Hope this helps.

Nipun Wijerathne
  • 1,839
  • 11
  • 13
0

I saw this difference:

In [24]: w = tf.Variable([1,2,3], collections=[tf.GraphKeys.WEIGHTS], dtype=tf.float32)
In [25]: params = tf.get_collection_ref(tf.GraphKeys.WEIGHTS)
In [26]: params
Out[26]: [<tf.Variable 'Variable_1:0' shape=(3,) dtype=float32_ref>]
In [27]: del params[:]
In [28]: tf.get_collection_ref(tf.GraphKeys.WEIGHTS)
Out[28]: []
In [29]: w = tf.Variable([1,2,3], collections=[tf.GraphKeys.WEIGHTS], dtype=tf.float32)
In [30]: params = tf.get_collection(tf.GraphKeys.WEIGHTS)
In [31]: params
Out[31]: [<tf.Variable 'Variable_2:0' shape=(3,) dtype=float32_ref>]
In [32]: del params[:]
In [33]: tf.get_collection_ref(tf.GraphKeys.WEIGHTS)
Out[33]: [<tf.Variable 'Variable_2:0' shape=(3,) dtype=float32_ref>]

So get_collection only returns the value of the collection, but get_collection_ref returns the reference of the collection then I can delete the collection by delete the returned variable it refers to.

And the scope parameter in the get_collection is for filtering variables by the scope name. But get_collection_ref don't offer such a function.

Lerner Zhang
  • 6,184
  • 2
  • 49
  • 66