RuntimeError: Assertion `cur_target >= 0 && cur_target < n_classes' failed

Hi,

I am trying to do an image segmentation with PyTorch. Unfortunally I am getting an error while computing the loss:

RuntimeError: Assertion cur_target &gt;= 0 &amp;&amp; cur_target &lt; n_classes' failed. at c:\programdata\miniconda3\conda-bld\pytorch_1533090623466\work\aten\src\thnn\generic/SpatialClassNLLCriterion.c:110

I am using a CrossEntropyLoss:

loss_ce = nn.CrossEntropyLoss()

ce_loss = loss_ce(y_pred, y_true)

The size of y_pred is [4,1,256,256], the size of y_true is [4,256,256].

The values of y_true are [0,1].

In my opinion, that should fit, to the requirements listed in the PyTorch documentation for the CrossEntropyLoss (torch.nn — PyTorch master documentation):

N = 4, C = 1, d1 = d2 = 256

I also checked the “autograd-flag” of the two variables:

print(y_true.requires_grad)
print(y_pred.requires_grad)

delivers

False
True

so that seems to be correct, too.

I can’t understand, why I am getting this error.

Can anybody help me please?

Thank you!

I think i found the problem, producing the error. In the documentation i red, that "the values in the target vector (in my case y_true) have to be: 0 <= y_pred[i] <= C-1.
In my case C = 1, so all the values y_pred[j] = 1 are out of range.

Since I can’t change my input vector (y_pred), I don’t know, how to use the CrossEntropy Loss for this binary problem properly.
Can anybody help me please?

For two classes, the usual setup is to take the binary cross entropy loss. PyTorch has nn.BCELoss and nn.BCELossWithLogits for this, the latter including the sigmoid layer that you would need to add to your net for the former. The trick here is that the “probability” of the 0 class is just 1 minus that of the 1 class.
CrossEntropyLoss and friends are for multiple classes, where you can’t do this trick. If you wanted to use that for two classes, you would need to have an output that has size two in the class dimension.

Best regards

Thomas

1 Like

Hi Tom,

thank you for your reply.
In the documentation for the Binary Cross Entropy Loss, it says that the input and target tensors have to be of the the same size. Therefore i won’t be abel to use it.

y_pred.size() = [4,1,256,256]
y_true.size() = [4,256,256]

Or is there a way to match their sizes?

Thank you!

y_pred.squeeze(1)

Best regards

Thomas

1 Like

Unfortunally, i am still getting the same error:

RuntimeError: Assertion `x >= 0. && x <= 1.' failed. input value should be between 0~1, but got -0.148084 at c:\programdata\miniconda3\conda-bld\pytorch_1533090623466\work\aten\src\thnn\generic/BCECriterion.c:34

Did you take BCELoss but didn’t have Sigmoid?
You’d need BCELossWithLogits instead.

1 Like

It worked!

Thank you so much!