训练ANN时出现IndexError: Target 5 is out of bounds问题求助
问题:训练ANN时出现IndexError: Target 5 is out of bounds
问题描述
训练人工神经网络时遇到以下错误:
IndexError: Target 5 is out of bounds
怀疑问题出在数据拆分代码:
from sklearn.datasets import fetch_california_housing california = fetch_california_housing() data = pd.DataFrame(california.data) data.columns = california.feature_names data['Price'] = california.target X = data.iloc[:, 0:8] y = data.iloc[:, 8]
完整错误栈:
IndexError Traceback (most recent call last) Input In [174], in <cell line: 58>() 55 plt.title("California House Prices Training Loss") 56 plt.show() ---> 58 train(classifier, optimizer, epochs, loss_fn) Input In [174], in train(classifier, optimizer, epochs, loss_fn) 43 for epoch in range(epochs): 44 out = classifier(X_train) ---> 45 loss = loss_fn(out, y_train) 46 loss.backward() 47 optimizer.step() File ~/opt/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py:1194, in Module._call_impl(self, *input, **kwargs) 1190 # If we don't have any hooks, we want to skip the rest of the logic in 1191 # this function, and just call forward. 1192 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks 1193 or _global_forward_hooks or _global_forward_pre_hooks): -> 1194 return forward_call(*input, **kwargs) 1195 # Do not call functions when jit is used 1196 full_backward_hooks, non_full_backward_hooks = [], [] File ~/opt/anaconda3/lib/python3.9/site-packages/torch/nn/modules/loss.py:1174, in CrossEntropyLoss.forward(self, input, target) 1173 def forward(self, input: Tensor, target: Tensor) -> Tensor: -> 1174 return F.cross_entropy(input, target, weight=self.weight, 1175 ignore_index=self.ignore_index, reduction=self.reduction, 1176 label_smoothing=self.label_smoothing) File ~/opt/anaconda3/lib/python3.9/site-packages/torch/nn/functional.py:3026, in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction, label_smoothing) 3024 if size_average is not None or reduce is not None: 3025 reduction = _Reduction.legacy_get_string(size_average, reduce) -> 3026 return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing) IndexError: Target 5 is out of bounds.
问题诊断
你提供的数据拆分代码没有问题,错误核心原因是:
- 加州房价数据集是回归任务(目标变量
Price是连续的房价数值),但你误用了仅适用于分类任务的CrossEntropyLoss损失函数。 CrossEntropyLoss要求目标值是类别索引(如0、1、2...),且索引值不能超过模型输出的类别数减一。而你的目标变量y是连续的房价(可能出现5左右的数值),损失函数误将其当作分类标签,该数值超出模型输出的类别范围,从而抛出索引越界错误。
修复方案
1. 替换损失函数
将分类用的CrossEntropyLoss换成回归任务专用的损失函数,比如:
# 均方误差损失(回归任务最常用) loss_fn = torch.nn.MSELoss() # 或者平均绝对误差损失 # loss_fn = torch.nn.L1Loss()
2. 调整模型输出层
回归任务的输出层不需要Softmax激活,直接输出单个连续值即可(输出维度设为1),示例模型定义:
import torch.nn as nn class HousePriceRegressor(nn.Module): def __init__(self): super().__init__() self.network = nn.Sequential( nn.Linear(8, 64), # 输入维度为8(数据集特征数) nn.ReLU(), nn.Linear(64, 32), nn.ReLU(), nn.Linear(32, 1) # 回归任务输出维度为1 ) def forward(self, x): return self.network(x)
补充说明
加州房价数据集的目标变量Price取值范围约为0.15到5万美元,属于典型的回归任务,必须匹配回归类损失函数和对应模型结构,不能混用分类任务的组件。
内容的提问来源于stack exchange,提问作者George Garman
相关产品推荐
相关产品推荐

