欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

對Pytorch中nn.ModuleList 和 nn.Sequential詳解

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

簡而言之就是,nn.Sequential類似于Keras中的貫序模型,它是Module的子類,在構建數(shù)個網(wǎng)絡層之后會自動調(diào)用forward()方法,從而有網(wǎng)絡模型生成。而nn.ModuleList僅僅類似于pytho中的list類型,只是將一系列層裝入列表,并沒有實現(xiàn)forward()方法,因此也不會有網(wǎng)絡模型產(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)部也必須額外使用一個nn.ModuleList修飾實例化,否則會無法識別類型而報錯!

摘錄自

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.

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

相關文章

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

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

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

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

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

    python通過微信發(fā)送郵件實現(xiàn)電腦關機

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

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

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

    python中pywifi的具體使用

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

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

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

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

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

    使用Python的判斷語句模擬三目運算

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

    python opencv畫局部放大圖實例教程

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

    python 彈窗提示警告框MessageBox的實例

    今天小編就為大家分享一篇python 彈窗提示警告框MessageBox的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06

最新評論