Only batches of spatial targets supported (3D tensors) but got targets of dimension: 1

Hi, I got a problem with a torch.nn.functional.cross_entropy(input, target)
Input is an output from the model.
Input.shape => torch.Size([25, 5])
target.shape => torch.Size([25]) - contains 25 targets for 5 classes, numbers starting from 0 to 4.
When I changed the input to random values using torch.randn(25, 5, requires_grad=True) it worked good.

Why this error would occur?

Could you add a print statement into the training loop and check the shape of input?
Your shapes and value ranges look correct, which also explains why the code using random tensors works.
Based on the error message, I assume the input tensor might become a 4-dimensional tensor for some reason (e.g. used in a segmentation use case) and thus the error is raised.

Thanks for your answer. I rechecked my model output, and there was a problem with dimensions. I wish you a nice day.