IndexError:Target 159 is out of bounds

Hi, I use nn.CrossEntropyLoss() for segmentation, but I have this error

IndexError Traceback (most recent call last)
in ()
26 labels=labels[…,0].squeeze()
27 labels = labels.squeeze_()
—> 28 loss1 = criterion1(logits_masks,masks1.long())
29 loss2 = criterion1(logits_labels,labels)
30 #print(‘loss1’,loss1)

2 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction)
2822 if size_average is not None or reduce is not None:
2823 reduction = _Reduction.legacy_get_string(size_average, reduce)
→ 2824 return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
2825
2826

IndexError: Target 1 is out of bounds.

for i, dat in enumerate(train_generator):

  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

For a multi-class classification use case nn.CrossEntropyLoss expects a model output containing logits in the shape [batch_size, nb_classes] and a target tensor in the shape [batch_size] containing class indices in the range [0, nb_classes-1].
Based on this error:

I would guess that your model returns an output in the shape [batch_size, 1] and thus (in the multi-class classification setup) returns a single logit for class0.
In case you are working on a binary classification, use nn.BCEWithLogits instead or keep nn.CrossEntropyLoss and return logits for both classes via an output in the shape [batch_size, 2].

When I used nn.BCEWithLogitsLoss() I can’t backward loss and I have this error

RuntimeError Traceback (most recent call last)
in ()
44 #avg_accuracy = train_accuracy / len(train_loader)
45 accuracy_train=train_accuracy+(correct_train/total_train_c)
—> 46 loss.backward(retain_graph=True)
47
48 ### UPDATE MODEL PARAMETERS

1 frames
/usr/local/lib/python3.7/dist-packages/torch/autograd/init.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables, inputs)
147 Variable.execution_engine.run_backward(
148 tensors, grad_tensors
, retain_graph, create_graph, inputs,
→ 149 allow_unreachable=True, accumulate_grad=True) # allow_unreachable flag
150
151

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).

masks1 torch.Size([4, 6, 32, 32])
logits_masks torch.Size([4, 6, 32, 32]) "output "

criterion2 = nn.BCEWithLogitsLoss()

In your screenshot it seems you are using retain_graph=True. 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).

Also, you can post code snippets by wrapping them into tree backticks ```, which makes debugging easier.