对Pytorch中nn.ModuleList 和 nn.Sequential详解
简而言之就是,nn.Sequential类似于Keras中的贯序模型,它是Module的子类,在构建数个网络层之后会自动调用forward()方法,从而有网络模型生成。而nn.ModuleList仅仅类似于pytho中的list类型,只是将一系列层装入列表,并没有实现forward()方法,因此也不会有网络模型产生的副作用。
需要注意的是,nn.ModuleList接受的必须是subModule类型,例如:
nn.ModuleList( [nn.ModuleList([Conv(inp_dim+j*increase,oup_dim,1,relu=False,bn=False)forjinrange(5)])foriin range(nstack)])
其中,二次嵌套的list内部也必须额外使用一个nn.ModuleList修饰实例化,否则会无法识别类型而报错!
摘录自
nn.ModuleListisjustlikeaPythonlist.Itwasdesignedtostoreanydesirednumberofnn.Module's.Itmaybeuseful,forinstance,ifyouwanttodesignaneuralnetworkwhosenumberoflayersispassedasinput:
classLinearNet(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)foriinrange(1,self.num_layers-1)]) self.linears.append(nn.Linear(layers_size,output_size)
nn.Sequentialallowsyoutobuildaneuralnetbyspecifyingsequentiallythebuildingblocks(nn.Module's)ofthatnet.Here'sanexample:
classFlatten(nn.Module): defforward(self,x): N,C,H,W=x.size()#readinN,C,H,W returnx.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), )
Innn.Sequential,thenn.Module'sstoredinsideareconnectedinacascadedway.Forinstance,intheexamplethatIgave,Idefineaneuralnetworkthatreceivesasinputanimagewith3channelsandoutputs10neurons.Thatnetworkiscomposedbythefollowingblocks,inthefollowingorder:Conv2D->ReLU->Linearlayer.Moreover,anobjectoftypenn.Sequentialhasaforward()method,soifIhaveaninputimagexIcandirectlycally=simple_cnn(x)toobtainthescoresforx.Whenyoudefineannn.Sequentialyoumustbecarefultomakesurethattheoutputsizeofablockmatchestheinputsizeofthefollowingblock.Basically,itbehavesjustlikeann.Module
Ontheotherhand,nn.ModuleListdoesnothaveaforward()method,becauseitdoesnotdefineanyneuralnetwork,thatis,thereisnoconnectionbetweeneachofthenn.Module'sthatitstores.Youmayuseittostorenn.Module's,justlikeyouusePythonliststostoreothertypesofobjects(integers,strings,etc).Theadvantageofusingnn.ModuleList'sinsteadofusingconventionalPythonliststostorenn.Module'sisthatPytorchis“aware”oftheexistenceofthenn.Module'sinsideannn.ModuleList,whichisnotthecaseforPythonlists.IfyouwanttounderstandexactlywhatImean,justtrytoredefinemyclassLinearNetusingaPythonlistinsteadofann.ModuleListandtrainit.Whendefiningtheoptimizer()forthatnet,you'llgetanerrorsayingthatyourmodelhasnoparameters,becausePyTorchdoesnotseetheparametersofthelayersstoredinaPythonlist.Ifyouuseann.ModuleListinstead,you'llgetnoerror.
以上这篇对Pytorch中nn.ModuleList和nn.Sequential详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。