print exact value of tensor(floating point precision) with pytorch

怎甘沉沦 提交于 2021-02-04 14:55:33

问题


I'm trying to print torch.FloatTensor like

a = torch.FloatTensor(5,5)
print(a)

this way I can got a value like

0.0000e+00  0.0000e+00  3.2286e-41  9.4448e+21  4.3346e-38
1.2412e-40  1.2313e+00  1.6751e-37  3.1138e-40  9.4460e+21
2.6801e-36  3.5873e-41  9.4463e+21  4.9653e-35  3.9963e-40
9.4454e+21  2.6801e-36  1.2771e-40  9.4460e+21  1.7153e-34
7.7056e-40  9.0090e+15  4.1877e-38  2.9775e-41  1.5695e-43

But I want to get more accurate value, like 10 decimal point

0.1234567891+01

in python, I could got it

print('{:.10f}'.format(a))

but in case of tensor, I got this error

TypeError: unsupported format string passed to torch.FloatTensor.__format__

How can I print accurate value of tensor?


回答1:


You can set the precision options

torch.set_printoptions(precision=10)

There are more formatting options on the documentation page: http://pytorch.org/docs/master/torch.html#creation-ops it is very similar to numpys.




回答2:


Just as a side note, this functionality has been taken from numpy. One of the reasons why PyTorch is smart is because they took many the good ideas from numpy.

However, in numpy the default precision is 8 and in PyTorch the default is 4.



来源:https://stackoverflow.com/questions/47483733/print-exact-value-of-tensorfloating-point-precision-with-pytorch

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