Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cast a 1-d IntTensor to int in Pytorch

Tags:

I get a 1-D IntTensor,but i want to convert it to a integer. I try it by this method:

print(dictionary[IntTensor.int()]) 

but got an error:

KeyError: Variable containing:  423 [torch.IntTensor of size 1] 

Thanks~

like image 659
Ruben Avatar asked Dec 01 '17 07:12

Ruben


People also ask

How do you make a one direction tensor in PyTorch?

Creating one-dimensional Tensor tensor() method. Syntax of creating one dimensional tensor is as follows: n= torch. tensor([Tensor elements])

How do I extract numbers from a tensor?

You can use x. item() to get a Python number from a Tensor that has one element.


1 Answers

The simplest and cleanest method I know:

IntTensor.item() 

Returns the value of this tensor as a standard Python number. This only works for tensors with one element. For other cases, see tolist.

  • PyTorch Docs
like image 60
Marcin Avatar answered Oct 11 '22 06:10

Marcin