fn
nansum
→Tensornansum(x: Tensor, dim: int | Sequence[int] | None = ..., keepdim: bool = ...)Sum the tensor, treating NaN entries as zero.
A NaN-safe variant of lucid.sum. NaN values are replaced by
0 before the reduction, so they neither contaminate the result
nor contribute to it.
Parameters
xTensorInput tensor (any floating-point dtype).
dimint | Sequence[int] | NoneAxis or axes along which to sum.
None (default) reduces over
the entire tensor.keepdimboolIf
True, retains the reduced dimensions with size 1.
Defaults to False.Returns
TensorReduced tensor.
Notes
With the set of indices being reduced and an indicator,
The gradient at NaN positions is zero — those entries contribute nothing to the forward sum, so they cannot influence it through perturbation.
Examples
>>> import lucid
>>> import math
>>> x = lucid.tensor([1.0, math.nan, 3.0])
>>> lucid.nansum(x)
Tensor(4.)