1. 程式人生 > >tensorflow學習筆記(二十八):collection tensorflow學習筆記(二十八):collection

tensorflow學習筆記(二十八):collection tensorflow學習筆記(二十八):collection

tensorflow學習筆記(二十八):collection
2016年12月27日 11:53:06 閱讀數:11346

tensorflow collection

tensorflowcollection提供一個全域性的儲存機制,不會受到變數名生存空間的影響。一處儲存,到處可取。

介面介紹

#向collection中存資料
tf.Graph.add_to_collection(name, value)

#Stores value in the collection with the given name.
#Note that collections are not sets, so it is possible to add a value to a collection #several times. # 注意,一個‘name’下,可以存很多值; add_to_collection("haha", [a,b]),這種情況下 #tf.get_collection("haha")獲得的是 [[a,b]], 並不是[a,b] tf.add_to_collection(name, value) #這個和上面函式功能上沒有區別,區別是,這個函式是給預設圖使用的
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 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.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

思考

tf自己也維護一些collection,就像我們定義的所有summary op都會儲存在name=tf.GraphKeys.SUMMARIES。這樣,tf.get_collection(tf.GraphKeys.SUMMARIES)就會返回所有定義的summary op

參考資料 
https://www.tensorflow.org/api_docs/python/framework/core_graph_data_structures#Graph.add_to_collection

個人分類:  tensorflow