Calculation of Convolutional Kernel in Pytorch

I have an input k_norm of shape(N X D) say 32,250
I want to design a convolutional kernel name Wc in such a way that the output of :
Wc * k_norm would give a result shape of (N X 1)

Wc = torch.nn.Conv2d(in_channels = ?, out_channels = ?, kernel_size=(?))

The formula that I’m using is :
kc_hat = Wc * k_norm

I want kc_hat shape as N X1

Hi Parul!

Could you explain your use case in a little more detail, maybe illustrating it
with some sample code (even if the sample code doesn’t do what you want)?
What goal are you trying to achieve with your specified “result shape?”

A couple of comments: Pytorch’s Conv2d take a four-dimensional tensor as
input, of shape [nBatch, channels, height, width]. (It’s permissible for
the nBatch and channels dimensions to have length one, but they still have
to be there.) Also, you apply such a convolutions, Wc = Conv2d ( stuff )
to its input using function-call notation: kc_hat = Wc (k_norm).


K. Frank

When you take in consideration stride and dilation, there are hundreds of ways you could do this. But, for the sake of simplicity, let’s just assume those are 1 and 1 respectively. In that case, you’d need a kernel size of (1, D):

import torch
import torch.nn as nn

N = 32
D = 250
batch_size = 1
channels = 1

kernel = nn.Conv2d(channels, channels, (1, D), bias = False)

x = torch.rand((batch_size, channels, N, D))