Pytorch训练神经网络出错IndexError: Target 10 is out of bounds.

  • 报错信息如下:
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个输出,修改网络线性层后就好了(我是个傻子)。

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值