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

對(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采集某網(wǎng)站文檔并保存word格式的示例

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

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

    python中通過(guò)selenium簡(jiǎn)單操作及元素定位知識(shí)點(diǎn)總結(jié)

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

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

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

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

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

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

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

    Python?ttkbootstrap的介紹與使用教程

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

    Python使用Matplotlib繪制散點(diǎn)趨勢(shì)線的代碼詳解

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

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

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

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

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

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

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

最新評(píng)論

开平市| 岗巴县| 疏勒县| 繁峙县| 伊春市| 津市市| 河北省| 肃宁县| 江山市| 社旗县| 江门市| 于都县| 桓台县| 绩溪县| 浮山县| 榆林市| 内江市| 开封县| 怀远县| 甘南县| 临夏市| 资源县| 禹州市| 遵化市| 钟山县| 涟水县| 沿河| 富民县| 永顺县| 固阳县| 绥德县| 前郭尔| 潍坊市| 永仁县| 建德市| 辽阳市| 利津县| 云和县| 璧山县| 呼图壁县| 浦城县|