Applying a 2D convolution kernel to each channel in Pytorch?
I have a single 2D kernel of size [3,3], and a Tensor of size [B, 64, H, W].
My question is, how can I apply the same 2D kernel to each input channel? Should I reshape/repeat the kernel? I tried to repeat my kernel as follows:
kernel = kernel.repeat((B, 64, 1, 1))

But when I apply it the tensor size changes to [1, 64, H, 1].

One way is to use grouped convolutions with one group per input channel.
Example using nn.functional.conv2d directly
# suppose kernel.shape == [3, 3] and x.shape == [B, 64, H, W]
weights = kernel[None, None, ...].repeat(64, 1, 1, 1)
y = nn.functional.conv2d(x, weights, groups=64)

or using nn.Conv2d
conv = nn.Conv2d(64, 64, 3, groups=64, bias=False)
conv.weight.data = kernel[None, None, ...].repeat(64, 1, 1, 1)
y = conv(x)

Of course you could also specify any padding, stride, or dilation that you want by including those arguments.
