英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论