Getting model.parameters() as empty list

I know it might be duplicate of many of the existing issues already here on this forum but I wasn’t able to figure out problem with my code. I’ve reduced my intricate codebase to the following simple snippet.

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.l1 = torch.zeros(2,2)
    def forward(self):
        return self.l1

If I print model=Model(); print(list(model.parameters())) it comes out as empty list, [], for both of the cases

1 Like

You should register the model parameters as nn.Parameter. Otherwise the tensors won’t be properly registered.
Alternatively, you could call register_parameter on the tensors.

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.l1 = nn.Parameter(torch.zeros(2,2))
    def forward(self):
        return self.l1

model = Model()
list(model.parameters())
2 Likes