Matrix multiplication in convolution

Hi,

I want to employ matrix convolution in the convolutional layer. For example, if X has the shape of (20, 200), and I want each feature map to have the shape of (20, 100). How can I design the kernel so that the backpropagation can update the weights?

Hello @chris666!

There are many ways you can take an input of (20, 200) —> (20, 100) using 1-dimensional convolution and related layers. Here are just a few:

import torch
import torch.nn.functional as F
import torch.nn as nn

x = torch.rand(20,200)

model1 = nn.Conv1d(20, 20, kernel_size=(2,), stride=(2,))
model2 = nn.Conv1d(20, 20, kernel_size=(4,), stride=(4,), padding=100)
model3 = nn.Conv1d(20, 20, kernel_size=(101,))

print(model1(x).size())
print(model2(x).size())
print(model3(x).size())
print(F.max_pool1d(x, kernel_size=(2,), stride=(2,)).size())
print(F.avg_pool1d(x, kernel_size=(2,), stride=(2,)).size())

torch.Size([20, 100])
torch.Size([20, 100])
torch.Size([20, 100])
torch.Size([20, 100])
torch.Size([20, 100])

Hi Johnson,

Thanks for your reply.

It seems I didn’t explain it clearly. I want a matrix as the kernel, like (20, ). So, for example, the input has 20500 (rows*columns). Suppose now we have output_channel=1. I don’t want there to be only one filter to go through all rows in the input. There should be one filter for each row, and a total of 20 filters.

Do you know how to design this? thanks.

That depends on the kernel_size, stride, padding, etc. Take, for instance, the first example, adjusted for out_channels=1:

model1 = nn.Conv1d(20, 1, kernel_size=(2,), stride=(2,), bias=False)

We can print the size of the weights with:

for param in model1.parameters():
    print(param.data.size())

torch.Size([1, 20, 2])

That’s the learnable weights portion of the layer.