I have a dataset with labels that only have three values: -1, 0, 1. In other words, I have a calssification problem for three classes with -1, 0, 1 labels
Can I use this raw labels for training a CNN or I have to map them with indices between [0 to num_classes - 1] like bellow?
Since you have a multi-label classification problem, you will most certainly use the torch.nn.CrossEntropyLoss. In the documentation, it clearly states that if you want to provide class indices as labels, they should be between [0, C-1], where C is the number of classes. So yes, definitely transform your labels. One simple solution would be to simply add 1 to the labels you already have.
I’d at least say it is not recommended to use any label that is not in the range [0, C-1]. According to the documentation (see my last answer), there is an optional parameter called ignore_index, but it states:
Specifies a target value that is ignored and does not contribute to the input gradient. When size_average is True, the loss is averaged over non-ignored targets. Note that ignore_index is only applicable when the target contains class indices.
and it won’t give you an error message, but then this target does not contribute to the input gradients. I don’t think this is what you want.
So as I wrote in my last answer, you could simply add 1 to your labels. For example, say that your labels are a PyTorch tensor containing -1, 0 and 1. If you do
labels += 1
the addition will happen elementwise and your labels are in the right range. Try it for yourself.
PS: Just to make sure: You do intend to use the categorical cross entropy loss, right? If not, what is the loss you want to use?