Runtime error: Expanded size error

Loud_BoomBox: <p>Hi,<br>
I am trying to run inference a custom network with following state_dict and corresponding weight and bias dimensions loaded from a pre-trained model. I have attached part of the network in which the error seems to be occurring.</p>
<pre><code class="lang-auto">.
.
.
name=encoder.original_model.classifier.weight, size=torch.Size([1000, 2208])
name=encoder.original_model.classifier.bias, size=torch.Size([1000])

name=decoder.conv2.weight, size=torch.Size([1104, 2208, 1, 1])
name=decoder.conv2.bias, size=torch.Size([1104])

name=decoder.up1.convA.weight, size=torch.Size([552, 1488, 3, 3])
name=decoder.up1.convA.bias, size=torch.Size([552])
name=decoder.up1.convB.weight, size=torch.Size([552, 552, 3, 3])
name=decoder.up1.convB.bias, size=torch.Size([552])
name=decoder.up2.convA.weight, size=torch.Size([276, 744, 3, 3])
name=decoder.up2.convA.bias, size=torch.Size([276])
name=decoder.up2.convB.weight, size=torch.Size([276, 276, 3, 3])
name=decoder.up2.convB.bias, size=torch.Size([276])
name=decoder.up3.convA.weight, size=torch.Size([138, 372, 3, 3])
name=decoder.up3.convA.bias, size=torch.Size([138])
name=decoder.up3.convB.weight, size=torch.Size([138, 138, 3, 3])
name=decoder.up3.convB.bias, size=torch.Size([138])
name=decoder.up4.convA.weight, size=torch.Size([69, 234, 3, 3])
name=decoder.up4.convA.bias, size=torch.Size([69])
name=decoder.up4.convB.weight, size=torch.Size([69, 69, 3, 3])
name=decoder.up4.convB.bias, size=torch.Size([69])
name=decoder.conv3.weight, size=torch.Size([1, 69, 3, 3])
name=decoder.conv3.bias, size=torch.Size([1])
</code></pre>
<p>I have implemented a custom convolution operation in the <code>decoder.conv2</code> layer and the implementation code snippet is attached below.</p>
<pre><code class="lang-auto">class approx_conv(torch.autograd.Function):

	@staticmethod

	def forward(ctx, in_feature, kernel, out_channel, padding, bias):

		batch_size = in_feature.size(0)
		in_channels = in_feature.size(1)
		orig_h, orig_w = in_feature.size(2), in_feature.size(3)

		kh, kw = kernel.size(2), kernel.size(3)
		dh, dw = 1, 1
		pad = padding

		out_h = (orig_h+2*pad-kh)//dh + 1
		out_w = (orig_w+2*pad-kw)//dw + 1

		img = F.pad(input= in_feature, pad= (pad, pad, pad, pad), mode='constant', value= 0)

		h, w = img.size(2), img.size(3)
		patches = img.unfold(2,kh,dh).unfold(3,kw,dw).reshape(batch_size, in_channels*kh*kw, -1)
		result = torch.zeros(batch_size, out_channel, out_h*out_w)

		for b in range(batch_size):
			x = patches[b]
			for i in range(kernel.size(0)):
				start = time.time()
				for j in range(x.size(1)):
					r=0
					for k in range(kernel.size(1)):
					
						t1 = int((kernel[i][k]*1000).round())
						t2 = int((x[k][j]*1000).round())
						
						if(t1&gt;255): t1 =255
						if(t1&lt;-255): t1 =-255
						if(t2&gt;255): t2=255
						if(t2&lt;-255): t2=-255
						
						if((t1&gt;0 and t2&gt;0) or (t1&lt;0 and t2&lt;0)):
							t1=abs(t1)
							t2=abs(t2)
							sign=1
						else:
							t1=abs(t1)
							t2=abs(t2)
							sign=-1
							
						r+= lookup_table[t1][t2]*sign
						
					result[b][i][j] = r/1000000
				end = time.time()
				print("time_taken for 300 conv in {} / 1104 = {}".format(i,end-start))
				
		result = result.reshape(batch_size, out_channel, out_h, out_w)

		if bias is not None:
			result += bias.unsqueeze(0).expand_as(result)
		
		return result
</code></pre>
<p>In this case <code>bias.shape = torch.Size([1104]) </code> and <code>result.shape = torch.Size([1,1104,15,20])</code></p>
<p>I am getting the error in the line: <code>result += bias.unsqueeze(0).expand_as(result)</code></p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "evaluate_py.py", line 47, in &lt;module&gt;
    e = evaluate(ap_model, rgb, depth, crop, batch_size=1)
  File "/gdrive/My Drive/DenseDepth_DenseNet_161/pytorch_densenet161/utils2.py", line 71, in evaluate
    pred_y = scale_up(2, predict(model, x / 255, minDepth=10, maxDepth=1000, batch_size=bs)[:, :, :, 0]) * 10.0
  File "/gdrive/My Drive/DenseDepth_DenseNet_161/pytorch_densenet161/utils2.py", line 35, in predict
    predictions = model(images)
  File "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/gdrive/My Drive/DenseDepth_DenseNet_161/pytorch_densenet161/approx_Model.py", line 61, in forward
    return self.decoder( self.encoder(x) )
  File "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/gdrive/My Drive/DenseDepth_DenseNet_161/pytorch_densenet161/approx_Model.py", line 36, in forward
    x_d0 = self.conv2(x_block4)
  File "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/gdrive/My Drive/DenseDepth_DenseNet_161/pytorch_densenet161/customconv_v2.py", line 129, in forward
    return approx_conv.apply(x, self.weight, self.out_channels, self.padding, self.bias)
  File "/gdrive/My Drive/DenseDepth_DenseNet_161/pytorch_densenet161/customconv_v2.py", line 106, in forward
    result += bias.unsqueeze(0).expand_as(result)
RuntimeError: The expanded size of the tensor (20) must match the existing size (1104) at non-singleton dimension 3.  Target sizes: [1, 1104, 15, 20].  Tensor sizes: [1, 1104]
</code></pre>
<p>I have implemented the forward() function just as suggested in the Pytorch documentation for making custom layers. Is there a workaround for this error?</p>
<p>When I reshape the result tensor in the following manner I do not get errors:<br>
<code>result = result.reshape(batch_size, out_h, out_w, out_channels)</code></p>
<p>But is the right way to go ahead?</p>
<p>TIA</p>

ptrblck: <p>The error is raised due to a mismatch in the number of dimensions.<br>
New dimensions would be added to the front of the expanded tensor, so you would have to unsqueeze the missing last dimensions:</p>
<pre><code class="lang-python">bias = torch.randn([1104]) 
result = torch.randn([1,1104,15,20])
bias[None, :, None, None].expand_as(result)
</code></pre>

Loud_BoomBox: <p>Makes sense now! Thank you</p>