Trying to apply custom kernels in convolution. Getting size errors

Hi there,

I want to add one convolutional layer within my network with my own, custom weights. The remaining convolutional layers are not custom, and generated with Conv2d. The custom weights are generated with nn.functional.conv2d.

Basically, my data will have 64 channels when it enters my custom convolutions. I have 8 3x3 kernels I want to apply to EACH of the 64 channels, so that my out_channels is 512, but I’ve been having issues with the tensor size.

def tile(self, a, dim, n_tile):
        init_dim = a.size(dim)
        repeat_idx = [1] * a.dim()
        repeat_idx[dim] = n_tile
        a = a.repeat(*(repeat_idx))
        order_index = torch.LongTensor(np.concatenate([init_dim * np.arange(n_tile) + i for i in range(init_dim)]))
        return torch.index_select(a, dim, order_index)

weights = torch.tensor([[
                        [[1, 0., -1.],
                         [1., 0., -1.],
                         [1., 0., -1.]],
                        [[1., 1., 1.],
                         [0., 0., 0.],
                         [-1., -1., -1.]],
                        [[1., 1., 1.],
                         [-2., -2., -2.],
                         [1., 1., 1.]],
                        [[0., 0., 0.],
                         [0.,1., 0.],
                         [0., 0., 0.]],
                        [[1., 0., -1.],
                         [-2., 0., 2.],
                         [1., 0., -1.]],
                        [[1., 0., -1.],
                         [-2., 0., 2.],
                         [1., 0., -1.]],
                         [[1., -2., 1.],
                         [0., 0., 0.],
                         [-1., 2., 1.]],
                        [[1., -2., 1.],
                         [-2., 4., -2.],
                         [1., -2., 1.]],
                         ]])
        weights = self.tile(weights,0,64)
        weights = weights.view(64,8,3,3)
        weights = weights.double()
        print(weights)
        print(weights.size())

A snippet of my forward function looks like this:

encode2 = self.batch2(func.relu(self.conv2(x_in)))
        encode3 = self.batch3(torch.tanh(self.conv3(encode2)))
        encode4 = torch.tanh(func.conv2d(input=encode3, weight=weights, stride=2, groups=64))
        encode5 = self.batch6(torch.tanh(self.conv5(encode4)))

I am getting the following output:

torch.Size([64, 8, 3, 3]) # size of weights 

ERROR message:
encode4 = torch.tanh(func.conv2d(input=encode3, weight=weights, stride=2, groups=64))
RuntimeError: Given groups=64, weight of size [64, 8, 3, 3], expected input[20, 64, 25, 15] to have 512 channels, but got 64 channels instead

20 is my batch size. I’ve tried a bunch of things and I’m unsure how to resolve this.