IndexError: Target 10 is out of bounds

vin: <p>Hello there,<br>
as commonly use, from my traning loss I use <code>nn.CrossEntropyLoss()</code>. This raised an error in this line od my code <code>loss = criterion(outputs, labels)</code>. Thus, what cause this issue? here is my labels <code>my labels are :  tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,         18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,         36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53])</code> and signal shape is <code>torch.Size([54, 2, 100, 200]</code>,<br>
54 denotes the total number of labels, so each signal size is <code>torch.Size([2, 100, 200])</code>.</p>
<p><code>Model design code:</code></p>
<pre><code class="lang-auto">self.conv1 = nn.Sequential(
            nn.Conv2d(2, 16, 3, stride=1, padding=1)
        )
        self.conv2 = nn.Sequential(
            nn.Conv2d(16, 32, 3, stride=1, padding=1)
        )
        self.conv3 = nn.Sequential(
            nn.Conv2d(32, 64, 3, stride=1, padding=1)
        )
        self.fc1 = nn.Sequential(
            nn.Linear(19200, 576)
        )
        self.fc2 = nn.Sequential(
            nn.Linear(576, 150)
        )
        self.fc3 = nn.Sequential(
            nn.Linear(150, 80)
        )
        self.fc4 = nn.Sequential(
            nn.Linear(80, 10)
        )
        self.pool = nn.Sequential(
            nn.MaxPool2d(2, 2)
        )

    def forward(self, input_data):
        input_data = self.pool(F.relu(self.conv1(input_data)))
        input_data = self.pool(F.relu(self.conv2(input_data)))
        input_data = self.pool(F.relu(self.conv3(input_data)))
        input_data = torch.flatten(input_data, 1)
        input_data = F.relu(self.fc1(input_data))
        input_data = F.relu(self.fc2(input_data))
        input_data = F.relu(self.fc3(input_data))
        input_data = self.fc4(input_data)
        return input_data
</code></pre>
<p><code>train model :</code></p>
<pre><code class="lang-auto">        for target, labels in trainloader:
            target, labels = target.to(DEVICE), labels.to(DEVICE)

            outputs = model(target.float())
            loss = criterion(outputs, labels)
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            running_loss = loss.item()
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/5/f524af420eea2018bb5630b78b127cdc4d80117a.png" data-download-href="https://discuss.pytorch.org/uploads/default/f524af420eea2018bb5630b78b127cdc4d80117a" title="777"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/5/f524af420eea2018bb5630b78b127cdc4d80117a_2_690x178.png" alt="777" data-base62-sha1="yYDzKFrqssMJXMZ2WryGBCW8eyC" width="690" height="178" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/5/f524af420eea2018bb5630b78b127cdc4d80117a_2_690x178.png, https://discuss.pytorch.org/uploads/default/optimized/3X/f/5/f524af420eea2018bb5630b78b127cdc4d80117a_2_1035x267.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/f/5/f524af420eea2018bb5630b78b127cdc4d80117a_2_1380x356.png 2x" data-dominant-color="303033"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">777</span><span class="informations">1578×409 58.3 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

InnovArul: <aside class="quote no-group" data-username="vin" data-post="1" data-topic="151868">
<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/v/a87d85/48.png" class="avatar"> vin:</div>
<blockquote>
<pre><code class="lang-auto">        self.fc4 = nn.Sequential(
            nn.Linear(80, 10)
        )
</code></pre>
</blockquote>
</aside>
<p>It is a common error. You seem to be predicting the confidence for 10 classes. It should be <code>0 &lt;= labels &lt;= 9</code>.</p>
<aside class="quote no-group" data-username="vin" data-post="1" data-topic="151868">
<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/v/a87d85/48.png" class="avatar"> vin:</div>
<blockquote>
<p><code>my labels are :  tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,         18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,         36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53])</code> and signal shape is</p>
</blockquote>
</aside>
<p>As you have mentioned, the labels are  &gt;= 10. Thus, it results in out of bounds error.</p>

vin: <p>Okay, I see. So, what can be my output? any suggestion?</p>

InnovArul: <p>It depends on the number of output classes you have. It depends on your dataset.<br>
For example, ImageNet image classification dataset has 1000 classes, HMDB action recognition dataset has 51 classes.</p>

vin: <p>If I don’t use classification, let’s say I want to self-certify my data because it’s audio signal data without any classes. In this case, is there any need for labels? Or do I have to apply labels? If labels are required, is it necessary to use classes or the usage of target (data) + labels is adequate?</p>

InnovArul: <p>In general,  to train a network based on gradients,  you need an objective function. One of the examples of this approach is to have class labels and applying cross entropy loss. However, this is not the only way.</p>
<p>If you can define a target for your data and an objective function to measure the target mathematically,  you can make a loss function out of it. I’m not sure what could be the target and loss function for your particular use case.<br>
Hopefully someone who knows about your domain may answer your question here.</p>

vin: <p>Noted. Thank you Sir for your time and answers</p>