快捷方式

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) – 如果是數字,則是用於替換正無窮大值的值。 如果為 None,則正無窮大值將被替換為 input 的 dtype 可表示的最大有限值。 預設值為 None。

  • neginf (Number, optional) – 如果是數字,則是用於替換負無窮大值的值。 如果為 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 的全面開發者文件

檢視文件

教學

取得針對初學者和進階開發者的深入教學

檢視教學

資源

尋找開發資源並獲得您的問題解答

檢視資源