How to define a new convolution layer?

We can use torch.nn.Conv2d to create an normal convolution layer, but how to create a convolution layer with a kernel of novel shape, such as ‘T’ shape(means with kernel weight of [w1 w2 w3; 0 w4 0; 0 w5 0] )?

One way to achieve this layer is using torch.nn.Conv2d to define a 3x3 normal convolution layer firstly (named NormalLayer), and then set the corresponding position as zero in NormalLayer.weight.data before every time I use NormalLayer. But the calculated amount will equal to 3x3 normal convolution (9 points) in this way, while the true calculated amount is 5 points (w1 to w5) in ‘T’ shape kernel. Apparently, this solution is not what I want.

Other solution maybe defines a new convolution layer. Just as a 3x3 normal convolution with dilation of 2. Its kernel size is 5x5, but have calculated amount of 9 (3x3=9) points only rather than 25 (5x5=25). So, how to define a new convolution layer to define ‘T’ shape kernel with calculated amount of 5 points? Maybe use the Extending in PyTorch?

Could somebody help me? Thanks very much!

Why do you think the calculated result is of 3x3 normal convolution? Since you are setting non ‘T’ elements to 0, don’t you think the convolution only calculates 5 multiplications effectively ?

What I mention about is calculated amount. Since I using 3x3 normal convolution, the calculation amount of single kernel is 9 (3x3) absolutely, nothing but some values of the kernel are set as zero artificially.