ML Collections的介绍(一)
ML Collections的介紹(一)
在看一篇論文的源碼時,看到import ml_collections這行代碼,系統報錯,經過尋找之后發現并不是源碼中的一個文件,之后感覺應該是一個深度學習的安裝包,然后就在網上搜索這個包的安裝過程,卻沒有任何信息,于是自己直接嘗試了pip install ml-collections,于是就安裝成功了。今天在github上搜索了這個安裝包,潛心學習了一下。
ML Collections
ML Collections是為ML use cases而設計的一個Python Collections的一個庫。它的兩個類是ConfigDict和FrozenConfigDict,是"dict-like" 的數據結構,以下是ConfigDict、FrozenConfigDict和FieldReference的示例用法,直接上代碼吧。
Basic Usage
import ml_collectionscfg = ml_collections.ConfigDict() cfg.float_field = 12.6 #float類型 cfg.integer_field = 123 #int類型 cfg.another_integer_field = 234 #int類型 cfg.nested = ml_collections.ConfigDict() #嵌套了ml_collections.ConfigDict() cfg.nested.string_field = 'tom' #str類型print(cfg.integer_field) # 輸出結果 123. print(cfg['integer_field']) # 也輸出123.try:cfg.integer_field = 'tom' # 輸出會報錯錯誤類型是TypeError,因為這你field是整數類型 except TypeError as e:print(e)cfg.float_field = 12 # int類型也可以指定給float類型. cfg.nested.string_field = u'bob' # string可以儲存Unicode字符串 print(cfg)下面是這段代碼的輸出結果
123 123 Could not override field 'integer_field' (reference). tom is of type <class 'str'> but should be of type <class 'int'> #報錯的地方 #以下輸出是所有的cfg的結果,以及嵌套的nested another_integer_field: 234 float_field: 12.0 integer_field: 123 nested:string_field: bobFrozenConfigDict
不可以改變的ConfigDict
import ml_collections #初始化一個字典 initial_dictionary = {'int': 1,'list': [1, 2],'tuple': (1, 2, 3),'set': {1, 2, 3, 4},'dict_tuple_list': {'tuple_list': ([1, 2], 3)} }cfg = ml_collections.ConfigDict(initial_dictionary)#把這個字典通過ConfigDict賦值給cfg frozen_dict = ml_collections.FrozenConfigDict(initial_dictionary)#把這個字典通過FrozenConfigDict賦值給frozen_dictprint(frozen_dict.tuple) # 輸出(1, 2, 3) print(frozen_dict.list) # 輸出 (1, 2) print(frozen_dict.set) # 輸出 {1, 2, 3, 4} print(frozen_dict.dict_tuple_list.tuple_list[0]) # 輸出 (1, 2)frozen_cfg = ml_collections.FrozenConfigDict(cfg)#將cfg變成Forzen類型,即不可再改變其中常量的值 print(frozen_cfg == frozen_dict) # True print(hash(frozen_cfg) == hash(frozen_dict)) # Truetry:frozen_dict.int = 2 # 會報錯,因為FrozenConfigDict是不可以改變其中的值的 except AttributeError as e:print(e)# 在`FrozenConfigDict` 與 `ConfigDict`之間進行轉換: thawed_frozen_cfg = ml_collections.ConfigDict(frozen_dict) #將frozen_dict轉化為ConfigDict print(thawed_frozen_cfg == cfg) # True frozen_cfg_to_cfg = frozen_dict.as_configdict()#將frozen_dict通過as_configdict方法轉化為ConfigDict print(frozen_cfg_to_cfg == cfg) # True以上代碼輸出結果:
(1, 2, 3) (1, 2) frozenset({1, 2, 3, 4}) (1, 2) True True FrozenConfigDict is immutable. Cannot call __setattr__().#報錯的地方 True TrueFieldReference
FieldReference有助于讓多個字段使用相同的值。它也可以用于lazy computation。
可以使用placeholder()方法來創建具有None默認值的FieldReference(field)。當程序使用可選的配置字段時,這將非常有用。
輸出結果:
Could not override field 'optional' (reference). tom is of type <class 'str'> but should be of type <class 'int'> #報錯的地方 nested:placeholder: 1 optional: 1555 placeholder: 1請注意,如果通過ConfigDict進行訪問,FieldReferences提供的間接尋址將丟失。
import ml_collectionsplaceholder = ml_collections.FieldReference(0) cfg.field1 = placeholder print(cfg.field1)#輸出為0 cfg.field2 = placeholder# 此字段將被tied到cfg.field1字段 print(cfg.field2)#輸出為0 cfg.field3 = cfg.field1 # 這只是一個初始化為0的int字段 print(cfg.field3)#輸出為0以上代碼的輸出結果:
0 0 0Lazy computation
在標準操作(加法、減法、乘法等)中使用FieldReference將返回另一個指向原始值的FieldReference。你可以用字段引用get()方法執行操作并獲取引用的計算值,以及字段引用set()方法更改原始引用的值
import ml_collectionsref = ml_collections.FieldReference(1) print("ref:",ref.get()) # 輸出為 1,通過get()方法獲取ref的值add_ten = ref.get() + 10 # ref.get()是一個整數,所以加法也是整數相加 add_ten_lazy = ref + 10 # add_ten_lazy是FieldReference,并不是一個整數print("add_ten:",add_ten) # 輸出為 11 print("add_ten_lazy:",add_ten_lazy.get()) # 輸出為11,因為ref的值是1# Addition is lazily computed for FieldReferences so changing ref will change # the value that is used to compute add_ten. ref.set(5) #更改ref的值為5 print("ref_:",ref.get()) #輸出為5 print("add_ten_:",add_ten) # 輸出為 11 print("add_ten_lazy_",add_ten_lazy.get()) # 輸出 15因為此時ref的值為5以上代碼的輸出:
ref: 1 add_ten: 11 add_ten_lazy: 11 ref_ 5 add_ten_: 11 add_ten_lazy_ 15如果FieldReference的原始值為None,或者任何操作的參數為None,則lazy computation的結果將為None。
我們也可以在lazy computation中使用ConfigDict中的字段。在這種情況下,只有ConfigDict.get_ref()方法用于獲取它
以上代碼的輸出結果
5.0 7.5 7.0 10.5Changing lazily computed values
ConfigDict中lazily computed的值可以與常規值相同的方式進行重新賦值。對用于lazily computed的FieldReference的之前的值將舍棄,并且之后的所有計算都將使用新值
以上代碼的結果
1 11 21 121 1 11 30 130Cycles
不能使用references創建循環。(這里沒有看太明白,希望有大佬可以指點一下)
“You cannot create cycles using references. Fortunately the only way to create a cycle is by assigning a computed field to one that is not the result of computation. This is forbidden:”這段話是github的原始解釋。
以上代碼輸出結果:
11 Found cycle in reference graph.#引發的錯誤后面還有一半的內容,下次再來填坑。第一次寫博客,也是自己學習之路的一個記錄吧,錯誤之處,希望可以指點一二。
總結
以上是生活随笔為你收集整理的ML Collections的介绍(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RACV2022观点集锦 | 视觉基础模
- 下一篇: java socket 聊天室_java