5

Im not able to assign int64 to torch tensor. I have got the following tensor

tempScale = torch.zeros((total, len(scale))).cuda() if useGpu else torch.zeros((nbPatchTotal, len(scale)))

In my code, when I'm using the following line, it's throwing an error message

tmpScale[:, j] = scale

The error message is

TypeError: can't assign a numpy.int64 to a torch.cuda.FloatTensor

what am I missing?

user1241241
  • 664
  • 5
  • 20

1 Answers1

5

You have to convert scale to a torch tensor of the same type and device as tmpScale before assignment.

tmpScale[:, j] = torch.from_numpy(scale).to(tmpScale)

Note that this is casting scale from an int64 to a float32 which will likely result in a loss of precision if values in scale have magnitude larger than 224 (about 16 million).

jodag
  • 19,885
  • 5
  • 47
  • 66