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

ziba: <p>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 <code>-1, 0, 1</code> labels<br>
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?</p>
<pre><code class="lang-auto">idx_to_label = {
    0: '0',
    1: '1',
    2: '-1'
}
</code></pre>
<p>Thanks in advance for any help.</p>

Imahn: <p>Since you have a multi-label classification problem, you will most certainly use the <a href="https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html" rel="noopener nofollow ugc"> <code>torch.nn.CrossEntropyLoss</code></a>. In the documentation, it clearly states that if you want to provide class indices as labels, they should be between <code>[0, C-1]</code>, where <code>C</code> is the number of classes. So yes, definitely transform your labels. One simple solution would be to simply add <code>1</code> to the labels you already have.</p>

ziba: <aside class="quote no-group" data-username="Imahn" data-post="2" data-topic="140583">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/i/8baadc/48.png" class="avatar"> Imahn:</div>
<blockquote>
<p>if you want to provide class indices as labels,</p>
</blockquote>
</aside>
<p>If I want to provide the exact class labels for labels, what should I do? Is it possible?</p>

Imahn: <p>I’d at least say it is not recommended to use any label that is not in the range <code>[0, C-1]</code>. According to the documentation (see my last answer), there is an optional parameter called <code>ignore_index</code>, but it states:</p>
<blockquote>
<p>Specifies a target value that is ignored and does not contribute to the input gradient. When <code>size_average</code> is <code>True</code>, the loss is averaged over non-ignored targets. Note that <code>ignore_index</code> is only applicable when the target contains class indices.</p>
</blockquote>
<p>So you could write</p>
<pre data-code-wrap="python"><code class="lang-python">loss_function = torch.nn.CrossEntropyLoss(reduction='mean', ignore_index=-1)
</code></pre>
<p>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.</p>
<p>So as I wrote in my last answer, you could simply add <code>1</code> to your labels. For example, say that your labels are a PyTorch tensor containing <code>-1</code>, <code>0</code> and <code>1</code>. If you do</p>
<pre data-code-wrap="python"><code class="lang-python">labels += 1
</code></pre>
<p>the addition will happen elementwise and your labels are in the right range. Try it for yourself.</p>
<p>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?</p>

ziba: <p>I asked out of curiosity.<br>
The actual labels of the dataset that I have are -1, 0, 1,<br>
but these numbers make sens:</p>
<ul>
<li>-1 label means decrease,</li>
<li>1 means increase and</li>
<li>0 means no change.</li>
</ul>