bing:

pred = output.data.max(1, keepdim=True)[1]


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 = output1.data.max(-1, 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?