How to convert an integer to a tensor?

I know I can create a new torch.int8 tensor. But I want to convert an integer from python user input to a tensor for some specific task.

.to() or torch.from_int() doesn’t exist.

You can directly pass the int to a tensor and convert it afterwards to an int8.
I’m not sure how you are getting int8 input in Python so could you describe your use case a bit more and what exactly is not working?

1 Like
>>> a = 1
>>> torch.tensor(a, dtype=torch.int8)
tensor(1, dtype=torch.int8)

I think this will work

2 Likes