快捷方式

torch.nan_to_num

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

input 中的 NaN、正無窮和負無窮值分別替換為由 nanposinfneginf 指定的值。預設情況下,NaN 將被零替換,正無窮將被 input 的 dtype 可表示的最大有限值替換,負無窮將被 input 的 dtype 可表示的最小有限值替換。

引數
  • input (Tensor) – 輸入張量。

  • nan (Number, optional) – 用於替換 NaN 的值。預設為零。

  • posinf (Number, optional) – 如果是 Number,則用於替換正無窮值的值。如果是 None,正無窮值將被 input 的 dtype 可表示的最大有限值替換。預設為 None。

  • neginf (Number, optional) – 如果是 Number,則用於替換負無窮值的值。如果是 None,負無窮值將被 input 的 dtype 可表示的最小有限值替換。預設為 None。

關鍵字引數

out (Tensor, optional) – 輸出張量。

示例

>>> 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 的完整開發者文件

檢視文件

教程

獲取針對初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並獲取問題的答案

檢視資源