HELP! Passing input and setting kernel weights for a Residual Block

Stieg_Nilsson: <p>I am new to PyTorch and Deep Learning. I am currently learning about residual blocks and res-nets and found following implementation:</p>
<pre><code class="lang-auto">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
</code></pre>
<p>Now, I want to pass a 3x5x5 tensor and use three 3x3x3 sized kernel weights, and I am getting the following error.</p>
<pre><code class="lang-auto">RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-35-6bab6a73e525&gt; in &lt;module&gt;()
----&gt; 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:
--&gt; 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

&lt;ipython-input-17-8ece58abdeeb&gt; in forward(self, x)
     20                 #TODO: forward
     21                 residual = x
---&gt; 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:
--&gt; 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:
--&gt; 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:
--&gt; 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) -&gt; Tensor:
--&gt; 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,
--&gt; 396                         self.padding, self.dilation, self.groups)
    397 
    398     def forward(self, input: Tensor) -&gt; 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
</code></pre>
<p>Help, what am I doing wrong here?</p>

ptrblck: <p><code>nn.*2d</code> layers expect a 4-dimensional input in the shape <code>[batch_size, channels, height, width]</code>.<br>
Based on the error message I guess the batch dimension might be missing and you can add it via:</p>
<pre><code class="lang-python">x = x.unsqueeze(0)
</code></pre>
<p>before passing it to the model.</p>