Shortcuts

torch.nan_to_num

torch.nan_to_num(input, nan=0.0, posinf=None, neginf=None, *, out=None) 张量

替换 NaN、正无穷大和负无穷大值在 input 中分别用 nanposinfneginf 指定的值。 默认情况下,NaN 被替换为零,正无穷大被替换为 input 的 dtype 可表示的最大有限值,负无穷大 被替换为 input 的 dtype 可表示的最小有限值。

Parameters
  • 输入 (张量) – 输入张量。

  • nan (数字, 可选) – 用于替换 NaN 的值。默认值为零。

  • posinf数字可选)——如果是一个数字,则用该值替换正无穷值。 如果为 None,则正无穷值将被替换为 input 的数据类型所能表示的最大有限值。 默认值为 None。

  • neginf (数字, 可选) – 如果是一个数字,则用该值替换负无穷值。 如果为 None,则负无穷值将被替换为 input 的数据类型所能表示的最小有限值。 默认值为 None。

Keyword Arguments

输出 (张量, 可选) – 输出张量。

示例:

>>> x = torch.tensor([float('nan'), float('inf'), -float('inf'), 3.14])
>>> torch.nan_to_num(x)
tensor([ 0.0000e+00,  3.4028e+38, -3.4028e+38,  3.1400e+00])
>>> torch.nan_to_num(x, nan=2.0)
tensor([ 2.0000e+00,  3.4028e+38, -3.4028e+38,  3.1400e+00])
>>> torch.nan_to_num(x, nan=2.0, posinf=1.0)
tensor([ 2.0000e+00,  1.0000e+00, -3.4028e+38,  3.1400e+00])