Hello, am having a similar error ,
Am doing Phase retrieval in digital holography but the error below is messing me up. any help will be greatly appreciated.
Here is CN network, and am using a batch size of 2.
class Network(nn.Module):
def init(self, in_channels=2 ):
     super(Network,self).__init__()

     self.Conv1 =nn.Sequential(
            nn.Conv2d(in_channels,16, 3,1,1),nn.ReLU(),
            nn.MaxPool2d(3,1,1),
            nn.Conv2d(16,2, 3),nn.ReLU())
     
     self.Conv2 = nn.Sequential(
            nn.Conv2d(in_channels, 16,3,1,1),nn.ReLU(),
			nn.MaxPool2d(3,1,1),
            nn.Conv2d(16, 4, 3),nn.ReLU())

     self.Conv3 =  nn.Sequential(
	        nn.Conv2d(in_channels, 16,3,1,1),nn.ReLU(),
		    nn.MaxPool2d(3,1,1),
            nn.Conv2d(16, 8, 3),nn.ReLU())
     
     self.Conv4 =nn.Sequential(
	        nn.Conv2d(in_channels, 16, 3,1,1),nn.ReLU(),
			nn.MaxPool2d(3,1,1),
            nn.Conv2d(16,16, 3),nn.ReLU())
           

     self.Upsample1 = nn.Sequential(
              nn.Upsample(size=(256, 256), scale_factor=None,align_corners=True, mode="bilinear"),
              nn.Conv2d(2, 4, 3),nn.ReLU(),
              
              nn.Upsample(size=(256, 256), scale_factor=None,align_corners=True, mode='bilinear'),
              nn.Conv2d(4, 8, 3),nn.ReLU(),
              
              nn.Upsample(size=(256, 256), scale_factor=None,align_corners=True, mode='bilinear'),
              nn.Conv2d(8, 16, 3),nn.ReLU())

     self.Upsample2 = nn.Sequential(
              nn.Upsample(size=(256, 256), scale_factor=None,align_corners=True, mode='bilinear'),
              nn.Conv2d(4, 8, 3),nn.ReLU(),
              
              nn.Upsample(size=(256, 256), scale_factor=None,align_corners=True, mode='bilinear'),
              nn.Conv2d(8, 16, 3),nn.ReLU())              
                
     self.Upsample3 = nn.Sequential(
              nn.Upsample(size=(256, 256), scale_factor=None,align_corners=True, mode='bilinear'),
              nn.Conv2d(8, 16, 3),nn.ReLU())
     
     self.Conv1a = nn.Conv2d(16, 16, 3)
     self.Conv2a = nn.Conv2d(16, 16, 3)                   
     self.Conv3a = nn.Conv2d(16, 16, 3)
     self.Conv4a = nn.Conv2d(16, 16, 3) 
     self.Conv5 =  nn.Conv2d(16, 64, 3)

def forward(self,x):
   # print(x.size)
    w = self.Conv1(x)
    w = self.Upsample1(w)
    w=  F.relu(self.Conv1a(w))

    k = self.Conv2(x)
    k = self.Upsample2(k)
    k=  F.relu(self.Conv2a(k))

    y = self.Conv3(x)
    y = self.Upsample3(y)
    y=  F.relu(self.Conv3a(y))

    z = self.Conv4(x)
    z=  F.relu(self.Conv4a(z))
    print('*****',w.shape,k.shape,y.shape,z.shape)
    
    p = torch.cat([w, k, y, z], 0)
    print(p.shape)
    q = self.Conv5(p)
    print(q.shape)


    return p

net= Network()
Below is the error am getting…
***** torch.Size([1, 16, 252, 252]) torch.Size([1, 16, 252, 252]) torch.Size([1, 16, 252, 252]) torch.Size([1, 16, 252, 252])
torch.Size([4, 16, 252, 252])
torch.Size([4, 64, 250, 250])
output_shape torch.Size([4, 16, 252, 252])
Traceback (most recent call last):
File “E:\DMU\pytnewCNN.ipy”, line 201, in 
loss =criterion(outputs, labels)
File “C:\Users\Hp\anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 550, in call
result = self.forward(*input, **kwargs)
File “C:\Users\Hp\anaconda3\lib\site-packages\torch\nn\modules\loss.py”, line 932, in forward
ignore_index=self.ignore_index, reduction=self.reduction)
File “C:\Users\Hp\anaconda3\lib\site-packages\torch\nn\functional.py”, line 2317, in cross_entropy
return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
File “C:\Users\Hp\anaconda3\lib\site-packages\torch\nn\functional.py”, line 2113, in nll_loss
.format(input.size(0), target.size(0)))
ValueError: Expected input batch_size (4) to match target batch_size (2).