如何将pytorch的预测转成普通文本

How to convert the prediction of pytorch into normal text

我有一个 PyTorch 模型,我正在对其进行预测。进行预测后,我得到的输出为

tensor([[-3.4333]], grad_fn=<AddmmBackward>)

但我需要它作为普通整数 -3.4333。我该怎么做。

在您的张量上调用 .item 将其转换为标准 python 数字。

获取张量调用中的值:tensor.item()

例如:

>>> x = torch.tensor([1.0])
>>> x.item()
1.0

https://pytorch.org/docs/stable/generated/torch.Tensor.item.html