快捷方式

torch.nan_to_num

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

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

参数
  • input (Tensor) – 输入张量。

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

  • posinf (数字, 可选) – 如果是数字,则为用于替换正无穷大值的值。如果为 None,则正无穷大值将替换为 input 的 dtype 可表示的最大有限值。默认为 None。

  • neginf (数字, 可选) – 如果是数字,则为用于替换负无穷大值的值。如果为 None,则负无穷大值将替换为 input 的 dtype 可表示的最小有限值。默认为 None。

关键字参数

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

示例

>>> 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])

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

获取面向初学者和高级开发者的深入教程

查看教程

资源

查找开发资源并获得问题解答

查看资源