Multiple torch.nn.Parameter won't show up in parameter list

class CNN(nn.Module):
    def __init__(self, vocab_size, embedding_dim, n_filters, filter_sizes, output_dim, 
                 dropout, pad_idx):
        
        super().__init__()
        ...       

        self.s = [torch.nn.Parameter(torch.arange(1, embedding_dim+1, dtype=torch.float),  requires_grad=True)]

I can’t see self.s in

for p in model.parameters():
    print(p)

but it does show up when it’s

self.s = torch.nn.Parameter(torch.arange(1, embedding_dim+1, dtype=torch.float),  requires_grad=True)

Is there another way to initialize multiple learn able parameters?

Looks like you are putting it as a list in the case where its not working.

self.s = [torch.nn.Parameter(torch.arange(1, embedding_dim+1, dtype=torch.float),  requires_grad=True)]    #The box bracket is for list initialisation.
That could be the problem

list is a variable and I think only tensors can be stored as parameters

if you do

self.s = nn.ParameterList([torch.nn.Parameter(torch.arange(1, embedding_dim+1, dtype=torch.float),  requires_grad=True)])

then it would show up

1 Like