This error is most likely raised by nn.CrossEntropyLoss when you are providing wrongly shaped tensors to it.
Here is a small example:
criterion = nn.CrossEntropyLoss()
output = torch.randn(2, 3, 4, requires_grad=True)
target = torch.randint(0, 3, (2, 4))

loss = criterion(output, target) # works

target = torch.randint(0, 3, (2,))
loss = criterion(output, target) # breaks

nn.CrossEntropyLoss expects a model output in the shape [batch_size, nb_classes, *] containing logits and a target in the shape [batch_size, *] containing the class indices in [0, nb_classes-1].
Note that the * stands for additional dimensions, so for e.g. a segmentation use case these would also be valid shapes:
output: [batch_size, nb_classes, height, width]
target: [batch_size, height, width]