Today I got an error message as following (In a team project source code):
IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)
I searched it on the Internet, I found it is so easy to solve. This error may be occurred frequently when using CrossEntropyLoss()
.
And the first entry of CrossEntropyLoss()
should be “Probability Distribution of Predicted Classification“. If you directly use argmax()
to input the label result, this error may occur.
Example
For example, we input the following data to CrossEntropyLoss()
function:
import torch import torch.nn as nn a = torch.tensor([1, 2, 3]) b = torch.tensor([1, 0, 1]) print(nn.CrossEntropyLoss()(a, b))
Output:
IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)
We can got this error message.
So, our variable a
should be a probability distribution. We fixed it as following:
import torch import torch.nn as nn a = torch.tensor([[-0.8, -0.2], [-0.2, -0.3], [-0.24, -0.35]]).float() b = torch.tensor([1, 0, 1]) print(nn.CrossEntropyLoss()(a, b))
Output:
tensor(0.6105)
As you can see, if variable a is a probability distribution, CrossEntropyLoss() function can be worked.
References
- https://github.com/pytorch/pytorch/issues/5554
- https://github.com/huggingface/transformers/issues/628
- https://discuss.pytorch.org/t/indexerror-dimension-out-of-range-expected-to-be-in-range-of-1-0-but-got-1/54267
Read More
- [Solved][PyTorch] TypeError: not a sequence
- [Solved][PyTorch] ValueError: expected sequence of length 300 at dim 1 (got 3)
- [Solved][PyTorch] AttributeError: ‘tuple’ object has no attribute ‘size’
- [Solved][PyTorch] RuntimeError: bool value of Tensor with more than one value is ambiguous
- [Solved][PyTorch] RuntimeError: Expected object of scalar type Float but got scalar type Long for argument
- [Solved][PyTorch] LSTM RuntimeError: input must have 3 dimensions, got 2