mathematics:

Please could you explain in brief why these error occurs ?
Also what are the different case scenario these cases occurs , in simple codes?


This error is raised, if the batch sizes of the model output and target do not match.
E.g. this small code example shows the issue, where the data uses a batch size of 5, while the target uses one of 4:

nb_classes = 10
output = torch.randn(5, nb_classes, requires_grad=True) # [batch_size, nb_classes]
target = torch.randint(0, nb_classes, (4,)) # [batch_size]

criterion = nn.CrossEntropyLoss()
loss = criterion(output, target)
> ValueError: Expected input batch_size (5) to match target batch_size (4).

Check the shapes of log_ps and labels in your code and make sure they have the same batch size.