python - 打印 PyTorch 张量的精确值(浮点精度)

标签 python pytorch

我正在尝试像这样打印 torch.FloatTensor:

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

这样我可以获得如下值:

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 位:

0.1234567891+01

对于其他 python 数字对象,我可以通过以下方式获取它:

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

但是在张量的情况下,我得到这个错误:

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

如何打印更精确的张量值?

最佳答案

您可以设置精度选项:

torch.set_printoptions(precision=10)

documentation page 上有更多格式设置选项, 它与 numpy 的非常相似。

关于python - 打印 PyTorch 张量的精确值(浮点精度),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47483733/

相关文章:

python - Pandas 不掉列

python - "from __future__ imports must occur at the beginning of the file": what defines the beginning of the file?

python - 在 pytorch 中使用交叉熵损失时,我应该使用 softmax 作为输出吗?

python - 导入错误:没有名为 'torchvision.datasets.mnist' 的模块

linux - Pytorch说CUDA不可用

python - python 2.x 中 json.dump 后 json.loads 上的 UnicodeDecodeError 与 Ensure_ascii=False

python - 字典将值赋予某个键,但其他键获得相同的值

python - 如何通过不包含子字符串的单元格过滤 Pandas 数据框?

python-3.x - PyTorch 中的 softmax 变暗和变量 volatile

python - 在 pytorch(或 Numpy)中更有效地实现这个方程