How can we make spatial separable conv without nn.conv2d?

I having trouble with making something like spatial separable convolution.

I have tensor parameter with [64, 16, 3, 1] and its transposed [64, 16, 1, 3]. What I want is to make [64, 16, 3, 3] tensor parameter by matrix multiplication of both tensors.

I know that it can be done with [1 x 3], [3 x 1] convolution if the input and output # of channel are same. But if not as in my case, it is impossible to use this method.

How i can make it??

I assume your parameters are conv weights.
Based on the shape you provided, both weights have the same number of input and output channels.
Have a look at this example:

conv = nn.Conv2d(in_channels=16, out_channels=64, kernel_size=3)
print(conv.weight.shape)
> torch.Size([64, 16, 3, 3])

So the difference is the kernel size. While one has a kernel of [3, 1], the other one is the transposed version of it.

Would this code work for you?

a = torch.ones(64, 16, 1, 3)
b = torch.ones(64, 16, 3, 1)

c = torch.matmul(b, a)
print(c.shape)
> torch.Size([64, 16, 3, 3])
1 Like

Thanks a lot.

I forgot to use โ€œmatmulโ€

I will try it again!!