torch.nan_to_num¶
- torch.nan_to_num(input, nan=0.0, posinf=None, neginf=None, *, out=None) Tensor¶
將
input中的NaN、正無窮和負無窮值分別替換為由nan、posinf和neginf指定的值。預設情況下,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])