IndexError: Target 25 is out of bounds, while training pytorch model

Question:

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

class HourPredictor(nn.Module):
    def __init__(self):
        super().__init__()
        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 <timed exec>:9

File ~/.envs/.vas/lib/python3.10/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 ~/.envs/.vas/lib/python3.10/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 ~/.envs/.vas/lib/python3.10/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 25 is out of bounds.

Answers:

TL;DR
You need to map your labels from [9, 12, 15, 18, 25] to [0, 1, 2, 3, 4].

Your model does not know that the five classes it predicts have "names", e.g., the first class is "9", the third is "15", and so on. It only outputs a probability over these five "buckets".
It is up to you to map the "names" into indices into the predicted class-probability vectors. These indices should be valid: in the range [0, 4].

Answered By: Shai