TypeError: expected torch.LongTensor (got torch.FloatTensor)

I’m not sure what exactly should be calculated in the criterion, but currently you are trying to pass the output of your model as the input and target to nn.CrossEntropyLoss.

Basically the error message comes from torch.LongTensor(output). I wouldn’t recommend to use the “type” tensors but rather cast your tensors using .long() etc.
So output.long() would work, but then the target won’t for your criterion.

After fixing some minor issues in your code (e.g. your model does not return anything in forward), this works for me:

x = torch.randn(batch_size, time_step, input_dim)
y = torch.empty(batch_size, dtype=torch.long).random_(output_dim)

output = lstm_model(x)
loss = criterion(output, y)

Probably the shapes, number of classes etc. might be wrong as I don’t know your exact use case.

3 Likes