RuntimeError: Expected 4-dimensional input for 4-dimensional weight
I have a network, in which there are 3 architectures that share the same classifier.
class VGGBlock(nn.Module):
    def __init__(self, in_channels, out_channels,batch_norm=False):

        super(VGGBlock,self).__init__()

        conv2_params = {'kernel_size': (3, 3),
                        'stride'     : (1, 1),
                        'padding'   : 1
                        }

        noop = lambda x : x

        self._batch_norm = batch_norm

        self.conv1 = nn.Conv2d(in_channels=in_channels,out_channels=out_channels , **conv2_params)
        self.bn1 = nn.BatchNorm2d(out_channels) if batch_norm else noop

        self.conv2 = nn.Conv2d(in_channels=out_channels,out_channels=out_channels, **conv2_params)
        self.bn2 = nn.BatchNorm2d(out_channels) if batch_norm else noop

        self.max_pooling = nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2))

    @property
    def batch_norm(self):
        return self._batch_norm

    def forward(self,x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = F.relu(x)

        x = self.conv2(x)
        x = self.bn2(x)
        x = F.relu(x)

        x = self.max_pooling(x)

        return x

class VGG16(nn.Module):

  def __init__(self, input_size, num_classes=1,batch_norm=False):
    super(VGG16, self).__init__()

    self.in_channels,self.in_width,self.in_height = input_size

    self.block_1 = VGGBlock(self.in_channels,64,batch_norm=batch_norm)
    self.block_2 = VGGBlock(64, 128,batch_norm=batch_norm)
    self.block_3 = VGGBlock(128, 256,batch_norm=batch_norm)
    self.block_4 = VGGBlock(256,512,batch_norm=batch_norm)

  @property
  def input_size(self):
      return self.in_channels,self.in_width,self.in_height

  def forward(self, x):

    x = self.block_1(x)
    x = self.block_2(x)
    x = self.block_3(x)
    x = self.block_4(x)

    return x

class VGG16Classifier(nn.Module):

  def __init__(self, num_classes=1,classifier = None,batch_norm=False):
    super(VGG16Classifier, self).__init__()


    self._vgg_a = VGG16((1,32,32),batch_norm=True)
    self._vgg_b = VGG16((1,32,32),batch_norm=True)
    self._vgg_star = VGG16((1,32,32),batch_norm=True)
    self.classifier = classifier

    if (self.classifier is None):
        self.classifier = nn.Sequential(
          nn.Linear(2048, 2048),
          nn.ReLU(True),
          nn.Dropout(p=0.5),
          nn.Linear(2048, 512),
          nn.ReLU(True),
          nn.Dropout(p=0.5),
          nn.Linear(512, num_classes)
        )

  def forward(self, x1,x2,x3):
      op1 = self._vgg_a(x1)
      op1 = torch.flatten(op1,1)
      op2 = self._vgg_b(x2)
      op2 = torch.flatten(op2,1)
      op3 = self._vgg_star(x3) 
      op3 = torch.flatten(op3,1)
      
      x1 = self.classifier(op1)
      x2 = self.classifier(op2)
      x3 = self.classifier(op3)

      return x1,x2,x3

model1 = VGG16((1,32,32),batch_norm=True)
model2 = VGG16((1,32,32),batch_norm=True)
model_star = VGG16((1,32,32),batch_norm=True)
model_combo = VGG16Classifier(model1,model2,model_star)

I want to traing model_combo using the following loss function:
class CombinedLoss(nn.Module):
    def __init__(self, loss_a, loss_b, loss_star, _lambda=1.0):
        super().__init__()
        self.loss_a = loss_a
        self.loss_b = loss_b
        self.loss_star = loss_star

        self.register_buffer('_lambda',torch.tensor(float(_lambda),dtype=torch.float32))


    def forward(self,y_hat,y):

        return (self.loss_a(y_hat[0],y[0]) + 
                self.loss_b(y_hat[1],y[1]) + 
                self.loss_combo(y_hat[2],y[2]) + 
                self._lambda * torch.sum(model_star.weight - torch.pow(torch.cdist(model1.weight+model2.weight), 2)))

In the training function I pass loaders, that for simplicity are loaders_a, loaders_b and again loaders_a, where loaders_a is related to the first 50% of data of MNIST and loaders_b to the latter 50% of MNIST.
def train(net, loaders, optimizer, criterion, epochs=20, dev=None, save_param=False, model_name=&quot;valerio&quot;):
      loaders_a, loaders_b, loaders_star = loaders
    # try:
      net = net.to(dev)
      #print(net)
      #summary(net,[(net.in_channels,net.in_width,net.in_height)]*2)


      criterion.to(dev)


      # Initialize history
      history_loss = {&quot;train&quot;: [], &quot;val&quot;: [], &quot;test&quot;: []}
      history_accuracy_a = {&quot;train&quot;: [], &quot;val&quot;: [], &quot;test&quot;: []}
      history_accuracy_b = {&quot;train&quot;: [], &quot;val&quot;: [], &quot;test&quot;: []}
      history_accuracy_star = {&quot;train&quot;: [], &quot;val&quot;: [], &quot;test&quot;: []}
      # Store the best val accuracy
      best_val_accuracy = 0

      # Process each epoch
      for epoch in range(epochs):
        # Initialize epoch variables
        sum_loss = {&quot;train&quot;: 0, &quot;val&quot;: 0, &quot;test&quot;: 0}
        sum_accuracy_a = {&quot;train&quot;: 0, &quot;val&quot;: 0, &quot;test&quot;: 0}
        sum_accuracy_b = {&quot;train&quot;: 0, &quot;val&quot;: 0, &quot;test&quot;: 0}
        sum_accuracy_star = {&quot;train&quot;: 0, &quot;val&quot;: 0, &quot;test&quot;: 0}

        progbar = None
        # Process each split
        for split in [&quot;train&quot;, &quot;val&quot;, &quot;test&quot;]:
          if split == &quot;train&quot;:
            net.train()
            #widgets = [
              #' [', pb.Timer(), '] ',
              #pb.Bar(),
              #' [', pb.ETA(), '] ', pb.Variable('ta','[Train Acc: {formatted_value}]')]

            #progbar = pb.ProgressBar(max_value=len(loaders_a[split]),widgets=widgets,redirect_stdout=True)

          else:
            net.eval()
          # Process each batch
          for j, ((input_a, labels_a), (input_b, labels_b), (input_s, labels_s)) in enumerate(zip(loaders_a[split], loaders_b[split], loaders_star[split])):
            labels_a = labels_a.unsqueeze(1).float()
            labels_b = labels_b.unsqueeze(1).float()
            labels_s = labels_s.unsqueeze(1).float()

            input_a = input_a.to(dev)
            labels_a = labels_a.to(dev)
            input_b = input_b.to(dev)
            labels_b = labels_b.to(dev)
            input_s = input_s.to(dev)
            labels_s = labels_s.to(dev)

            # Reset gradients
            optimizer.zero_grad()
            # Compute output
            pred = net(input_a,input_b, input_s)

            loss = criterion(pred, [labels_a, labels_b, labels_s])
            # Update loss
            sum_loss[split] += loss.item()
            # Check parameter update
            if split == &quot;train&quot;:
              # Compute gradients
              loss.backward()
              # Optimize
              optimizer.step()

            # Compute accuracy
            pred_labels = (pred[2] &gt;= 0.0).long()  # Binarize predictions to 0 and 1
            pred_labels_a = (pred[0] &gt;= 0.0).long()  # Binarize predictions to 0 and 1
            pred_labels_b = (pred[1] &gt;= 0.0).long()  # Binarize predictions to 0 and 1


            batch_accuracy_star = (pred_labels == labels_s).sum().item() / len(labels_s)
            batch_accuracy_a = (pred_labels_a == labels_a).sum().item() / len(labels_a)
            batch_accuracy_b = (pred_labels_b == labels_b).sum().item() / len(labels_b)
            # Update accuracy
            sum_accuracy_star[split] += batch_accuracy_star
            sum_accuracy_a[split] += batch_accuracy_a
            sum_accuracy_b[split] += batch_accuracy_b

            #if (split=='train'):
              #progbar.update(j, ta=batch_accuracy)
              #progbar.update(j, ta=batch_accuracy_a)
              #progbar.update(j, ta=batch_accuracy_b)

        #if (progbar is not None):
          #progbar.finish()
        # Compute epoch loss/accuracy
        #for split in [&quot;train&quot;, &quot;val&quot;, &quot;test&quot;]:
          #epoch_loss = sum_loss[split] / (len(loaders_a[split])+len(loaders_b[split])) 
          #epoch_accuracy_combo = {split: sum_accuracy_combo[split] / len(loaders[split]) for split in [&quot;train&quot;, &quot;val&quot;, &quot;test&quot;]}
          #epoch_accuracy_a = sum_accuracy_a[split] / len(loaders_a[split])
          #epoch_accuracy_b = sum_accuracy_b[split] / len(loaders_b[split])
        epoch_loss = sum_loss[&quot;train&quot;] / (len(loaders_a[&quot;train&quot;])+len(loaders_b[&quot;train&quot;])+len(loaders_s[&quot;train&quot;])) 
        epoch_accuracy_a = sum_accuracy_a[&quot;train&quot;] / len(loaders_a[&quot;train&quot;])
        epoch_accuracy_b = sum_accuracy_b[&quot;train&quot;] / len(loaders_b[&quot;train&quot;])
        epoch_accuracy_star = sum_accuracy_star[&quot;train&quot;] / len(loaders_s[&quot;train&quot;]) 

        epoch_loss_val = sum_loss[&quot;val&quot;] / (len(loaders_a[&quot;val&quot;])+len(loaders_b[&quot;val&quot;])+len(loaders_s[&quot;val&quot;])) 
        epoch_accuracy_a_val = sum_accuracy_a[&quot;val&quot;] / len(loaders_a[&quot;val&quot;])
        epoch_accuracy_b_val = sum_accuracy_b[&quot;val&quot;] / len(loaders_b[&quot;val&quot;])
        epoch_accuracy_star_val = sum_accuracy_star[&quot;val&quot;] / len(loaders_s[&quot;val&quot;]) 

        epoch_loss_test = sum_loss[&quot;test&quot;] / (len(loaders_a[&quot;test&quot;])+len(loaders_b[&quot;test&quot;])+len(loaders_s[&quot;test&quot;])) 
        epoch_accuracy_a_test = sum_accuracy_a[&quot;test&quot;] / len(loaders_a[&quot;test&quot;])
        epoch_accuracy_b_test = sum_accuracy_b[&quot;test&quot;] / len(loaders_b[&quot;test&quot;])
        epoch_accuracy_star_test = sum_accuracy_star[&quot;test&quot;] / len(loaders_s[&quot;test&quot;]) 


        # Store params at the best validation accuracy
        if save_param and epoch_accuracy[&quot;val&quot;] &gt; best_val_accuracy:
          # torch.save(net.state_dict(), f&quot;{net.__class__.__name__}_best_val.pth&quot;)
          torch.save(net.state_dict(), f&quot;{model_name}_best_val.pth&quot;)
          best_val_accuracy = epoch_accuracy[&quot;val&quot;]

        # Update history
        for split in [&quot;train&quot;, &quot;val&quot;, &quot;test&quot;]:
          history_loss[split].append(epoch_loss)
          history_accuracy_a[split].append(epoch_accuracy_a)
          history_accuracy_b[split].append(epoch_accuracy_b)
          history_accuracy_star[split].append(epoch_accuracy_star)
        # Print info
        print(f&quot;Epoch {epoch + 1}:&quot;,
              f&quot;Training Loss = {epoch_loss:.4f},&quot;,)
        print(f&quot;Epoch {epoch + 1}:&quot;,
              f&quot;Training Accuracy for A = {epoch_accuracy_a:.4f},&quot;)
        print(f&quot;Epoch {epoch + 1}:&quot;,
              f&quot;Training Accuracy for B = {epoch_accuracy_b:.4f},&quot;)
        print(f&quot;Epoch {epoch + 1}:&quot;,
              f&quot;Training Accuracy for star = {epoch_accuracy_star:.4f},&quot;)
        
        print(f&quot;Epoch {epoch + 1}:&quot;,
              f&quot;Val Loss = {epoch_loss_val:.4f},&quot;,)
        print(f&quot;Epoch {epoch + 1}:&quot;,
              f&quot;Val Accuracy for A = {epoch_accuracy_a_val:.4f},&quot;)
        print(f&quot;Epoch {epoch + 1}:&quot;,
              f&quot;Val Accuracy for B = {epoch_accuracy_b_val:.4f},&quot;)
        print(f&quot;Epoch {epoch + 1}:&quot;,
              f&quot;Val Accuracy for star = {epoch_accuracy_star_val:.4f},&quot;)
        
        print(f&quot;Epoch {epoch + 1}:&quot;,
              f&quot;Test Loss = {epoch_loss_test:.4f},&quot;,)
        print(f&quot;Epoch {epoch + 1}:&quot;,
              f&quot;Test Accuracy for A = {epoch_accuracy_a_test:.4f},&quot;)
        print(f&quot;Epoch {epoch + 1}:&quot;,
              f&quot;Test Accuracy for B = {epoch_accuracy_b_test:.4f},&quot;)
        print(f&quot;Epoch {epoch + 1}:&quot;,
              f&quot;Test Accuracy for star = {epoch_accuracy_star_test:.4f},&quot;)
        print(&quot;\n&quot;)

But I got this error:
RuntimeError: Expected 4-dimensional input for 4-dimensional weight [64, 1, 3, 3], but got 2-dimensional input of size [128, 2048] instead


From your code &amp; error, I guess you're passing binary image (h, w, 1) to the network.
The issue raises in Conv2d layer, where it expects 4 dimensional input.
To rephrase - Conv2d layer expects 4-dim tensor like:
T = torch.randn(1,3,128,256)
print(T.shape)
out: torch.Size([1, 3, 128, 256])

Where:

The first dimension (number 1) is batch dimension to stack multiple tensors across this dim to perform batch operation.
Second dimension (number 3) is in_channels for Conv. It's basically number of channels of image. Standard RGB or BGR image has 3 channels.
Third dimension (number 128) is height dimension.
Fourth dimension (number 256) is width dimension.

Binary images have 1 channel dimension:
[128, 256, 1] , [Height, Width, Channels]
OR 
[128, 256], [Height, Width]

Take in consideration:
That standard Numpy image array dims have [H, W, C] shape, where torch expects Channel dimension after batch dimension, so: [B, C, H, W].
I'm not sure where channel clamping is happening, but binary image becomes 2-dim image, because there's no need for channel dimension as long as it's just one.
If you want to pass 2-dim binary image to Conv2d layer, you should unsqueeze it to 4-dim tensor.
Before: Input.shape = torch.Size[128, 2048]

Preprocess:
Tensor = Input.view(1, 1, Input.shape[0], Input.shape[1])
Tensor.shape
out: = torch.Size[1, 1, 128, 2048]

Same could be done by just unsqueezing zeroth dim two times:
Tensor = Input.unsqueeze(0).unsqueeze(0)
Tensor.shape
out: = torch.Size[1, 1, 128, 2048]

But it's messier - so I'd recommend first option.
