Tensorlfow學習筆記----collection

本文來源:http://blog.csdn.net/u012436149/article/details/53894354

tensorflow  之  collection

tensorflowcollection提供一個全局的存儲機制,不會受到變量名生存空間的影響。一處保存,處處可取。python

  
 1 #向collection中存數據
 2 tf.Graph.add_to_collection(name, value)  3 
 4 #Stores value in the collection with the given name.
 5 #Note that collections are not sets, so it is possible to add a value to a collection
 6 #several times.
 7 # 注意,一個‘name’下,能夠存不少值; add_to_collection("haha", [a,b]),這種狀況下
 8 #tf.get_collection("haha")得到的是 [[a,b]], 並非[a,b]
 9 tf.add_to_collection(name, value) 10 #這個和上面函數功能上沒有區別,區別是,這個函數是給默認圖使用的
#從collection中獲取數據
tf.Graph.get_collection(name, scope=None) Returns a list of values in the collection with the given name. This is different from get_collection_ref() which always returns the actual collection list if it exists in that it returns a new list each time it is called. Args: name: The key for the collection. For example, the GraphKeys class contains many standard names for collections. scope: (Optional.) If supplied, the resulting list is filtered to include only items whose name attribute matches using re.match. Items without a name attribute are never returned if a scope is supplied and the choice or re.match means that a scope without special tokens filters by prefix. #返回re.match(r"scope", item.name)匹配成功的item, re.match(從字符串的開始匹配一個模式)
Returns: The list of values in the collection with the given name, or an empty list if no value has been added to that collection. The list contains the values in the order under which they were collected.

 

實例:函數

v4 = tf.get_variable(name='v4', shape=[1], collections=[tf.GraphKeys.GLOBAL_VARIABLES , 'positives'],initializer=tf.constant_initializer(3))  
with tf.Session() as sess:  
    sess.run(tf.global_variables_initializer())  
    print(tf.get_collection('positives'))  

>>spa

<tf.Variable 'v4:0' shape=(1,) dtype=float32_ref>]
相關文章
相關標籤/搜索