Hello, I’m new to PyTorch and I’m trying to custom a layer, but it didn’t work. (pytorch version 0.3.1)
Here comes the thing:
In main.py, I feed data into the net:
#net = models.CIFAR10_VGG(<Args>)
output = net(data)

In models.py, I try to form the net by nn.Sequential:
        self.comb = nn.Sequential(
            dfxp.Conv2d_q(  
                name='conv1_1',
                bits=self.bits,
                ksize=[3, 3, 3, 128],
                strides=[1, 1, 1, 1],
                padding=1,
                weight_decay=self.weight_decay
            ),
            # import from dynamic_fixed_point.py
            dfxp.ReLU_q(),
            ...
         )

In dynamic_fixed_point.py, I want to achieve a convolution layer by ‘conv2d’ function from nn.functional:
class Conv2d_func(torch.autograd.Function):
    @staticmethod
    def forward(ctx, input, weight, bias, strides, padding):
        ctx.save_for_backward(input, weight, bias)
        output = Function.conv2d(Variable(input), Variable(weight), Variable(bias),
                                 stride=strides[1], padding=1)
        return output
    
    # temporarily I want to use the default backward function

class Conv2d_q(nn.Module):
    def __init__(self, name, bits, ksize, strides, padding=0, weight_decay=0):
        super(Conv2d_q, self).__init__()
        
        self.bits = bits
        self.ksize = ksize
        self.strides = strides
        self.padding = padding
        self.weight_decay = weight_decay

        # order: (out_channels, in_channels, H, W)
        self.weight = nn.Parameter(torch.Tensor(ksize[3], ksize[0], ksize[1], ksize[2]))
        self.bias = nn.Parameter(torch.Tensor(ksize[3]))
        # initialization
        self.weight.data.uniform_(-0.1, 0.1)
        self.bias.data.uniform_(-0.1, 0.1)

    def forward(self, input):
        return Conv2d_func.apply(input, self.weight, self.bias,
                                 self.strides, self.padding)

And the error message is:
Traceback (most recent call last):
File “main.py”, line 241, in 
training_losses = train(epoch)
File “main.py”, line 161, in train
loss = optimizer.step(closure)
File “/content/halp.py”, line 139, in step
self._compute_full_grad(closure)
File “/content/halp.py”, line 116, in _compute_full_grad
closure(data, target)
File “main.py”, line 154, in closure
output = net(data)
File “/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.py”, line 357, in call
result = self.forward(*input, **kwargs)
File “/usr/local/lib/python2.7/dist-packages/torch/nn/parallel/data_parallel.py”, line 71, in forward
return self.module(*inputs[0], **kwargs[0])
File “/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.py”, line 357, in call
result = self.forward(*input, **kwargs)
File “/content/models.py”, line 125, in forward
out = self.comb(input)
File “/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.py”, line 357, in call
result = self.forward(*input, **kwargs)
File “/usr/local/lib/python2.7/dist-packages/torch/nn/modules/container.py”, line 67, in forward
input = module(input)
File “/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.py”, line 357, in call
result = self.forward(*input, **kwargs)
File “/content/dynamic_fixed_point.py”, line 39, in forward
self.strides, self.padding)
RuntimeError: data must be a Tensor
Could anyone help me out?