Can I use raw three class labels with 0, 1, -1 in CNN?

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?

idx_to_label = {
    0: '0',
    1: '1',
    2: '-1'

Thanks in advance for any help.

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.

If I want to provide the exact class labels for labels, what should I do? Is it possible?

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.

So you could write

loss_function = torch.nn.CrossEntropyLoss(reduction='mean', ignore_index=-1)

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?

1 Like

I asked out of curiosity.
The actual labels of the dataset that I have are -1, 0, 1,
but these numbers make sens:

  • -1 label means decrease,
  • 1 means increase and
  • 0 means no change.