IndexError: Target 25 is out of bounds

I have a custom custom company dataset which has 14 features and 1 output label having 6 classes [9,12,15,18,21]. I have built a linear model using following definition:

class HourPredictor(nn.Module):
    def __init__(self):
        self.fc1 = nn.Linear(in_features=14, out_features=64)
        self.fc2 = nn.Linear(in_features=64, out_features=32)
        self.output = nn.Linear(in_features=32, out_features=5)
    def forward(self, x):
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.output(x)
        return x

But when i try to train the model it is giving me this index error:

IndexError Traceback (most recent call last) File :9

File ~/.envs/.vas/lib/python3.10/site-packages/torch/nn/modules/, 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 ~/.envs/.vas/lib/python3.10/site-packages/torch/nn/modules/, 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 ~/.envs/.vas/lib/python3.10/site-packages/torch/nn/, 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 25 is out of bounds.

nn.CrossEntropyLoss expects target tensors to contain class indices in the range [0, nb_classes-1] while your target apparently contains an index of 25, which is wrong.
Map the targets to [0, nb_classes-1] and it should work.
Also, you are explaining to deal with 6 classes but the last output layer uses out_features=5 which also seems to be wrong.

It worked for me. Thank you.