代碼1
@attrs
class CopyTaskParams(object):
name = attrib(default="copy-task")
controller_size = attrib(default=100, convert=int)
controller_layers = attrib(default=1,convert=int)
num_heads = attrib(default=1, convert=int)
sequence_width = attrib(default=8, convert=int)
sequence_min_len = attrib(default=1,convert=int)
sequence_max_len = attrib(default=20, convert=int)
memory_n = attrib(default=128, convert=int)
memory_m = attrib(default=20, convert=int)
num_batches = attrib(default=50000, convert=int)
batch_size = attrib(default=1, convert=int)
rmsprop_lr = attrib(default=1e-4, convert=float)
rmsprop_momentum = attrib(default=0.9, convert=float)
rmsprop_alpha = attrib(default=0.95, convert=float)
解析
這段代碼是使用Python的attrs庫(kù)定義一個(gè)class,名為CopyTaskParams。這個(gè)class的對(duì)象包含了一系列的參數(shù),用于控制復(fù)制任務(wù)模型的行為。下面解釋一下每個(gè)參數(shù)的含義:
- name: str,復(fù)制任務(wù)的名稱,默認(rèn)值為"copy-task"。
- controller_size: int,控制器的大小,默認(rèn)值為100。
- controller_layers: int,控制器的層數(shù),默認(rèn)值為1。
- num_heads: int,注意力機(jī)制中頭的數(shù)量,默認(rèn)值為1。
- sequence_width: int,序列的寬度,默認(rèn)值為8。
- sequence_min_len: int,序列的最小長(zhǎng)度,默認(rèn)值為1。
- sequence_max_len: int,序列的最大長(zhǎng)度,默認(rèn)值為20。
- memory_n: int,記憶矩陣的行數(shù),默認(rèn)值為128。
- memory_m: int,記憶矩陣的列數(shù),默認(rèn)值為20。
- num_batches: int,訓(xùn)練時(shí)的批次數(shù),默認(rèn)值為50000。
- batch_size: int,每個(gè)批次的大小,默認(rèn)值為1。
- rmsprop_lr: float,RMSProp中的學(xué)習(xí)率,默認(rèn)值為1e-4。
- rmsprop_momentum: float,RMSProp中的動(dòng)量值,默認(rèn)值為0.9。
- rmsprop_alpha: float,RMSProp中的alpha值,默認(rèn)值為0.95。
這個(gè)類是使用 Python 庫(kù) attrs 中的裝飾器 attrib 定義的,該裝飾器使用方法類似于 Python 標(biāo)準(zhǔn)庫(kù)中的 property 裝飾器。attrib 裝飾器幫助我們自動(dòng)生成實(shí)例變量,init 方法以及默認(rèn)參數(shù)值等等,并提供驗(yàn)證和轉(zhuǎn)換等功能。
通過(guò)使用 attrib 裝飾器,我們可以在屬性中設(shè)置 default 參數(shù),來(lái)指定屬性的初始值。convert 參數(shù)則指定該屬性的類型轉(zhuǎn)換方法。例如,convert=int 在將其賦為整數(shù)值之前,會(huì)嘗試將其轉(zhuǎn)換為整數(shù)類型。
另外,值得注意的是,在類中未定義 str 或 repr 方法時(shí),它們將使用 attrs 幫助我們自動(dòng)生成,以便在實(shí)例被打印時(shí)很好地顯示。
總之,通過(guò)使用 attrs,有助于減少模板代碼的編寫和維護(hù)。同時(shí),它還提供了很多其他有用的功能,如比較實(shí)例,填寫數(shù)據(jù)缺失值等等。
代碼2
import attr
@attr.s
class Point:
x = attr.ib(default=0)
y = attr.ib(default=0)
p1 = Point(x=1, y=2)
p2 = attr.evolve(p1, x=3)
p1 = attr.evolve(p1, x=3)
attr.evolve 是 attrs 庫(kù)中的一個(gè)函數(shù),其作用是創(chuàng)建一個(gè)原始對(duì)象的副本,并替換其中的一些屬性值。它的函數(shù)簽名如下:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-461157.html
attr.evolve(inst, **changes)
其中 inst 是需要進(jìn)行修改的原始對(duì)象實(shí)例,changes 是一個(gè)字典,用于指定需要修改的屬性和對(duì)應(yīng)的新值。函數(shù)返回值是一個(gè)新生成的對(duì)象實(shí)例。
具體來(lái)說(shuō),changes 的鍵是需要進(jìn)行修改的屬性名,值是對(duì)應(yīng)的新值。例如,假設(shè)有一個(gè) Point 類,它用來(lái)表示二維平面上的點(diǎn)坐標(biāo)。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-461157.html
- 現(xiàn)在我們創(chuàng)建了一個(gè)名為 p1 的 Point 對(duì)象實(shí)例。
- 如果我們需要修改這個(gè)對(duì)象實(shí)例中的 x 屬性,可以使用 evolve 方法。例如,要把它的 x 屬性值從 1 改為 3。
- 這樣,p2 對(duì)象實(shí)例的 x 屬性值就被修改為了 3,而 y 屬性的值保持原來(lái)不變。
- 需要注意的是,原始對(duì)象實(shí)例 p1 的值并沒有發(fā)生改變,evolve 方法并不會(huì)修改原始對(duì)象,而是生成一個(gè)新的對(duì)象實(shí)例。如果希望將原始對(duì)象實(shí)例也進(jìn)行修改,要對(duì)p1操作。
到了這里,關(guān)于NTM中attr的用法的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!