即使我使用float64,张量中的有效数字个数为5。我如何使其超过8个数字?

huangapple go评论63阅读模式
英文:

Even if I use float64, the number of significant digits in tensor is 5. how can I make it more than 8 digits?

问题

I would like to convert the following numpy to tensor with at least 8 significant digits.

array([-8.32457799, -8.18170165, -8.03901151, ..., -4.34838355,
       -4.33105147, -4.31420002])

Therefore, the following code was executed to convert it to pytorch tensor.

torch.tensor(numpy_array, dtype=torch.float64)

However, the number of significant digits is reduced to five. I want more than 8 digits.

print(torch.tensor(numpy_array, dtype=torch.float64))
print(torch.tensor(numpy_array, dtype=torch.float64)[0])

# tensor([-8.3246, -8.1817, -8.0390,  ..., -4.3484, -4.3311, -4.3142],
       dtype=torch.float64)
# tensor(-8.3246, dtype=torch.float64)
英文:

I would like to convert the following numpy to tensor with at least 8 significant digits.

array([-8.32457799, -8.18170165, -8.03901151, ..., -4.34838355,
       -4.33105147, -4.31420002])

Therefore, the following code was executed to convert it to pytorch tensor.

torch.tensor(numpy_array, dtype=torch.float64)

However, the number of significant digits is reduced to five. I want more than 8 digits.

print(torch.tensor(numpy_array, dtype=torch.float64))
print(torch.tensor(numpy_array, dtype=torch.float64)[0])

# tensor([-8.3246, -8.1817, -8.0390,  ..., -4.3484, -4.3311, -4.3142],
       dtype=torch.float64)
# tensor(-8.3246, dtype=torch.float64)

答案1

得分: 0

这只是 PyTorch 的默认打印选项,用于提高可读性。您的张量中的值实际上具有更高的精度,但在打印时,PyTorch 会对其进行四舍五入。如果要以更高精度打印,可以使用 [torch.set_printoptions] 来更改此选项:

import numpy as np
import torch

torch.set_printoptions(precision=8)

numpy_array = np.array([-8.32457799, -8.18170165, -8.03901151, -4.34838355, -4.33105147, -4.31420002])
tensor_array = torch.tensor(numpy_array, dtype=torch.float64)

print(tensor_array)
print(tensor_array[0])

这将允许您在打印张量时看到更多小数位。

输出:

tensor([-8.32457799, -8.18170165, -8.03901151, -4.34838355, -4.33105147,
        -4.31420002], dtype=torch.float64)
tensor(-8.32457799, dtype=torch.float64)

请注意,这实际上不会更改计算的精度,只是在打印时显示张量的方式。在此更改之前,计算已经以完全精度进行。

英文:

This is just a default print option of PyTorch for readability. The values in your tensor actually have higher precision, but PyTorch rounds them off when printing. If you want to print with more precision, you can change this option using <code>torch.<b>set_printoptions</b></code>:

import numpy as np
import torch

torch.set_printoptions(precision=8)

numpy_array = np.array([-8.32457799, -8.18170165, -8.03901151, -4.34838355, -4.33105147, -4.31420002])
tensor_array = torch.tensor(numpy_array, dtype=torch.float64)

print(tensor_array)
print(tensor_array[0])

This will allow you to see more decimal places when printing the tensor.

Output:

tensor([-8.32457799, -8.18170165, -8.03901151, -4.34838355, -4.33105147,
        -4.31420002], dtype=torch.float64)
tensor(-8.32457799, dtype=torch.float64)

Note that this does not actually change the precision of the calculations, just how the tensors are displayed when printed. The calculations were already being done at full precision before this change.

huangapple
  • 本文由 发表于 2023年5月13日 15:24:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241541.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定