fn
nan_to_num
→Tensornan_to_num(input: Tensor, nan: float | None = ..., posinf: float | None = ..., neginf: float | None = ...)Replace NaN, positive and negative infinity entries with finite values.
Returns a tensor identical to input except that NaN, +inf and -inf
entries are substituted by nan, posinf and neginf respectively. When
a replacement argument is None the engine picks a sensible default
(typically zero for NaN and the dtype-specific finite extrema for the
infinities).
Parameters
inputTensorInput tensor.
nanfloat or None= ...Value used to replace
NaN entries.posinffloat or None= ...Value used to replace
+inf entries.neginffloat or None= ...Value used to replace
-inf entries.Returns
TensorSanitized tensor with the same shape and dtype as input.
Notes
Useful as a numerical safety net after operations that can produce non-finite intermediate values (logs, divisions, large exponentials). Gradient flows through the substitution unchanged.
Examples
>>> import lucid
>>> x = lucid.tensor([1.0, float('nan'), float('inf'), float('-inf')])
>>> lucid.nan_to_num(x, nan=0.0, posinf=1e10, neginf=-1e10)
Tensor([1.0, 0.0, 1e10, -1e10])