Expect FloatTensors but got LongTensors in MNIST-like task
I am performing a MNIST-like task, the input is 10-class images, and the expected output is the predicted class of the images.

But now the output is like [-2.3274, -2.2723, ...], which the length is the batch_size. And the target is [4., 2., 2., 8., ...] 

Error message: RuntimeError: expected object for scalar type Long but got scalar type float for argument #2 'target'

class Net(nn.Module):
    ...
    ...
    def forward(self, x):
        ...
        ...
        return F.log_softmax(x, dim = 1)


criterion = torch.nn.NLLLoss()


Can anyone give me some advice? Thanks.

The error you got refers to the second (#2) argument of the loss: the target.
NLLLoss expects (for each element) to have a float vector of probabilities, and a single long (i.e., integer) target per element.
In your case, your "target" values are [4., 2., 2., 8., ...] which are of type float. you need to convert your target to long:

target = target.to(dtype=torch.long)

