Pytorch: List of layers returns 'optimizer got an empty parameter list'
I have a defined model and defined layer, I add n instances of my defined layer to a list in the init function of my model as follow:

    self.layers = []
    for i in range(len(nhid)-1):
        self.layers.append(MyLayer(nhid[i], nhid[i+1]))


but when I create optimizer by

optim.Adam(model.parameters(),lr=args.lr, weight_decay=args.weight_decay) 


it says:


  ValueError: optimizer got an empty parameter list


But when I write it for two layers as follow I got no error:

self.layer1 = MyLayer[nhid[0],nhid[1]]
self.layer2 = MyLayer[nhid[1],nhid[2]]


I solved the problem by using nn.ModuleList() as follow:

temp = []
for i in range(len(nhid)-1):
    temp.append(MyLayer(nhid[i], nhid[i+1]))
self.layers = nn.ModuleList(temp)


I also read about nn.Sequential(), but I didn't find out how to use it in a correct way.
