我正在使用 libtorch C++。在 python 版本中,我们可以通过调用张量的值来轻松检查张量的numpy值,在numpy我们有np.isnan(). 我想知道是否有内置函数libtorch C++来检查张量是否有任何NAN价值?
谢谢, 阿夫辛
补充法比奥的回答(我的声誉太低,无法评论):
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
试试at::isnan。
int main() {
torch::Tensor tensor = torch::rand({2, 3});
std::cout << tensor << std::endl;
std::cout << at::isnan(tensor) << std::endl;
return 0;
}
注意:我必须安装 libtorch 的夜间版本,因为稳定版本没有isnan.