Conversion between tensor types in neural network training


(Soumyadeep Ghosh) #1

Lets say we are defining the loss function of a neural network as follows:

classifier_loss = classifier_criterion(classifier_output, label)

Is it true that classifier_output needs to be a FloatTensor and label as a LongTensor ??

I have batch size =1 thats why my label is a 1x1 ByteTensor. Do I need to convert into a LongTensor ??..

If yes, how do I do that ??
I am using crossentropyloss.
Thanks in advance


#2

If you’re using CrossEntropyLoss, your target (label) should be a LongTensor. It varies based on the loss function. For example, for MSELoss, your target (label) should have the same type as your input.
To convert any tensor t to a LongTensor, you can do t.long().


(Soumyadeep Ghosh) #3

Thanks @richard for your reply I fixed it. I also observed that for cross entropy loss requires a [1,] size tensor for batch size =1. I was giving a [1x1] tensor and it was not being able to work with that. This was pretty annoying though.

It would have been better if these things were consistent for all the loss functions.