Dtype error expected long but got float

Hi
I am trying to implement an Classifier but I got the following error. I have no idea why this is happening

Could you check the dtype of input via print(input.type())?
I guess it might be a LongTensor and you can transform it via input = input.float().

PS: It’s better to post code directly by wrapping it into three backticks ``` :wink:

Thank you for the reply. I found the solution on another website.
Simply change all the parameters of model to float by using net.float() before loss and convert the input to float.

@ptrblck
Could you elaborate why the model’s dtype is initialized to long even though he didn’t specify anywhere?
Thanks

Might be default.Not sure though

I don’t think the model’s parameters are initialized to long, but rather the input.
torch.from_numpy() respects the data type passed to this method and often we see issues, when the original numpy array contained int values.

1 Like

Okay, got your point. Thanks

Could somebody help me. I am new to Pytorch and am trying to predict house prices. I am always getting the following error: Found dtype Long but expected Float.
My Notebook: https://colab.research.google.com/drive/1Vd9fJs0bfd7IZ8D5dHS859EThz1DEB58?usp=sharing

#Doesn't Work
epochs=500
final_losses=[]
for i in range(epochs):
    i=i+1
    y_pred=model.forward(X_train)
    y_pred=y_pred#.to(torch.float32)#.float()
    #print(y_pred.type())
    loss=loss_function(y_pred,y_train)#.float()
    #print("Loss: ",loss.type())
    final_losses.append(loss)
    if i%10==1:
        print("Epoch number: {} and the loss : {}".format(i,loss.item()))
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

Any help would be highly appreciated.

Hi techboy,
What you should do to fix this error is to convert the tensor which is in dtype Long to float. You can figure out which tensor is causing error by looking out the line which gives error while execution.

Thank you very much. I had forgotten to change y_train and y_test to float.