亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

對(duì)Pytorch中nn.ModuleList 和 nn.Sequential詳解

 更新時(shí)間:2019年08月18日 08:58:05   作者:ustc_lijia  
今天小編就為大家分享一篇對(duì)Pytorch中nn.ModuleList 和 nn.Sequential詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

簡(jiǎn)而言之就是,nn.Sequential類似于Keras中的貫序模型,它是Module的子類,在構(gòu)建數(shù)個(gè)網(wǎng)絡(luò)層之后會(huì)自動(dòng)調(diào)用forward()方法,從而有網(wǎng)絡(luò)模型生成。而nn.ModuleList僅僅類似于pytho中的list類型,只是將一系列層裝入列表,并沒(méi)有實(shí)現(xiàn)forward()方法,因此也不會(huì)有網(wǎng)絡(luò)模型產(chǎn)生的副作用。

需要注意的是,nn.ModuleList接受的必須是subModule類型,例如:

nn.ModuleList(
      [nn.ModuleList([Conv(inp_dim + j * increase, oup_dim, 1, relu=False, bn=False) for j in range(5)]) for i in
       range(nstack)])

其中,二次嵌套的list內(nèi)部也必須額外使用一個(gè)nn.ModuleList修飾實(shí)例化,否則會(huì)無(wú)法識(shí)別類型而報(bào)錯(cuò)!

摘錄自

nn.ModuleList is just like a Python list. It was designed to store any desired number of nn.Module's. It may be useful, for instance, if you want to design a neural network whose number of layers is passed as input:

class LinearNet(nn.Module):
 def __init__(self, input_size, num_layers, layers_size, output_size):
   super(LinearNet, self).__init__()
 
   self.linears = nn.ModuleList([nn.Linear(input_size, layers_size)])
   self.linears.extend([nn.Linear(layers_size, layers_size) for i in range(1, self.num_layers-1)])
   self.linears.append(nn.Linear(layers_size, output_size)

nn.Sequential allows you to build a neural net by specifying sequentially the building blocks (nn.Module's) of that net. Here's an example:

class Flatten(nn.Module):
 def forward(self, x):
  N, C, H, W = x.size() # read in N, C, H, W
  return x.view(N, -1)
 
simple_cnn = nn.Sequential(
      nn.Conv2d(3, 32, kernel_size=7, stride=2),
      nn.ReLU(inplace=True),
      Flatten(), 
      nn.Linear(5408, 10),
     )

In nn.Sequential, the nn.Module's stored inside are connected in a cascaded way. For instance, in the example that I gave, I define a neural network that receives as input an image with 3 channels and outputs 10 neurons. That network is composed by the following blocks, in the following order: Conv2D -> ReLU -> Linear layer. Moreover, an object of type nn.Sequential has a forward() method, so if I have an input image x I can directly call y = simple_cnn(x) to obtain the scores for x. When you define an nn.Sequential you must be careful to make sure that the output size of a block matches the input size of the following block. Basically, it behaves just like a nn.Module

On the other hand, nn.ModuleList does not have a forward() method, because it does not define any neural network, that is, there is no connection between each of the nn.Module's that it stores. You may use it to store nn.Module's, just like you use Python lists to store other types of objects (integers, strings, etc). The advantage of using nn.ModuleList's instead of using conventional Python lists to store nn.Module's is that Pytorch is “aware” of the existence of the nn.Module's inside an nn.ModuleList, which is not the case for Python lists. If you want to understand exactly what I mean, just try to redefine my class LinearNet using a Python list instead of a nn.ModuleList and train it. When defining the optimizer() for that net, you'll get an error saying that your model has no parameters, because PyTorch does not see the parameters of the layers stored in a Python list. If you use a nn.ModuleList instead, you'll get no error.

以上這篇對(duì)Pytorch中nn.ModuleList 和 nn.Sequential詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python中字符串內(nèi)置函數(shù)的用法總結(jié)

    python中字符串內(nèi)置函數(shù)的用法總結(jié)

    這篇文章給大家總結(jié)了python中字符串內(nèi)置函數(shù)的用法以及相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友學(xué)習(xí)下。
    2018-09-09
  • 使用python爬取4K壁紙保存到本地文件夾的全過(guò)程

    使用python爬取4K壁紙保存到本地文件夾的全過(guò)程

    圖片信息豐富多彩,許多網(wǎng)站上都有大量精美的圖片資源,有時(shí)候我們可能需要批量下載這些圖片,而手動(dòng)一個(gè)個(gè)下載顯然效率太低,所以本文給大家介紹了使用python爬取4K壁紙保存到本地文件夾的全過(guò)程,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下
    2024-03-03
  • python通過(guò)微信發(fā)送郵件實(shí)現(xiàn)電腦關(guān)機(jī)

    python通過(guò)微信發(fā)送郵件實(shí)現(xiàn)電腦關(guān)機(jī)

    這篇文章主要為大家詳細(xì)介紹了python通過(guò)微信發(fā)送郵件實(shí)現(xiàn)電腦關(guān)機(jī),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • 在Linux系統(tǒng)上安裝Python的Scrapy框架的教程

    在Linux系統(tǒng)上安裝Python的Scrapy框架的教程

    這篇文章主要介紹了在Linux系統(tǒng)上安裝Python的Scrapy框架的教程,Scrapy是著名的專門針對(duì)搜索引擎的爬蟲(chóng)制作而研發(fā)的Python框架,需要的朋友可以參考下
    2015-06-06
  • python中pywifi的具體使用

    python中pywifi的具體使用

    本文主要介紹了python中pywifi的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • 一文教你將Visual Studio Code變成Python開(kāi)發(fā)神器

    一文教你將Visual Studio Code變成Python開(kāi)發(fā)神器

    Visual Studio Code 是一款功能強(qiáng)大、可擴(kuò)展且輕量級(jí)的代碼編輯器,經(jīng)過(guò)多年的發(fā)展,已經(jīng)成為 Python 社區(qū)的首選代碼編輯器之一。本文將為大家介紹一下如何將Visual Studio Code變成Python開(kāi)發(fā)神器,需要的可以參考一下
    2022-07-07
  • python中l(wèi)xml模塊的使用詳解

    python中l(wèi)xml模塊的使用詳解

    lxml是python的一個(gè)解析庫(kù),支持HTML和XML的解析,支持XPath解析方式,而且解析效率非常高,這篇文章主要來(lái)和大家講解一下lxml模塊的使用,感興趣的可以了解一下
    2023-08-08
  • 使用Python的判斷語(yǔ)句模擬三目運(yùn)算

    使用Python的判斷語(yǔ)句模擬三目運(yùn)算

    這篇文章主要介紹了使用Python的判斷語(yǔ)句模擬三目運(yùn)算,Python中沒(méi)有類似C語(yǔ)言那樣的三目運(yùn)算符,不過(guò)可以進(jìn)行簡(jiǎn)單地模擬實(shí)現(xiàn),需要的朋友可以參考下
    2015-04-04
  • python opencv畫(huà)局部放大圖實(shí)例教程

    python opencv畫(huà)局部放大圖實(shí)例教程

    這篇文章主要給大家介紹了關(guān)于python opencv畫(huà)局部放大圖的相關(guān)資料,獲取鼠標(biāo)的單擊相應(yīng)以及鼠標(biāo)的移動(dòng)信息,進(jìn)行放大功能的實(shí)現(xiàn),需要的朋友可以參考下
    2021-10-10
  • python 彈窗提示警告框MessageBox的實(shí)例

    python 彈窗提示警告框MessageBox的實(shí)例

    今天小編就為大家分享一篇python 彈窗提示警告框MessageBox的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-06-06

最新評(píng)論