Skip to content
fn

nan_to_num_op

→TensorImplPtr
int nan_to_num_op(const int & a, int nan_val, int posinf_val, int neginf_val)

Python wrappers

Public Python APIs implemented by this engine symbol.

Replace NaN and ±∞\pm\infty values with user-specified finite substitutes, preserving dtype and shape.

Differentiable: the gradient passes through finite slots and is zero where a value was replaced (NanToNumBackward).

Math

\text{nan\_val} & x_i \text{ is NaN} \\ \text{posinf\_val} & x_i = +\infty \\ \text{neginf\_val} & x_i = -\infty \\ x_i & \text{otherwise} \end{cases}$$ **Shape** Output shape equals input shape (elementwise). **See Also** `isnan_op`, `isinf_op`, `isfinite_op`.

Parameters

aTensorImplPtr
Input tensor of any floating-point dtype. Integer inputs are passed through unchanged (no non-finite values to replace).
nan_valstd::optional<double>
Replacement for NaN positions. nullopt means 0.0.
posinf_valstd::optional<double>
Replacement for +∞+\infty positions. nullopt means the largest finite value of a's dtype (65504 for float16, ~3.3895e+38 for bfloat16, ~3.4028e+38 for float32, ~1.7977e+308 for float64; per part for complex).
neginf_valstd::optional<double>
Replacement for −∞-\infty positions. nullopt means the lowest finite value of a's dtype.

Returns

TensorImplPtr

New tensor with the same shape, dtype, and device as a; non-finite slots are replaced according to the three scalars.

Notes

The defaults used to be float32's extremes for every dtype. In float16 and bfloat16 those round to infinity, so a default call left every infinity in place. float64 got 3.4e38 where the reference framework gives 1.8e308. An explicit replacement is rounded into the dtype as a cast would round it, so one the dtype cannot hold becomes its infinity (float16 posinf=1e10 gives inf), as in the reference framework.