comparing torch.nn.CrossEntropyLoss with label as int and prob tensor produces an error
I have this criterion:
criterion = nn.CrossEntropyLoss()

And during training phase i have:
label = tensor([0.])
outputs = tensor([[0.0035, 0.0468]], grad_fn=&lt;AddmmBackward&gt;)

When i try to compare them:
 criterion(outputs, label)

I get this error:
    ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
RuntimeError: Expected object of scalar type Long but got scalar type Float for argument #2 'target' in call to _thnn_nll_loss_forward


nn.CrossEntropyLoss expects its label input to be of type torch.Long and not torch.Float.
Note that this behavior is opposite to nn.BCELoss where target is expected to be of the same type as the input.
If you simply remove the . from your label:
label = torch.tensor([0])  # no . after 0 - now it is an integer

pytorch will automatically create label as torch.Long type, and you are okay:
In [*]: criterion(outputs, torch.tensor([0]))
Out[*]: tensor(0.7150)


Commenting on the other answers by planet_pluto and Craig.Li:

A more general way of casting an existing tensor is to use .to(...):

label = torch.tensor([0]).to(dtype=torch.long)

However, creating-and-casting is not  very efficient way of doing stuff:
Think of it, you make pytroch create a torch.float tensor and then cast it to torch.long.

Alternatively, you can explicitly define the desired dtype upon creation of the tensor:

label = torch.tensor([0.], dtype=torch.long)

This way pytorch creates label with the desired dtype and no 2nd phase of casting is required.
