Extend convolution function

I would like to extend torch._C._functions.ConvNd. Can someone tell me where (which files) to get started? Appreciate any help!

Basically, I would like to fix two things:
(1) Allow kernel size > input size
(2) Convolute over individual channels separately, instead of summing up the convolution results of all channels as it is now.

It’s in https://github.com/pytorch/pytorch/blob/master/torch/csrc/autograd/functions/convolution.cpp

However, extending it might be difficult. I suspect there may be easier ways to achieve your goals:

  1. Narrow the weight tensor if the input is smaller than the kernel size
  2. Use groups, and set groups=channels http://pytorch.org/docs/master/nn.html#torch.nn.Conv2d
1 Like

Following your suggestions, I tried the following and ran into the following problem. Would you please help have a look. Thanks a lot!

x = torch.nn.Conv2d(10, 10, [3, 3], groups=10) # in_channels = 10, out_channels = 10
img = torch.autograd.Variable( torch.randn(10, 5, 5) )  # in_channels = 10
o = x(img)
Traceback (most recent call last):
  File "<pyshell#47>", line 1, in <module>
    o = x(img);
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/torch/nn/modules/module.py", line 206, in __call__
    result = self.forward(*input, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/torch/nn/modules/conv.py", line 237, in forward
    self.padding, self.dilation, self.groups)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/torch/nn/functional.py", line 39, in conv2d
    return f(input, weight, bias)
RuntimeError: expected 3D tensor

The error is because your input is 3D (channels x h x w). You need a batch dimension (batch x channels x h x w), although it can be 1.

x = torch.nn.Conv2d(10, 10, [3, 3], groups=10) # in_channels = 10, out_channels = 10
img = torch.autograd.Variable( torch.randn(1, 10, 5, 5) )  # in_channels = 10
o = x(img)
1 Like

It works. Thanks a lot!