RuntimeError: Expected object of scalar type Long but got scalar type Float for argument #3 'mat2' in call to _th_addmm_out

Hi everyone, I am getting this error and I am not sure what is happening. I am trying to predict air passenger data with a LTSM. The notebook is available on this link:

https://drive.google.com/file/d/1lk9uKhjbMlLHBvvqmaIuBFvbVfoKAia7/view?usp=sharing

Thanks in advance to anyone willing to help.

Have you tried just converting the seq variable to a float in your training loop like this

    for seq, y_train in train_data:
        
        seq = seq.float()
        optimizer.zero_grad()
        model.hidden = (torch.zeros(1,1,model.hidden_size),
                        torch.zeros(1,1,model.hidden_size))

if that doesn’t work can you print out the type of seq and the hidden states.

Hello,

This is just because LSTM expects Float in input, and your data is Long. Use this code

for seq, y_train in train_data:
    seq = seq.type(torch.float32)
    optimizer.zero_grad()
    y_pred = model(seq)
    loss = criterion(y_pred, y_train)
    loss.backward()
    optimizer.step()

Thank you very much @omarfoq and @Dwight_Foster but I have tried both options getting a confusing message. You can see the updated notebook here:
https://drive.google.com/file/d/1VKpZCkSeEJoP_iyXAi0xBZRlPqE8B1fE/view?usp=sharing

How can I proceed?

So now it seems like your loss is the problem. Try this:

        
        loss = criterion(y_pred, y_train).float()
        loss.backward()
        optimizer.step()
        

It throws the same error. It is very strange.