Getting runtime error: Shape [1,1,4] is invalid for input of size 12

I am trying to run inference on an image classification task for 4 images.
I am getting

File "", line 88, in accuracy test_correct += np.sum(np.squeeze(pred.eq( # noqa RuntimeError: shape '[1, 1, 4]' is invalid for input of size 12
I am quite not sure where I am doing wrong,as the same code is working fine when I am running along with training loop for training set.
Below is my code for testing -

def accuracy(model, loader, criterion, use_cuda=False):
    """Calculate Accuracy."""
    test_accuracy = 0.0
    test_loss = 0.0
    test_correct = 0.0
    test_total = 0.0
    for batch_idx, (data, target) in enumerate(loader):
        # move to GPU
        if use_cuda:
            data, target = data.cuda(), target.cuda()
        # update the average validation loss
        output = model(data).squeeze()
        output = torch.unsqueeze(output, 0)
        pred =, keepdim=True)[1]
        test_correct += np.sum(np.squeeze(pred.eq( # noqa
        test_total += data.size(0)
    test_accuracy = 100. * (test_correct/test_total)
    return None

Could you check the shapes of data, output, pred and target?
Based on the error message I guess you might accidentally view or reshape the data or an activation in a wrong way, which could result in such a shape mismatch (e.g. by flattening the activation and removing the batch dimension).

1 Like

Thank You.
Below are the shapes of data, output, pred, target

data shape: torch.Size([12, 3, 600, 600])
target shape: torch.Size([12])
output1:  torch.Size([12, 4]) # Before and after squeezing
output2:  torch.Size([1, 12, 4])
Prediction:  torch.Size([1, 1, 4])

I think, I have removed the batch dimension somewhere, I am now running with using the batch size 1 but how to make it work for any batch size

Could you try and see if this works, considering the batch size to be 18

target = torch.ones((16,12))
output1 = torch.ones((16,12,4))
output2 = torch.ones((16,1,12,4))
pred =, keepdim=False)[1]

The shapes after doing the above is

target:  torch.Size([16, 12])
output1 : torch.Size([16, 12, 4])
output2:  torch.Size([16, 1, 12, 4])
Predicion:  torch.Size([16, 12])

Now your targets and preds are in the same shape. Does this help?

1 Like

You are currently calculating the max in the batch dimension, which seems to be wrong as you would usually compute the predictions by taking the argmax in the class dimension.
This should work:

output = model(data).squeeze() # output.shape == [12, 4]
preds = torch.argmax(output, dim=1) # preds.shape == [12]

I don’t think you need to unsqueeze and squeeze the tensors to calculate the accuracy and for a multi-class classification your target should also have the shape [batch_size==12].

PS: don’t use the .data attribute as it might yield unwanted side effects.

1 Like