PyTorch 打印输出到 2dp

use*_*120 3 python printing decimal pytorch

考虑到输出是多元素 FloatTensor,有没有办法将 CNN 模型评估的输出打印为 2dp?

例如。

prediction = torch.exp(model(image2))
print(prediction)
Run Code Online (Sandbox Code Playgroud)

出去:

Variable containing:
 2.84e-01  1.68e-07  7.16e-01
[torch.FloatTensor of size 1x3]
Run Code Online (Sandbox Code Playgroud)

如果我可以将值输出为:

Variable containing:
 0.28  0.00  0.72
[torch.FloatTensor of size 1x3]
Run Code Online (Sandbox Code Playgroud)

我试过了:

print("%.2f" % prediction)
Run Code Online (Sandbox Code Playgroud)

并使用:

torch.set_printoptions(precision=2)
Run Code Online (Sandbox Code Playgroud)

但都没有达到预期的效果。

我查看了文档页面:

http://pytorch.org/docs/master/torch.html#creation-ops

...在“torch.set_printoptions”下,但我看不出任何参数在这种情况下有什么帮助。

提前谢谢了!

dkv*_*dkv 5

目前,该措施已得到实施。使用

torch.set_printoptions(sci_mode=False)
Run Code Online (Sandbox Code Playgroud)

https://pytorch.org/docs/stable/torch.html#torch.set_printoptions