Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does nn.CrossEntropyLoss throw "TypeError: iteration over a 0-d tensor" when I verify inputs to be non-0-dimensional?

Tags:

pytorch

I am using PyTorch version 1.5.0. When I pass an input torch tensor of size [8,21,400,400] with a target of size [8,400,400], the program raises a TypeError: iteration over a 0-d tensor. However, the dimensions of the arguments are 4 and 3 respectively.

What could be causing this error?

The traceback points to torch\tensor.py's iter function.

Traceback (most recent call last):
  File "train.py", line 108, in <module>
    loss, accuracy = lossLayer(pred2, targetBatch)
  File "C:\Users\PC\anaconda3\lib\site-packages\torch\tensor.py", line 462, in __iter__
    raise TypeError('iteration over a 0-d tensor')
TypeError: iteration over a 0-d tensor
like image 984
ROS Avatar asked Sep 15 '25 05:09

ROS


1 Answers

You get the error because nn.CrossEntropyLoss just returns one torch.Tensor, not a pair (it doesn't return accuracy). And this tensor is 0-dimensional, i.e. one number (unless you don't override reduction argument to 'none' to get per-element loss). So when you try to assign its value to two variables loss, accuracy python tries to iterate over this tensor variable, hence the error message. Simply use loss = lossLayer(pred2, targetBatch).

like image 148
Alexander Pivovarov Avatar answered Sep 17 '25 19:09

Alexander Pivovarov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!