0

I am using the following snippet while defining the convolution method in python

I am trying to implement the convolution code which is implicitly defined by the library in python.

def conv(x, in_channels, out_channels, kernel_size, stride, padding, weight, bias):
"""
Args:
    x: torch tensor with size (N, C_in, H_in, W_in),
    in_channels: number of channels in the input image, it is C_in;
    out_channels: number of channels produced by the convolution;
    kernel_size: size of onvolving kernel, 
    stride: stride of the convolution,
    padding: implicit zero padding to be added on both sides of each dimension,
    
Return:
    y: torch tensor of size (N, C_out, H_out, W_out)
"""

y = None
N, C_in, H_in, W_in = x.shape
n_h = int((H_in - kernel_size + 2*padding)/stride) + 1
n_w = int((W_in - kernel_size + 2*padding)/stride) + 1
y = np.zeros((N, C_in, n_h, n_w))
x_pad = np.pad(array = x, pad_width = ((0,0),(padding,padding), (padding,padding), (0,0)), mode = 'constant', constant_values = 0)
for i in range(N):
  for h in range(n_h):
    for w in range(n_w):
      for c in range(C_in):
        w_start = w * stride
        w_end = w_start + kernel_size
        h_start = h * stride
        h_end = h_start + kernel_size
        conv = np.multiply(x[i, :, h_start:h_end, w_start:w_end], weight[:,c,:,:])
        y[i,c,h,w] = np.sum(conv) + bias[:,c,:,:]
return y

I am making the following method call:

conv(x,in_channels=3,
                      out_channels=6,
                      kernel_size=3,
                      stride=1,
                      padding=0,
                      weight=torch_conv.weight,
                      bias=torch_conv.bias)

Shape of x: torch.Size([2, 3, 32, 32])

Shape of weight: torch.Size([6, 3, 3, 3])

Shape of bias: torch.Size([6])

However, I am getting the following error:

RuntimeError                              Traceback (most recent call last)
<ipython-input-113-d2d90ada6b93> in <module>
      5                       padding=0,
      6                       weight=torch_conv.weight,
----> 7                       bias=torch_conv.bias)

1 frames
/usr/local/lib/python3.7/dist-packages/torch/_tensor.py in __array__(self, dtype)
    755             return handle_torch_function(Tensor.__array__, (self,), self, dtype=dtype)
    756         if dtype is None:
--> 757             return self.numpy()
    758         else:
    759             return self.numpy().astype(dtype, copy=False)

RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.

Can someone please help with this?

Steamer
  • 19
  • 1

0 Answers0