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

爷,独闯天下 提交于 2019-12-05 04:42:43

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.

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"

torch.tensor('variable',dtype=torch.int8)

try this

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!