torch.nn.Conv2d custom filters

How torch.nn.Conv2d compute the convolution matrix using its default filter.
I have a small problem to know how the calculation is performed and how to use my own filter (mask vector), and why we use unsqueeze from the model.
My code:

inputconv = torch.randn(3, 4, 4)
inputconv.unsqueeze_(0)
mm = torch.nn.Conv2d(3, 3, kernel_size=2, padding=0, stride=(2, 1))
print(inputconv)
print('Conv2d: ')
print(mm(inputconv))

output:

tensor([[[[ 0.8845, 0.2022, -0.8536, -0.5750],
[-0.6650, 0.1512, 1.4356, 0.4598],
[ 0.1666, 1.8639, 0.2500, -0.3754],
[-1.1593, 0.1265, 1.1665, -0.6877]],

     [[-0.3083, -0.7364,  1.6745, -1.6611],
      [ 0.7673, -0.9379,  0.0095, -0.4120],
      [ 0.8867,  0.0865,  0.1563, -0.0828],
      [ 0.8034, -0.6904, -0.4510, -0.6925]],

     [[-0.4779, -0.7025, -0.1098,  0.6809],
      [ 0.3011,  0.3318, -0.7675, -0.8880],
      [ 0.9923, -1.5812, -0.1318,  0.2460],
      [-0.0436, -0.1883, -0.1694,  0.3716]]]])

Conv2d:
tensor([[[[-0.1268, 0.0031, -0.3460],
[-0.0473, -0.3350, -0.1950]],

     [[ 0.1417, -0.8431, -0.4891],
      [ 0.9959, -0.2300,  0.1318]],

     [[ 0.0118, -0.4037, -0.5443],
      [ 0.3780, -0.5303, -0.3546]]]])

My question is how to custom filters using tensor variables: example tf = torch.Tensor([[1, 0, -1], [1, 0, -1], [1, 0, -1]])

Thank you all in advance!

You need to use .unsqueeze_(0) in your example, since the batch dimension is missing for inputconv.
nn.Conv2d expects an input of the shape [batch_size, channels, height, width].
.unsqueeze(0) adds an additional dimension at position 0, i.e. inputconv will have shape [1, 3, 4, 4].

You can create a custom filter kernel and apply it using the functional API.
The shape of the filter kernel should be [number_of_filters, input_channels, height, width].
Here is a small example:

conv_filter = torch.randn(1, 3, 5, 5)
x = torch.randn(1, 3, 24, 24)
output = F.conv2d(x, conv_filter, padding=2)
4 Likes

I tried with small matrix and custom conv_filter, it works fine.

conv_filter = torch.Tensor([[[[1, 0, 0]], [[0, 1, 0]], [[0, 0, 1]]]])
x = torch.randn(1, 3, 5, 4)
output = F.conv2d(x, conv_filter, padding=0)

Than you for your help

1 Like

What if instead of a random initial weigh, the filter is created using a custom function?
suppose there are two parameters(variables) that are responsible for producing an entry in the weight matrix (2 for each entry for example). Something like this .
how should we go about such cases?

1 Like