PyTorch torch.isnan 函数

约 274 字 更新于 2026-11-18 5iyo.com

PyTorch torch.isnan 函数


torch.isnan 是 PyTorch 中用于检查是否为 NaN 值的函数。

函数定义

torch.isnan(input)

使用示例

实例

import torch
import math

x = torch.tensor([1.0, math.nan, 3.0, float('nan')])

result = torch.isnan(x)

print(result)

输出结果为:

tensor([False,  True, False,  True])