Target 7 is out of bounds

username: Jami_Anwesh_Kumar
cooked: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/b/9b3b697eb4a323dce3f970e3e147711cc08235d5.png" data-download-href="https://discuss.pytorch.org/uploads/default/9b3b697eb4a323dce3f970e3e147711cc08235d5" title="outof bounds error"><img src="https://discuss.pytorch.org/uploads/default/original/3X/9/b/9b3b697eb4a323dce3f970e3e147711cc08235d5.png" alt="outof bounds error" data-base62-sha1="m9fisuu1HDpKsP68OkKF28bhYQB" width="690" height="263" data-dominant-color="F9F9FA"><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">outof bounds error</span><span class="informations">1150×439 22.1 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><br>
I uploaded the out of bounds error picture here.I used cross entropy loss and running my model on rafdb dataset which contain 7 labels.</p>
<pre><code class="lang-auto">` epoch = 0

# Train the model

total_step = len(train_loader)

curr_lr = learning_rate

for epoch in range(num_epochs):

    for i, (images, labels) in enumerate(train_loader):

        #images = images.to(device)

        #labels = labels.to(device)

        print(labels)

        print(labels.size())

        # Forward pass

        #print(images.size)

        outputs = model(images)

        #print(outputs)

        print(outputs.size())

        loss = criterion(outputs, labels)

        print(loss)

        # Backward and optimize

        optimizer.zero_grad()

        loss.backward()

        optimizer.step()`
</code></pre>
<p>It is showing error at loss function.Thanks in advance for the help.</p>

username: KFrank
cooked: <p>Hi Jami!</p>
<aside class="quote no-group quote-modified" data-username="Jami_Anwesh_Kumar" data-post="1" data-topic="94831" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jami_anwesh_kumar/48/27615_2.png" class="avatar"> Jami_Anwesh_Kumar:</div>
<blockquote>
<p>Target 7 is out of bounds</p>
</blockquote>
</aside>
<p><code>CrossEntropyLoss</code> expects class labels that run from<br>
<code>{0, 1, ..., nClass - 1}</code> (not from <code>1</code> to <code>nClass</code>).</p>
<p>So in your case, <code>6</code> is the largest legal class label, and <code>7</code> is out<br>
of bounds.</p>
<aside class="quote no-group">
<blockquote>
<p>I uploaded the out of bounds error picture here.</p>
</blockquote>
</aside>
<p>By the way, please don’t post screen-shots of textual information.<br>
Doing so breaks screen readers (accessibility), search, and copy-paste.</p>
<p>Best.</p>
<p>K. Frank</p>

username: Jami_Anwesh_Kumar
cooked: <p>Hi KFrank,<br>
Thanks a lot for the reply,What to do then,my class labels are starting from 1 and i have class labels from 1 to 7.<br>
And  also i can’t see any out of bounds error picture here(which you mentioned you uploaded it).<br>
I posted screen shot because it will be clear about the error.May be next time on wards i will post whole error once by copy pasting it.</p>

username: KFrank
cooked: <p>Hi Jami!</p>
<aside class="quote no-group" data-username="Jami_Anwesh_Kumar" data-post="3" data-topic="94831" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jami_anwesh_kumar/48/27615_2.png" class="avatar"> Jami_Anwesh_Kumar:</div>
<blockquote>
<p>What to do then,my class labels are starting from 1 and i have class labels from 1 to 7.</p>
</blockquote>
</aside>
<p>Remember, class labels are really just arbitrary labels that distinguish<br>
one class from another, but nothing else.  They have no numerical<br>
meaning.</p>
<p>So just subtract <code>1</code> from all of your labels to get a set that runs from<br>
<code>0</code> to <code>6</code>.  You could either do this by preprocessing your data, or you<br>
could do it on the fly.</p>
<aside class="quote no-group">
<blockquote>
<p>And  also i can’t see any out of bounds error picture here(which you mentioned you uploaded it).</p>
</blockquote>
</aside>
<p>Sorry about that.  I was quoting you (rather than referring to an upload<br>
of my own), but I messed up the forum quote block.  (I’ve edited my<br>
post to fix the quoting and clean that up.)</p>
<aside class="quote no-group">
<blockquote>
<p>May be next time on wards i will post whole error once by copy pasting it.</p>
</blockquote>
</aside>
<p>Thanks, that would be great.  Makes for a more functional forum.</p>
<p>Best.</p>
<p>K. Frank</p>

username: Jami_Anwesh_Kumar
cooked: <p>Thanks a lot KFrank,Actually that idea of class labels from 0 to 6 worked for me.</p>