Conv2d vs F.conv2d

I swapped out Conv2d for F.conv2d in

and Conv2d works better since it’s optimized but conv2d seems to take a really long time to converge, did I miss something in the code?

class CNN(nn.Module):
    def __init__(self, vocab_size, embedding_dim, n_filters, filter_sizes, output_dim, 
                 dropout, pad_idx):
        self.embedding = nn.Embedding(vocab_size, embedding_dim, padding_idx = pad_idx)
        self.conv_0 = nn.Parameter(torch.randn((1, 
                                filter_sizes[0], embedding_dim)))
        self.conv_1 = nn.Parameter(torch.randn((1, 
                                filter_sizes[1], embedding_dim)))
        self.conv_2 = nn.Parameter(torch.randn((1, 
                                filter_sizes[2], embedding_dim)))
        self.fc = nn.Linear(len(filter_sizes) * n_filters, output_dim)
        self.dropout = nn.Dropout(dropout)
    def forward(self, text):
        #text = [sent len, batch size]
        text = text.permute(1, 0)
        #text = [batch size, sent len]
        embedded = self.embedding(text)
        #embedded = [batch size, sent len, emb dim]
        embedded = embedded.unsqueeze(1)
        #embedded = [batch size, 1, sent len, emb dim]
        conved_0 = F.relu(F.conv2d(embedded,self.conv_0).squeeze(3))
        conved_1 = F.relu(F.conv2d(embedded,self.conv_1).squeeze(3))
        conved_2 = F.relu(F.conv2d(embedded,self.conv_2).squeeze(3))
        #conved_n = [batch size, n_filters, sent len - filter_sizes[n] + 1]
        pooled_0 = F.max_pool1d(conved_0, conved_0.shape[2]).squeeze(2)
        pooled_1 = F.max_pool1d(conved_1, conved_1.shape[2]).squeeze(2)
        pooled_2 = F.max_pool1d(conved_2, conved_2.shape[2]).squeeze(2)
        #pooled_n = [batch size, n_filters]
        cat = self.dropout(, pooled_1, pooled_2), dim = 1))

        #cat = [batch size, n_filters * len(filter_sizes)]
        return self.fc(cat)

Problem could be with random initialization of convolution parameters, resulting in slower convergence. Try initializing nn.Parameter with kaiming_uniform_.

Your version is missing biases as well that are enabled by default with regular Convs.

1 Like