日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ML Collections的介绍(一)

發布時間:2024/1/8 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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: bob

FrozenConfigDict

不可以改變的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 True

FieldReference

FieldReference有助于讓多個字段使用相同的值。它也可以用于lazy computation。
可以使用placeholder()方法來創建具有None默認值的FieldReference(field)。當程序使用可選的配置字段時,這將非常有用。

import ml_collections from ml_collections.config_dict import config_dictplaceholder = ml_collections.FieldReference(0) #占位符,值為0 cfg = ml_collections.ConfigDict() cfg.placeholder = placeholder cfg.optional = config_dict.placeholder(int) cfg.nested = ml_collections.ConfigDict() cfg.nested.placeholder = placeholdertry:cfg.optional = 'tom' # 會報類型錯誤,因為是int類型 except TypeError as e:print(e)cfg.optional = 1555 cfg.placeholder = 1 # 改變placeholder and nested.placeholder 的值.print(cfg)

輸出結果:

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 0

Lazy 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()方法用于獲取它

import ml_collectionsconfig = ml_collections.ConfigDict() config.reference_field = ml_collections.FieldReference(1) config.integer_field = 2 config.float_field = 2.5# 因為我們沒有使用`get_ref()`所以沒有lazy evaluatuations config.no_lazy = config.integer_field * config.float_field #2*2.5# 這里的lazily evaluate 只能是 config.integer_field config.lazy_integer = config.get_ref('integer_field') * config.float_field # 這個lazily evaluate 只能是 config.float_field config.lazy_float = config.integer_field * config.get_ref('float_field')# 這里的lazily evaluate 既有 config.integer_field 也有 config.float_Field config.lazy_both = (config.get_ref('integer_field') *config.get_ref('float_field'))config.integer_field = 3 print(config.no_lazy) # 輸出 5.0 - 這里用了integer_field'的原始值print(config.lazy_integer) # 輸出 7.5 3*2.5 config.float_field = 3.5 print(config.lazy_float) # 輸出 7.0,2*3.5 這里的integer_field 是2 print(config.lazy_both) # 輸出 10.5 3*3.5

以上代碼的輸出結果

5.0 7.5 7.0 10.5

Changing lazily computed values
ConfigDict中lazily computed的值可以與常規值相同的方式進行重新賦值。對用于lazily computed的FieldReference的之前的值將舍棄,并且之后的所有計算都將使用新值

import ml_collectionsconfig = ml_collections.ConfigDict() config.reference = 1 config.reference_0 = config.get_ref('reference') + 10 #1+10 config.reference_1 = config.get_ref('reference') + 20 #1+20 config.reference_1_0 = config.get_ref('reference_1') + 100 #21+100print(config.reference) # Prints 1. print(config.reference_0) # Prints 11. print(config.reference_1) # Prints 21. print(config.reference_1_0) # Prints 121.config.reference_1 = 30 #此處給reference_1 賦新的值,之后的reference_1 都將是30print(config.reference) # Prints 1 (unchanged). print(config.reference_0) # Prints 11 (unchanged). print(config.reference_1) # Prints 30. print(config.reference_1_0) # Prints 130.

以上代碼的結果

1 11 21 121 1 11 30 130

Cycles
不能使用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的原始解釋。

import ml_collections from ml_collections.config_dict import config_dictconfig = ml_collections.ConfigDict() config.integer_field = 1 config.bigger_integer_field = config.get_ref('integer_field') + 10 #11 print(config.bigger_integer_field)try:# 引發一個MutabilityError,因為設置config.integer()字段會引起循環。config.integer_field = config.get_ref('bigger_integer_field') + 2 except config_dict.MutabilityError as e:print(e)

以上代碼輸出結果:

11 Found cycle in reference graph.#引發的錯誤

后面還有一半的內容,下次再來填坑。第一次寫博客,也是自己學習之路的一個記錄吧,錯誤之處,希望可以指點一二。

總結

以上是生活随笔為你收集整理的ML Collections的介绍(一)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。