Empty Parameters

Below is a sample code of something I am trying to implement :

import torch
import torch.nn as nn

class tmpNw(nn.Module):

def __init__(self):
    super(tmpNw, self).__init__()
    self.linear = []
    self.linear.append(nn.Linear(2, 3))
    self.linear.append(nn.Linear(3, 3))

def forward(self, x):
    x = self.linear[0](x)
    x = self.linear[1](x)
    return x

nw = tmpNw()
print list(nw.parameters())

optimizer = torch.optim.Adam(nw.parameters(), lr=1e-3)

It returns empty parameter, so I am unable to create the optimizer object.

For attributes to be registered as parameters, they need to be of type nn.Parameter or nn.Module. Either do

self.linear1 = nn.Linear(...)
self.linear2 = ...

or use nn.ModuleList

self.linear = nn.ModuleList([nn.Linear(...), ...])

Thanks a lot, telluri. It worked.