How to custom operations in the forward function

I am doing an experiment on images of small size as follows:

The input image has only one channel, then I apply 2 customed filters (denoted by A and B) to do the convolution with the image. The filter has the same size as the image and each of A and B have 10 channels respectively. So after the convolution we have two 1 * 10 tensors. Denote them as (a1, a2, … a10) and (b1, b2, … b10). Let w = (w1, w2, …, w10) be a trainable 1 *10 tensor. Then I want to compute the following fraction: (the numerator is the norm of weighted sum of vectors (ai, bi) and the denominator is the sum of weighted norm. The norm here is the ordinary Euclidean norm.)

I am wondering how to do this computation in the forward function and where I should claim w as trainable tensor? The template of my code is as follows:

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.filter_A = ...
        self.filter_B = ...

    def forward(self, x):
        x_A = F.conv2d(x, self.filter_A)
        x_B = F.conv2d(x, self.filter_B)

I don’t completely understand the shape information and what e.g. “we have two 1 * 10 tensors” means in this case.
However, based on your description and code snippet it seems you are not using any padding and since the filters have the same shape as the input, the result would be an elementwise multiplication and summation, wouldn’t it?
The trainable filters should be defined in the __init__, which seems to be the case in your code snippet (assuming the ... refer to `nn.Parameter(torch.tensor(…)) :wink: ).

Let me know, if I misunderstood the question.