首页 > 解决方案 > Torch C++:检查 NAN 的 API

问题描述

我正在使用 libtorch C++。在 python 版本中,我们可以通过调用张量的值来轻松检查张量的numpy值,在numpy我们有np.isnan(). 我想知道是否有内置函数libtorch C++来检查张量是否有任何NAN价值?

谢谢, 阿夫辛

标签: pytorchtorchlibtorch

解决方案


补充法比奥的回答(我的声誉太低,无法评论):

assert如果你真的想在 an or条件下使用有关 NAN 的信息,if你需要将它从 a 转换torch::Tensor为 C++bool像这样

torch::Tensor myTensor;
// do something
auto tensorIsNan = at::isnan(myTensor).any().item<bool>(); // will be of type bool

推荐阅读