-2

I am running a deep network on GPU so ideally all of my code should work effeciently under GPU (so torch rather than numpy) and I am trying to round the tensor values with torch.round() but the result is not expected:

testing_value = torch.tensor(3.440000057220459)
torch.round(testing_value, decimals = 2)

this output results tensor(3.4400), but shouldn't it be tensor(3.44)?

How can I fix this?

Hawkeye
  • 13
  • 5
  • 1
    Welcome to floating points. You simply can't have exactly the number `3.44` with standard floats. There is nothing to fix here, the number is as close to `3.44` as it's going to get. – Mikael Öhman Oct 20 '22 at 19:39

1 Answers1

-1

This how the code should look like:

testing_value = torch.tensor(3.440000057220459)
torch.set_printoptions(precision=2)
torch.round(testing_value, decimals = 2)

Output:

tensor(3.44)
rafathasan
  • 524
  • 3
  • 15