如何在 Pytorch 中将一维 IntTensor 转换为 int

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

如何将一维 IntTensor 转换为整数?这个:

IntTensor.int()

报错:

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

您可以使用:

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

您使用的密钥是 autograd.Variable 类型的对象。 .data 给出张量和索引 0 可用于访问元素。

我知道的最简单干净的方法:

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.