1
test = ['0.01171875', '0.01757812', '0.02929688']  
test = np.array(test).astype(float)   
print(test)  
->[0.01171875 0.01757812 0.02929688]


test_torch = torch.from_numpy(test)  
test_torch  
->tensor([0.0117, 0.0176, 0.0293], dtype=torch.float64)

It looks like from_numpy() loses some precision there... If I want to convert this float data exactly the same, what kind of functions do I use?

Arya McCarthy
  • 8,554
  • 4
  • 34
  • 56
user10735026
  • 15
  • 1
  • 4
  • Does this answer your question? [How to convert a list or numpy array to a 1d torch tensor?](https://stackoverflow.com/questions/42894882/how-to-convert-a-list-or-numpy-array-to-a-1d-torch-tensor) – Dwa Aug 04 '21 at 15:27
  • @Dwa I don't think this is the issue here, OP already knows how to convert from NumPy to PyTorch. However, OP is concerned with - what seems to be - a lack of precision after conversion. – Ivan Aug 04 '21 at 18:38

1 Answers1

4

The data precision is the same, it's just that the format used by PyTorch to print the values is different, it will round the floats down:

>>> test_torch = torch.from_numpy(test)
>>> test_torch
tensor([0.0117, 0.0176, 0.0293], dtype=torch.float64)

You can check that it matches your original input by converting to a list with tolist:

>>> test_torch.tolist()
[0.01171875, 0.01757812, 0.02929688]
Ivan
  • 34,531
  • 8
  • 55
  • 100