I am new to PyTorch and Deep Learning. I am currently learning about residual blocks and res-nets and found following implementation:
class BasicBlock(nn.Module):
def __init__(self, channel_num):
super(BasicBlock, self).__init__()
self.conv_block1 = nn.Sequential(
nn.Conv2d(channel_num, channel_num, 3, padding=1),
nn.BatchNorm2d(channel_num),
nn.ReLU(),
)
self.conv_block2 = nn.Sequential(
nn.Conv2d(channel_num, channel_num, 3, padding=1),
nn.BatchNorm2d(channel_num),
)
self.relu = nn.ReLU()
def forward(self, x):
residual = x
x = self.conv_block1(x)
x = self.conv_block2(x)
x = x + residual
out = self.relu(x)
return out
Now, I want to pass a 3x5x5 tensor and use three 3x3x3 sized kernel weights, and I am getting the following error.
RuntimeError Traceback (most recent call last)
<ipython-input-35-6bab6a73e525> in <module>()
----> 1 output_res = model_res(x)
6 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
887 result = self._slow_forward(*input, **kwargs)
888 else:
--> 889 result = self.forward(*input, **kwargs)
890 for hook in itertools.chain(
891 _global_forward_hooks.values(),
<ipython-input-17-8ece58abdeeb> in forward(self, x)
20 #TODO: forward
21 residual = x
---> 22 x = self.conv_block1(x)
23 x = self.conv_block2(x)
24 x = x + residual
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
887 result = self._slow_forward(*input, **kwargs)
888 else:
--> 889 result = self.forward(*input, **kwargs)
890 for hook in itertools.chain(
891 _global_forward_hooks.values(),
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/container.py in forward(self, input)
117 def forward(self, input):
118 for module in self:
--> 119 input = module(input)
120 return input
121
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
887 result = self._slow_forward(*input, **kwargs)
888 else:
--> 889 result = self.forward(*input, **kwargs)
890 for hook in itertools.chain(
891 _global_forward_hooks.values(),
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py in forward(self, input)
397
398 def forward(self, input: Tensor) -> Tensor:
--> 399 return self._conv_forward(input, self.weight, self.bias)
400
401 class Conv3d(_ConvNd):
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)
394 _pair(0), self.dilation, self.groups)
395 return F.conv2d(input, weight, bias, self.stride,
--> 396 self.padding, self.dilation, self.groups)
397
398 def forward(self, input: Tensor) -> Tensor:
RuntimeError: Expected 4-dimensional input for 4-dimensional weight [5, 5, 3, 3], but got 3-dimensional input of size [3, 5, 5] instead
Help, what am I doing wrong here?