IndexError:Target 159 is out of bounds

username: A_Shamsadini
cooked: <p>Hi, I use nn.CrossEntropyLoss() for segmentation, but I have this error</p>
<p>IndexError                                Traceback (most recent call last)<br>
 in ()<br>
26       labels=labels[…,0].squeeze()<br>
27       labels = labels.squeeze_()<br>
—&gt; 28       loss1 = criterion1(logits_masks,masks1.long())<br>
29       loss2 = criterion1(logits_labels,labels)<br>
30       <span class="hashtag">#print</span>(‘loss1’,loss1)</p>
<p>2 frames<br>
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction)<br>
2822     if size_average is not None or reduce is not None:<br>
2823         reduction = _Reduction.legacy_get_string(size_average, reduce)<br>
 → 2824     return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)<br>
2825<br>
2826</p>
<p>IndexError: Target 1 is out of bounds.</p>
<p>for i, dat in enumerate(train_generator):</p>
<pre><code>  j+=1

  features, masks,labels = dat

  features = features.to(device).float()

  masks = masks.to(device)

  

  labels = labels.to(device)

  optimizer.zero_grad()  

  ### FORWARD AND BACK PROP

  features = features.permute(0, 3, 1,2)

  logits_labels,logits_masks = model((features))

  masks1=masks[...,0].squeeze()

  #masks1 = masks1.squeeze_()

  labels=labels[...,0].squeeze()

  labels = labels.squeeze_()

  loss1 = criterion1(logits_masks,masks1.long())

  loss2 = criterion1(logits_labels,labels)

  #print('loss1',loss1)

  #print('loss2',loss2)

  loss=loss1+loss2</code></pre>

username: ptrblck
cooked: <p>For a multi-class classification use case <code>nn.CrossEntropyLoss</code> expects a model output containing logits in the shape <code>[batch_size, nb_classes]</code> and a target tensor in the shape <code>[batch_size]</code> containing class indices in the range <code>[0, nb_classes-1]</code>.<br>
Based on this error:</p>
<aside class="quote no-group" data-username="A_Shamsadini" data-post="1" data-topic="133053">
<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/a_shamsadini/48/30506_2.png" class="avatar"> A_Shamsadini:</div>
<blockquote>
<p>IndexError: Target 1 is out of bounds.</p>
</blockquote>
</aside>
<p>I would guess that your model returns an output in the shape <code>[batch_size, 1]</code> and thus (in the multi-class classification setup) returns a single logit for class0.<br>
In case you are working on a binary classification, use <code>nn.BCEWithLogits</code> instead or keep <code>nn.CrossEntropyLoss</code> and return logits for both classes via an output in the shape <code>[batch_size, 2]</code>.</p>

username: A_Shamsadini
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="133053">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>nn.BCEWithLogits</p>
</blockquote>
</aside>
<p>When I used nn.BCEWithLogitsLoss() I can’t  backward loss and I have this error</p>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in ()<br>
44       <span class="hashtag-raw">#avg_accuracy</span> = train_accuracy / len(train_loader)<br>
45       accuracy_train=train_accuracy+(correct_train/total_train_c)<br>
—&gt; 46       loss.backward(retain_graph=True)<br>
47<br>
48         ### UPDATE MODEL PARAMETERS</p>
<p>1 frames<br>
/usr/local/lib/python3.7/dist-packages/torch/autograd/<strong>init</strong>.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables, inputs)<br>
147     Variable.<em>execution_engine.run_backward(<br>
148         tensors, grad_tensors</em>, retain_graph, create_graph, inputs,<br>
 → 149         allow_unreachable=True, accumulate_grad=True)  # allow_unreachable flag<br>
150<br>
151</p>
<p>RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.DoubleTensor [4, 6, 32, 32]] is at version 1; expected version 0 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/f/9fb0e2d6e7db9e48eca494ef7cf923f36617b176.png" data-download-href="https://discuss.pytorch.org/uploads/default/9fb0e2d6e7db9e48eca494ef7cf923f36617b176" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/f/9fb0e2d6e7db9e48eca494ef7cf923f36617b176_2_690x387.png" alt="image" data-base62-sha1="mMGTstB7hTHw6yf0hGjvNl4uhw2" width="690" height="387" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/f/9fb0e2d6e7db9e48eca494ef7cf923f36617b176_2_690x387.png, https://discuss.pytorch.org/uploads/default/optimized/3X/9/f/9fb0e2d6e7db9e48eca494ef7cf923f36617b176_2_1035x580.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/9/f/9fb0e2d6e7db9e48eca494ef7cf923f36617b176.png 2x" data-dominant-color="EAF0EF"><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">image</span><span class="informations">1366×768 118 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>
<p>masks1 torch.Size([4, 6, 32, 32])<br>
logits_masks torch.Size([4, 6, 32, 32]) "output "</p>
<p>criterion2 = nn.BCEWithLogitsLoss()</p>

username: ptrblck
cooked: <p>In your screenshot it seems you are using <code>retain_graph=True</code>. Could you explain this usage and why it’s needed? Often users add this argument to trade one error for another one (which is often the error you are facing now).</p>
<p>Also, you can post code snippets by wrapping them into tree backticks ```, which makes debugging easier.</p>