Search code examples
pythonpytorch

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)

Solution

  • 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 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])
    

    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.