torch 报错 IndexError: Target -1 is out of bounds.

torch报错:

1
2
3
File "D:\anaconda3\lib\site-packages\torch\nn\functional.py", line 2220, in nll_loss
ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
IndexError: Target -1 is out of bounds.

Target N is out of bounds

其中的N是正在处理的数据输入的标签,即第几类,是一个 [0, 类数-1] 的整数,如果标签里的值超出这个范围会报错。
解决方法:把-1改成 [0, 类数-1] 之间的整数