最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

簡而言之就是,nn.Sequential類似于Keras中的貫序模型,它是Module的子類,在構(gòu)建數(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采集某網(wǎng)站文檔并保存word格式的示例

    Python采集某網(wǎng)站文檔并保存word格式的示例

    這篇文章主要介紹了Python采集某網(wǎng)站文檔并保存word格式的示例,我們平常需要下載文檔的時候,是不是發(fā)現(xiàn),要么不能下載,要么不能復制,那么我們今天來分享一下,如何用Python將這些不給下載的文檔給批量下載下來,需要的朋友可以參考下
    2023-07-07
  • python中通過selenium簡單操作及元素定位知識點總結(jié)

    python中通過selenium簡單操作及元素定位知識點總結(jié)

    在本篇文章里小編給大家整理的是關于python中通過selenium簡單操作及元素定位的知識點,有需要的朋友們可以學習下。
    2019-09-09
  • Python使用openpyxl實現(xiàn)Excel超鏈接批量化設置

    Python使用openpyxl實現(xiàn)Excel超鏈接批量化設置

    在Excel中,超鏈接是一種非常有用的功能,本文我們將介紹如何使用Python來處理Excel中的超鏈接,以及如何將超鏈接與對應的工作表鏈接起來,需要的可以參考一下
    2023-07-07
  • Python3+selenium實現(xiàn)cookie免密登錄的示例代碼

    Python3+selenium實現(xiàn)cookie免密登錄的示例代碼

    這篇文章主要介紹了Python3+selenium實現(xiàn)cookie免密登錄的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-03-03
  • Python神經(jīng)網(wǎng)絡TensorFlow基于CNN卷積識別手寫數(shù)字

    Python神經(jīng)網(wǎng)絡TensorFlow基于CNN卷積識別手寫數(shù)字

    這篇文章主要介紹了Python神經(jīng)網(wǎng)絡TensorFlow基于CNN卷積識別手寫數(shù)字的實現(xiàn)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-10-10
  • Python?ttkbootstrap的介紹與使用教程

    Python?ttkbootstrap的介紹與使用教程

    這篇文章主要介紹了Python?ttkbootstrap的介紹與使用,本文僅僅簡單介紹了ttkbootstrap的使用,而ttkbootstrap可以使我們創(chuàng)建一個簡單用戶圖形界面,并對其可以做一些操作,需要的朋友可以參考下
    2023-03-03
  • Python使用Matplotlib繪制散點趨勢線的代碼詳解

    Python使用Matplotlib繪制散點趨勢線的代碼詳解

    Matplotlib是一個用于數(shù)據(jù)可視化的強大Python庫,其基本功能之一是創(chuàng)建帶有趨勢線的散點圖,散點圖對于可視化變量之間的關系非常有用,本文將指導您使用Matplotlib繪制散點趨勢線的過程,涵蓋線性和多項式趨勢線,需要的朋友可以參考下
    2025-01-01
  • 使用Python編寫截圖輕量化工具

    使用Python編寫截圖輕量化工具

    這篇文章主要為大家詳細介紹了如何使用Python編寫一個截圖輕量化工具,文中的示例代碼簡潔易懂,具有一定的借鑒價值,有需要的小伙伴可以參考一下
    2025-02-02
  • Python實現(xiàn)去除代碼前行號的方法

    Python實現(xiàn)去除代碼前行號的方法

    這篇文章主要介紹了Python實現(xiàn)去除代碼前行號的方法,實例分析了Python操作字符的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03
  • python中的__init__ 、__new__、__call__小結(jié)

    python中的__init__ 、__new__、__call__小結(jié)

    這篇文章主要介紹了python中的__init__ 、__new__、__call__小結(jié),需要的朋友可以參考下
    2014-04-04

最新評論

大冶市| 介休市| 朝阳市| 勃利县| 竹北市| 义乌市| 吉水县| 五河县| 多伦县| 乌海市| 竹北市| 屏东市| 祁东县| 渭南市| 海丰县| 平顶山市| 东丰县| 清徐县| 久治县| 太白县| 昂仁县| 繁昌县| 南华县| 濉溪县| 大方县| 木里| 连州市| 江都市| 邵东县| 西乌| 江阴市| 景东| 图木舒克市| 宝兴县| 阿图什市| 云南省| 广南县| 武穴市| 武威市| 城市| 开江县|