close

[Solved] RuntimeError: expected scalar type Long but found Float

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error RuntimeError: expected scalar type Long but found Float in Python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How RuntimeError: expected scalar type Long but found Float Error Occurs?

Today I get the following error RuntimeError: expected scalar type Long but found Float in Python.

How To Solve RuntimeError: expected scalar type Long but found Float Error ?

  1. How To Solve RuntimeError: expected scalar type Long but found Float Error?

    To Solve RuntimeError: expected scalar type Long but found Float Error LongTensor is synonymous with integer. PyTorch won't accept a FloatTensor as categorical target, so it's telling you to cast your tensor to LongTensor. This is how you should change your target dtype:

Solution 1

LongTensor is synonymous with integer. PyTorch won’t accept a FloatTensor as categorical target, so it’s telling you to cast your tensor to LongTensor. This is how you should change your target dtype:

Yt_train = Yt_train.type(torch.LongTensor)

This is very well documented on the PyTorch website, you definitely won’t regret spending a minute or two reading this page. PyTorch essentially defines nine CPU tensor types and nine GPU tensor types:

╔══════════════════════════╦═══════════════════════════════╦════════════════════╦═════════════════════════╗
║        Data type         ║             dtype             ║     CPU tensor     ║       GPU tensor        ║
╠══════════════════════════╬═══════════════════════════════╬════════════════════╬═════════════════════════╣
║ 32-bit floating point    ║ torch.float32 or torch.float  ║ torch.FloatTensor  ║ torch.cuda.FloatTensor  ║
║ 64-bit floating point    ║ torch.float64 or torch.double ║ torch.DoubleTensor ║ torch.cuda.DoubleTensor ║
║ 16-bit floating point    ║ torch.float16 or torch.half   ║ torch.HalfTensor   ║ torch.cuda.HalfTensor   ║
║ 8-bit integer (unsigned) ║ torch.uint8                   ║ torch.ByteTensor   ║ torch.cuda.ByteTensor   ║
║ 8-bit integer (signed)   ║ torch.int8                    ║ torch.CharTensor   ║ torch.cuda.CharTensor   ║
║ 16-bit integer (signed)  ║ torch.int16 or torch.short    ║ torch.ShortTensor  ║ torch.cuda.ShortTensor  ║
║ 32-bit integer (signed)  ║ torch.int32 or torch.int      ║ torch.IntTensor    ║ torch.cuda.IntTensor    ║
║ 64-bit integer (signed)  ║ torch.int64 or torch.long     ║ torch.LongTensor   ║ torch.cuda.LongTensor   ║
║ Boolean                  ║ torch.bool                    ║ torch.BoolTensor   ║ torch.cuda.BoolTensor   ║
╚══════════════════════════╩═══════════════════════════════╩════════════════════╩═════════════════════════╝

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment