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

后端 未结 3 1201
独厮守ぢ
独厮守ぢ 2021-02-13 11:46

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:

相关标签:
3条回答
  • 2021-02-13 12:17

    You can use:

    print(dictionary[IntTensor.data[0]])

    The key you're using is an object of type autograd.Variable. .data gives the tensor and the index 0 can be used to access the element.

    0 讨论(0)
  • 2021-02-13 12:17

    The simplest and cleanest method I know:

    IntTensor.item()
    

    From PyTorch docs:

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

    0 讨论(0)
  • 2021-02-13 12:28
    torch.tensor('variable',dtype=torch.int8)
    

    try this

    0 讨论(0)
提交回复
热议问题