how to count numbers of nan in tensor pytorch
I used to use
assert torch.isnan(myTensor.view(-1)).sum().item()==0
to count whether if there is some nan in my tensor. But I found this way inefficient and may be wrong
Is there any better solution? THx 
You can use torch.nonzero(torch.isnan(myTensor.view(-1))
, which might be a bit more efficient.
The probably most efficient way to do so should be
assert not torch.isnan(myTensor).any()
as there is no view
included (since it isnβt necessary here) , no dimension has to be inferred, and calling any()
on a ByteTensor should be quite efficient itself.
2 Likes
Iβd say your solution is brilliant