PyTorch: RuntimeError: “mse_cpu” not implemented for ‘Long’

I'm getting this weird error when doing softmax:

RuntimeError: "mse_cpu" not implemented for 'Long'

Add Comment
1 Answer(s)
The error you're encountering is because the Mean Squared Error (MSE) loss in PyTorch doesn't work with torch.LongTensor, which is used for integer types. Instead, you should be using torch.FloatTensor or torch.DoubleTensor, which are for floating point numbers. Here's a short example of how to avoid this error: ``` target = target.float() # Ensure target tensor is float output = model(input) criterion = nn.MSELoss() loss = criterion(output, target) ``` In this case we convert the target tensor to a float tensor before the loss is calculated. It's important to always be mindful of the tensor types you're working with, especially when it comes to loss calculations in PyTorch. Different operations and functions require different types, and trying to use the wrong one will result in errors like this.
Answered on August 19, 2023.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.