TypeError: torch.nn.parameter.Parameter is not a Module subclass

My model has a custom bias list like below. But it faced an error

nn.ModuleList([nn.Parameter(torch.randn(1, out_chs, 1, 1)) for _ in range(n_layers)])

-->
TypeError: torch.nn.parameter.Parameter is not a Module subclass

So I just declare as a list like this. But now it looks it is not affected by model.cuda(). So how to pass the list of nn.Parameter to gpu?

[nn.Parameter(torch.randn(1, out_chs, 1, 1)) for _ in range(n_layers)]

-->
didn't match because some of the arguments have invalid types:  │ (torch.FloatTensor)
1 Like
nn.ParameterList

see docs:

http://pytorch.org/docs/master/nn.html?highlight=parameterlist#torch.nn.ParameterList

3 Likes

Thank you! It works!

wow, so cool! Does the ParameterList is with gradient?

nn.ParameterList will make sure to register all parameters properly, so e.g. that they will be pushed to the device, if you call model.to('cuda:0').
Each nn.Parameter should require gradients by default. :wink:

the same happens also with nn.ModuleDict and should be with nn.ParameterDict() replaced

1 Like