直接使用 torch.set_printoptions(precision=4,sci_mode=False)
例如:
torch.set_printoptions(precision=4,sci_mode=False)
print(f"当前这张图的norm后的标准差为:\n{std}\n均值为:\n{mean}")
> 当前这张图的norm后的标准差为:
tensor([[[1.]],[[1.]],[[1.]]])
均值为:
tensor([[[ 0.0000]],[[ 0.0000]],[[ 0.0000]]])
另外,还有numpy、pandas以及jupyter设置float显示的方式
np.set_printoptions(suppress=True)
pd.options.display.float_format = '{:.2f}'.format
jupyter设置输出浮点数格式主要是靠魔法函数%precision
,例如:
In [1]: from math import piIn [2]: %precision 3
Out[2]: u'%.3f'In [3]: pi
Out[3]: 3.142In [4]: %precision %i
Out[4]: u'%i'In [5]: pi
Out[5]: 3In [6]: %precision %e
Out[6]: u'%e'In [7]: pi**10
Out[7]: 9.364805e+04In [8]: %precision
Out[8]: u'%r'In [9]: pi**10
Out[9]: 93648.047476082982
参考:
- CSDN文章:Python不使用科学计数输出
- Stack Overflow: Print exact value of PyTorch tensor (floating point precision)
- pytorch官方文档:https://pytorch.org/docs/master/generated/torch.set_printoptions.html#torch.set_printoptions
- ipython官方文档: Built-in magic commands-%precision