How to fix RuntimeError “Expected object of scalar type Float but got scalar type Double for argument”?

后端 未结 4 1143
伪装坚强ぢ
伪装坚强ぢ 2020-12-28 11:55

I\'m trying to train a classifier via PyTorch. However, I am experiencing problems with training when I feed the model with training data. I get this error on y_pred =

相关标签:
4条回答
  • 2020-12-28 12:04

    Reference is from this github issue.

    When the error is RuntimeError: Expected object of scalar type Float but got scalar type Double for argument #4 'mat1', you would need to use the .float() function since it says Expected object of scalar type Float.

    Therefore, the solution is changing y_pred = model(X_trainTensor) to y_pred = model(X_trainTensor.float()).

    Likewise, when you get another error for loss = loss_fn(y_pred, y_trainTensor), you need y_trainTensor.long() since the error message says Expected object of scalar type Long.

    You could also do model.double(), as suggested by @Paddy .

    0 讨论(0)
  • 2020-12-28 12:09

    I had same issue

    resolved

    Before converting to Tensor, try this

    X_train = X_train.astype(np.float32)
    
    0 讨论(0)
  • 2020-12-28 12:14

    This issue can also occur if the wrong loss function is selected. For example, if you have regression problem, but you are trying to use cross entropy loss. Then it will be fixed by changing your loss function on MSE

    0 讨论(0)
  • 2020-12-28 12:18

    The issue can be fixed by setting the datatype of input to Double i.e torch.float32

    I hope the issue came because your datatype is torch.float16

    0 讨论(0)
提交回复
热议问题