Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Torch C++: API to check NAN

I am using libtorch C++. In python version we can easily check the value of a tensor by calling its numpy value, and in numpy we have np.isnan(). I was wondering if there is a built in function in libtorch C++ to check whether a tensor has any NAN value?

Thanks, Afshin

like image 609
Afshin Oroojlooy Avatar asked Dec 02 '25 15:12

Afshin Oroojlooy


1 Answers

Adding on to Fábio's answer (my reputation is too low to comment):

If you actually want to use the information about NANs in an assert or if condition you need convert it from a torch::Tensor to a C++ bool like so

torch::Tensor myTensor;
// do something
auto tensorIsNan = at::isnan(myTensor).any().item<bool>(); // will be of type bool
like image 53
Jan Ca Avatar answered Dec 05 '25 22:12

Jan Ca



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!