I currently encountered the error. Sample code to reproduce the error:
prediction = torch.rand((2,3,5,5))
label = torch.randint(0,2,(2,3,5,5))
criterion = nn.CrossEntropyLoss()
loss = criterion(prediction, label)
Traceback (most recent call last):
File "/home/moshood/Documents/AMMI/AMMI_Research_Project/trail-1.py", line 240, in <module>
loss = criterion(prediction, label)
File "/home/moshood/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
result = self.forward(*input, **kwargs)
File "/home/moshood/.local/lib/python3.8/site-packages/torch/nn/modules/loss.py", line 961, in forward
return F.cross_entropy(input, target, weight=self.weight,
File "/home/moshood/.local/lib/python3.8/site-packages/torch/nn/functional.py", line 2468, in cross_entropy
return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
File "/home/moshood/.local/lib/python3.8/site-packages/torch/nn/functional.py", line 2266, in nll_loss
ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
RuntimeError: only batches of spatial targets supported (3D tensors) but got targets of dimension: 4
I read posts relating to Crossentroy with the label, not one-encoded, but in my case, the label per channel are either one or zero. I will be glad for your assistance.