Why model.named_parameters() doesn't have conv layer?

I’m doing a NLP task use CNN, and I need to use 3 filters to deal with the sequence,like this:

class CNN_Text(nn.Module):

def __init__(self, args):
        super(CNN_Text, self).__init__()
        self.args = args
        V = args.embed_num  
        D = args.embed_dim  
        C = args.class_num  
        Ci = 1  
        Co = args.kernel_num  
        Ks = args.kernel_sizes

        self.embed = nn.Embedding(V, D, scale_grad_by_freq=True)
        self.embed.weight.requires_grad = False
        self.convs1 = [nn.Conv2d(in_channels=Ci, out_channels=Co, kernel_size=(K, D),           
                              stride=(1, 1), padding=(K // 2, 0), dilation=1, bias=True) for K in Ks]
        self.dropout = nn.Dropout(args.dropout)
        self.dropout_embed = nn.Dropout(args.dropout_embed)
        in_fea = len(Ks) * Co 
        self.fc = nn.Linear(in_features=in_fea, out_features=C, bias=True)

def forward(self, x):
       x = self.embed(x)  # (N,W,D)  
       x = self.dropout_embed(x)
       x = x.unsqueeze(1)  # (N,Ci,W,D)
       x = [F.relu(conv(x)).squeeze(3) for conv in self.convs1]
       x = [F.max_pool1d(i, i.size(2)).squeeze(2) for i in x] 
       x = torch.cat(x, 1)
       x = self.dropout(x)  # (N,len(Ks)*Co)
       logit = self.fc(x)
       return logit      

model = CNN_Text()
for name, parameters in model.named_parameters():
print(name, “:”, parameters.size())

Then there is an issue, my output of model.name_parameters() is :

                   embed.weight : torch.Size([13838, 300])
                   fc.weight : torch.Size([2, 800])
                   fc.bias : torch.Size([2])

WHY there doesn’t have my conv layers parameter?? Does this mean my convolutional parameters are not learnable when training??

1 Like

You should use nn.ModuleList to register the layers in your model.
A python list won’t register them properly.


Thank you so much !!!