mindspore.ops.nansum
- mindspore.ops.nansum(input, axis=None, keepdims=False, *, dtype=None)[source]
Computes sum of input over a given dimension, treating NaNs as zero.
- Parameters
input (Tensor) – The input Tensor.
axis (Union[int, tuple(int)], optional) – The dimensions to reduce. Supposed the rank of input is r, axis must be in the range [-rank(input), rank(input)). Default:
None
, all dimensions are reduced.keepdims (bool, optional) – Whether the output Tensor keeps dimensions or not. Default:
False
.
- Keyword Arguments
dtype (
mindspore.dtype
, optional) – The dtype of output Tensor. Default:None
.- Returns
Tensor, the sum of input input in the given dimension dim, treating NaNs as zero.
If axis is None, keepdims is False, the output is a 0-D Tensor representing the sum of all elements in the input Tensor.
If axis is int, set as 2, and keepdims is False, the shape of output is \((input_1, input_3, ..., input_R)\).
If axis is tuple(int) or list(int), set as (2, 3), and keepdims is False, the shape of output is \((input_1, input_4, ..., input_R)\).
- Raises
TypeError – If input is not Tensor.
TypeError – If keepdims is not a bool.
TypeError – If the dtype of input or dtype is complex type.
ValueError – If 'axis' not in [-rank(input), rank(input)).
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore >>> import numpy as np >>> from mindspore import Tensor, ops >>> x = Tensor(np.array([[float("nan"), 2, 3], [1, 2, float("nan")]]), mindspore.float32) >>> output1 = ops.nansum(x, axis=0, keepdims=False, dtype=mindspore.float32) >>> output2 = ops.nansum(x, axis=0, keepdims=True, dtype=mindspore.float32) >>> print(output1) [1. 4. 3.] >>> print(output2) [[1. 4. 3.]]