Hi Barber!



 blackbirdbarber:

Here is the code I have:
...
input = torch.rand(bs,3, 256,256)
target = torch.rand(bs, 1000)

# target = target.long()
...
loss_fn = torch.nn.CrossEntropyLoss()
# loss_fn = torch.nn.BCEWithLogitsLoss()
loss = loss_fn(output, target)
...

torch.nn.CrossEntropyLoss() should be the main loss function
(criterion) for ImageNet classification. I just tried to test the code and got this error I could not fix fast.
If I set the torch.nn.BCEWithLogitsLoss()  it will work but the params p1 and p2 will be just the equal.
What am I doing wrong?


I assume that by “got this error” you mean the error message
in your post title, “Expected object of scalar type Long but got
scalar type Float.”  This was to be expected.
CrossEntropyLoss takes integer class labels for its target,
specifically of type LongTensor.  In your code you are passing
it a FloatTensor.
For random “toy” data, you probably want something like
target = torch.randint (nClass, (bs,)), where
nClass is the number of classes in your classification problem.
(I see that you commented out the line of code
# target = target.long().  Didn’t that fix this
particular error for you.)
(As a further note, BCEWithLogitsLoss() does take a
FloatTensor target, so it would not give you this error.  But,
yes, for a multiclass – i.e., not binary – classification problem
you would want CrossEntropyLoss.)
Good luck.
K. Frank