Model parameter is not included in nn.parameters() depsite added using nn.Parameter()

Hi, I have faced some problem that despite added new parameters using nn.Parameter() it is not added into the list of nn.parameters().
I have simplified my code as below:

class CoNet(nn.Module):

    def __init__(self):
        super(CoNet, self).__init__()
        self.std = 0.01
        self.layers = [64, 32, 16, 8]
        self.class_size = 2
        self.initialize_variables()
    
    def initialize_variables(self):
        #weights and biases: apps
        self.weights_apps = {}
        self.biases_apps = {}

        for l in range(len(self.layers) - 1):
            self.weights_apps[l] = nn.Parameter(torch.normal(mean=0, std=self.std, size=(self.layers[l], self.layers[l+1]), requires_grad=True))
            self.biases_apps[l] = nn.Parameter(torch.normal(mean=0, std=self.std, size=(self.layers[l+1],), requires_grad=True))
        self.W_apps = nn.Parameter(torch.normal(mean=0, std=self.std, size=(self.layers[-1], self.class_size), requires_grad=True))
        self.b_apps = nn.Parameter(torch.normal(mean=0, std=self.std, size=(self.class_size,), requires_grad=True))

model = CoNet()
list(model.parameters())
[Parameter containing:
 tensor([[-0.0046,  0.0010],
         [-0.0066, -0.0176],
         [ 0.0054,  0.0118],
         [-0.0128, -0.0059],
         [ 0.0155,  0.0053],
         [-0.0106, -0.0087],
         [ 0.0029,  0.0146],
         [-0.0236, -0.0152]], requires_grad=True), Parameter containing:
 tensor([0.0284, 0.0035], requires_grad=True)]

The output is basically the last two parameters added namely: self.W_apps and self.b_apps.
Is there any reason why only these two are added?

Help please, thanks!

Try using nn.ParameterList instead of the {}

1 Like

yes it works, thank you!