- 报错信息如下:
Traceback (most recent call last):
File "F:\Anaconda3\envs\Desktop\lib\site-packages\IPython\core\interactiveshell.py", line 3417, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-e94b7ec388b6>", line 1, in <module>
runfile('E:/My Research Subject/Threat Assessment/NetWorkTest/GroupTA/CNN/CNN.py', wdir='E:/My Research Subject/Threat Assessment/NetWorkTest/GroupTA/CNN')
File "F:\Pycharm\PyCharm Community Edition 2021.2.1\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_umd.py", line 198, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "F:\Pycharm\PyCharm Community Edition 2021.2.1\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "E:/My Research Subject/Threat Assessment/NetWorkTest/GroupTA/CNN/CNN.py", line 156, in <module>
train()
File "E:/My Research Subject/Threat Assessment/NetWorkTest/GroupTA/CNN/CNN.py", line 107, in train
loss = criterion(outputs, target.long())
File "F:\Anaconda3\envs\Desktop\lib\site-packages\torch\nn\modules\module.py", line 889, in _call_impl
result = self.forward(*input, **kwargs)
File "F:\Anaconda3\envs\Desktop\lib\site-packages\torch\nn\modules\loss.py", line 1048, in forward
ignore_index=self.ignore_index, reduction=self.reduction)
File "F:\Anaconda3\envs\Desktop\lib\site-packages\torch\nn\functional.py", line 2693, in cross_entropy
return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
File "F:\Anaconda3\envs\Desktop\lib\site-packages\torch\nn\functional.py", line 2388, in nll_loss
ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
IndexError: Target 10 is out of bounds.
- 原因分析:
最后一行提醒是IndexError: Target 10 is out of bounds.
,感觉很奇怪,计算loss的时候怎么会出现索引异常呢?最后发现,我的数据集target一共10个类别,网络线性层最后只有4个输出,修改网络线性层后就好了(我是个傻子)。