get_collection和get_collection_ref有什么区别? [英] What's the difference between get_collection and get_collection_ref?
问题描述
我已经检查了这两种方法的文档,但它们看起来相同,只是get_collection可以采用其他范围参数。
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?
推荐答案
我看到了这样的区别:
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>]
所以get_collection仅返回集合的值,但get_collection_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.
get_collection中的scope参数用于通过作用域名称过滤变量。但是get_collection_ref不提供这种功能。
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.
这篇关于get_collection和get_collection_ref有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!