Print exact value of PyTorch tensor (floating point precision)(打印 PyTorch 张量的精确值(浮点精度))
问题描述
我正在尝试打印 torch.FloatTensor
像:
I'm trying to print torch.FloatTensor
like:
a = torch.FloatTensor(3,3)
print(a)
这样我可以得到一个值:
This way I can get a value like:
0.0000e+00 0.0000e+00 3.2286e-41
1.2412e-40 1.2313e+00 1.6751e-37
2.6801e-36 3.5873e-41 9.4463e+21
但我想获得更准确的值,例如小数点后 10 位:
But I want to get more accurate value, like 10 decimal point:
0.1234567891+01
使用其他 python 数值对象,我可以得到它:
With other python numerical objects, I could get it with:
print('{:.10f}'.format(a))
但在张量的情况下,我收到此错误:
but in the case of a tensor, I get this error:
TypeError: unsupported format string passed to torch.FloatTensor.__format__
如何打印更精确的张量值?
How can I print more precise values of tensors?
推荐答案
可以设置精度选项:
torch.set_printoptions(precision=10)
文档页面上有更多格式选项,它是与 numpy 非常相似.
There are more formatting options on the documentation page, it is very similar to numpy's.
这篇关于打印 PyTorch 张量的精确值(浮点精度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!