You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何运行PyTorch代码时出现IndexError: Target 1 is out of bounds错误?

问题排查:CrossEntropyLoss触发IndexError("Target 1 is out of bounds")

问题重现

执行以下代码时,第一段正常运行,第二段设置target_tensor = [1]会触发索引越界错误:

import torch
import torch.nn as nn

# 第一段:正常执行
input_tensor = torch.tensor([[2.0]])
target_tensor = torch.tensor([0], dtype=torch.long)
loss_function = nn.CrossEntropyLoss()
loss = loss_function(input_tensor, target_tensor)
print(loss)

# 第二段:触发错误
input_tensor = torch.tensor([[2.0]])
target_tensor = torch.tensor([1], dtype=torch.long)
loss_function = nn.CrossEntropyLoss()
loss = loss_function(input_tensor, target_tensor)
print(loss)

错误栈信息:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[214], line 14
     12 target_tensor = torch.tensor([1], dtype=torch.long)
     13 loss_function = nn.CrossEntropyLoss()
---> 14 loss = loss_function(input_tensor, target_tensor)
     15 print(loss)

File ~/jupyter-env-3.10/lib/python3.10/site-packages/torch/nn/modules/module.py:1110, in Module._call_impl(self, *input, **kwargs)
   1106 # If we don't have any hooks, we want to skip the rest of the logic in
   1107 # this function, and just call forward.
   1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1109         or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110     return forward_call(*input, **kwargs)
   1111 # Do not call functions when jit is used
   1112 full_backward_hooks, non_full_backward_hooks = [], []

File ~/jupyter-env-3.10/lib/python3.10/site-packages/torch/nn/modules/loss.py:1163, in CrossEntropyLoss.forward(self, input, target)
   1162 def forward(self, input: Tensor, target: Tensor) -> Tensor:
-> 1163     return F.cross_entropy(input, target, weight=self.weight,
   1164                            ignore_index=self.ignore_index, reduction=self.reduction,
   1165                            label_smoothing=self.label_smoothing)

File ~/jupyter-env-3.10/lib/python3.10/site-packages/torch/nn/functional.py:2996, in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction, label_smoothing)
   2994 if size_average is not None or reduce is not None:
   2995     reduction = _Reduction.legacy_get_string(size_average, reduce)
-> 2996 return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)

IndexError: Target 1 is out of bounds.

核心原因

PyTorch的CrossEntropyLoss对输入有明确要求:

  • 输入张量input的形状必须是[batch_size, num_classes],其中num_classes是分类任务的类别总数
  • 目标张量target中的每个元素必须是0到num_classes-1之间的整数,代表样本所属的类别索引

你的代码中,input_tensor = torch.tensor([[2.0]])的形状是[1,1],意味着num_classes=1,此时合法的目标索引只能是0。当你把target设为1时,超出了0~0的范围,直接触发索引越界错误。

解决方法

根据你的实际任务需求选择对应方案:

  • 如果是二分类任务:将输入张量调整为包含两个类别的预测值,比如input_tensor = torch.tensor([[2.0, 1.0]]),此时num_classes=2,target可以设为0或1。
  • 如果是单分类/二分类的另一种实现:若任务本质是判断样本是否属于某一类,可改用BCELoss(二分类交叉熵损失),注意需要在模型输出后添加sigmoid激活,同时target的取值应为0或1的浮点型张量。
  • 确认任务类别数:若确实只有1个类别,那target只能设为0,无需调整输入形状。

内容的提问来源于stack exchange,提问作者Sazzad Hissain Khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 13:24:54