0

I'm new to Pytorch, I need to simply check if a Tensor built of 1D list (of floats) sums up to 10 (exactly) or not.

I found that:

t.sum(dtype=torch.float16).item() == 10

Is the shortest way to get the sum (where t is the tensor), as if I don't set the dtype sum may not be accurate (values that sums up to 10 are actually summed to 10.0000001192092896 using torch.sum without reducing to float16). Then I must call item(), as I could not find any other way to test the actual value of the sum.

Is it really the shortest way to do this? Seems a bit too much to just get a sum of a 1D array.

ChikChak
  • 936
  • 19
  • 44
  • 1
    https://stackoverflow.com/questions/5595425/what-is-the-best-way-to-compare-floats-for-almost-equality-in-python – Alexey Birukov Mar 05 '22 at 14:36
  • Does this answer your question? [What is the best way to compare floats for almost-equality in Python?](https://stackoverflow.com/questions/5595425/what-is-the-best-way-to-compare-floats-for-almost-equality-in-python) – Zephyr Mar 06 '22 at 10:03

0 Answers0