Optimizer got an empty parameter list in pytorch
I try to use convolutional network structures in a VAE experiment, the code is something like:

Class CNN(nn.Module):...

hparams1=[...]
hparams2=[...]

Model1=CNN(hyparams1)
Model2=CNN(hyparams2)

def VAE(x):
   encoder=Model1(x)
   z=reparameterize(encoder)  #reparameterize is defined
   decoder=Model2(z)
   return decoder

if args.cuda:
    Model1.cuda()
    Model2.cuda()

parameters = chain(Model1.parameters(), Model2.parameters())
optimizer = optim.Adam(parameters, lr=1e-3)

Error:Optimizer got an empty parameter list.


The same problem occurs when I defind VAE as a class:

class VAE(nn.Module):
  def encoder(x,model):
     return model(x)

  def reparameterize():

  def decoder(z,model):
     return model(z)

  def forward(x,Model1,Model2)
     output=encoder(x,Model1)
     z=reparameterize(output)
     return decoder(z,Model2)

model=VAE()
optimizer = optim.Adam(model.parameters(), lr=1e-3)

Error:Optimizer got an empty parameter list.


I guess maybe the problem is that I fail to initialize the parameters in a class VAE, but I don't know how to deal with it. Thank you very much!

In the first case, you can use:

parameters = list(Model1.parameters())+ list(Model2.parameters())
optimizer = optim.Adam(parameters, lr=1e-3)


In the second case, you didn't create the object, so basically you can try this:

model = VAE()
optimizer = optim.Adam(model.parameters(), lr=1e-3)


By the way, you can start from modifying the VAE example provided by Pytorch.

EDIT

Perhaps you miss the initial function or initialize the model in a wrong way. See the init function here.

Hope it helps.
