本篇內(nèi)容主要講解“torch.nn.ModuleList有什么用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“torch.nn.ModuleList有什么用”吧!
按需定制可以根據(jù)自己的需求進(jìn)行定制,網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)構(gòu)思過程中功能建設(shè)理應(yīng)排到主要部位公司網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)的運(yùn)用實(shí)際效果公司網(wǎng)站制作網(wǎng)站建立與制做的實(shí)際意義
簡要介紹
class torch.nn.ModuleList(modules=None) Holds submodules in a list. 它可以以列表的形式來保持多個(gè)子模塊。 ModuleList can be indexed like a regular Python list, but modules it contains are properly registered, and will be visible by all Module methods. ModuleList 能夠像python列表一樣被索引訪問, 而且其中的模塊會(huì)被正確地登記注冊, 而且它保存的模塊可以被所有Module方法可見, 之所以不能直接用python列表來保存, 是因?yàn)镻yTorch需要自動(dòng)跟蹤計(jì)算圖并計(jì)算自動(dòng)梯度, 如果直接使用python列表或者python字典來保存module, 那么無法正確地自動(dòng)計(jì)算梯度. Parameters 參數(shù) modules (iterable, optional) – an iterable of modules to add 接受的參數(shù)是模塊module的可迭代類型,用來添加到ModuleList append(module) 方法 Appends a given module to the end of the list. Parameters module (nn.Module) – module to append 用來將一個(gè)module添加到列表末尾 extend(modules) 方法 Appends modules from a Python iterable to the end of the list. 從python可迭代對象向列表添加多個(gè)module Parameters 參數(shù) modules (iterable) – iterable of modules to append module的可迭代對象 insert(index, module) 方法 Insert a given module before a given index in the list. Parameters index (int) – index to insert. module (nn.Module) – module to insert 該方法就像操作python普通列表一樣,向ModuleList中指定位置插入一個(gè)module
演示代碼:
class MyModule(nn.Module):def __init__(self):super(MyModule, self).__init__()self.linears = nn.ModuleList([nn.Linear(10, 10) for i in range(10)])def forward(self, x):# ModuleList can act as an iterable, or be indexed using intsfor i, l in enumerate(self.linears):x = self.linears[i // 2](x) + l(x)return x
注意: 子模塊必須放在頂層的屬性中,而不可能放在列表或者字典中。 因?yàn)槿绻辉陧攲樱窃诹斜砘蛘咦值渲校? 那么優(yōu)化器就不能夠準(zhǔn)確定位這些子模塊, 因而也就無法優(yōu)化更新這些模塊的參數(shù)。 如果你需要一個(gè)列表或者字典來存放你的子模塊, 那么就需要使用pytorch提供的方式nn.ModuleList和nn.ModuleDict方法。 注意: 子模塊submodules必須位于頂層屬性,不能存放在列表或者字典中, 否則優(yōu)化器不能夠準(zhǔn)確定位到這些學(xué)習(xí)參數(shù),因而無法正確優(yōu)化參數(shù)。 如有必要,我們可以使用pytorch提供的 nn.ModuleList 和nn.ModuleDict來實(shí)現(xiàn)列表和字典的功能。
到此,相信大家對“torch.nn.ModuleList有什么用”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
網(wǎng)頁標(biāo)題:torch.nn.ModuleList有什么用
分享地址:http://jinyejixie.com/article46/gdgphg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、網(wǎng)站策劃、定制開發(fā)、微信公眾號、響應(yīng)式網(wǎng)站、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)