Prevent rounding numbers imported from numpy

Hello everyone,
I just imported an array from numpy into pytorch:

import numpy as np
import torch

a = np.array([ 1.07790701e-01,  5.27183563e-02,  1.03966855e-01])
print(a)
# [0.1077907  0.05271836 0.10396685]

b = torch.from_numpy(a)
print(b)
# tensor([0.1078, 0.0527, 0.1040], dtype=torch.float64)

Even though the tensor is float64 type, every value in tensorb is rounded up to 2 decimals compared with the original array a.

How can I keep the values of b unchanged from a?

Many thanks,

It’s not a rounding error, it’s about print. PyTorch uses less precision to print.

a-b.numpy()
Out[3]: array([0., 0., 0.])
np.linalg.norm((a-b.numpy()))
Out[4]: 0.0

print(b[0])
tensor(0.1078, dtype=torch.float64)
print(b[0].item())
0.107790701
2 Likes

To increase the precision for print outputs, just set:

torch.set_printoptions(precision=10)
2 Likes