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.