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

I am reproducing a model in a paper, model structure in the paper:

There are only two classes: 0 or 1.
my target is 1, and my model’s output is: tensor([[0.4853]].

How should I compute loss?

In [118]: model
  (conv1): Conv1d(4, 16, kernel_size=(9,), stride=(3,))
  (conv2): Conv1d(16, 16, kernel_size=(9,), stride=(3,))
  (dropout): Dropout(p=0.3)
  (dense): Linear(in_features=192, out_features=1, bias=True)
  (sigmoid): Sigmoid()

In [119]: target
Out[119]: tensor([1])

In [120]: output
Out[120]: tensor([[0.4853]], grad_fn=<SigmoidBackward>)

In [121]: loss_func
Out[121]: CrossEntropyLoss()

In [122]: loss_func(output, target)

Use nn.BCELoss as the loss_func.
Alternatively, you could also remove the sigmoid and use nn.BCEWithLogitsLoss.

1 Like