Hi Joe!



 joekid:

Thank you for your comments. I can’t make it work
…
The error occurs in the line

loss = criterion(output, target) ,

Here is my code:
…
criterion = torch.nn.CrossEntropyLoss()
…
The error is
…
RuntimeError: Expected object of scalar type Long but got scalar type Float for argument #2 ‘target’ in call to _thnn_nll_loss2d_forward


The error (“scalar type Long”) and the documentation for
your loss function, CrossEntropyLoss 23, should serve to sort
things out.
As @ptrblck mentioned in one of his posts, above,
CrossEntropyLoss takes a LongTensor for its targets,
not a FloatTensor, hence the error.
CrossEntropyLoss (perhaps better called more explicitly
“categorical cross-entropy loss”) requires integer class labels
in [0, nClass - 1] for its targets.  Provided the target in
your code is, indeed, class labels (as compared to probabilities,
or some such), it should suffice to convert it to a LongTensor,
e.g., loss = criterion (output, target.long()).
BCELossWithLogits does take a FloatTensor for its targets.
(It takes probabilities, not just 0,1 class labels.)  So that’s why
you didn’t get the error when you tried BCELossWithLogits.
Good luck.
K. Frank