mindspore.Tensor.sum

Tensor.sum(dim=None, keepdim=False, *, dtype=None) Tensor

Calculate sum of Tensor elements over a given dim.

Note

The dim with tensor type is only used for compatibility with older versions and is not recommended.

Parameters
  • dim (Union[None, int, tuple(int), list(int), Tensor], optional) – Dimensions along which a sum is performed. If None , sum all the elements of the self tensor. If the dim is a tuple or list of ints, a sum is performed on all the dimensions specified in the tuple. Must be in the range \([-self.ndim, self.ndim)\) . Default: None .

  • keepdim (bool, optional) – Whether the output tensor has dim retained or not. If True , keep these reduced dimensions and the length is 1. If False , don't keep these dimensions. Default: False .

Keyword Arguments

dtype (mindspore.dtype, optional) – The desired data type of returned Tensor. Default: None .

Returns

A Tensor, sum of elements over a given dim in self.

Raises
  • TypeError – If dim is not an int, tulpe(int), list(int), Tensor or None.

  • ValueError – If dim is not in the range \([-self.ndim, self.ndim)\) .

  • TypeError – If keepdim is not a bool.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor
>>> from mindspore import dtype as mstype
>>> x = Tensor(np.array([[[1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3]],
...                      [[4, 4, 4, 4, 4, 4], [5, 5, 5, 5, 5, 5], [6, 6, 6, 6, 6, 6]],
...                      [[7, 7, 7, 7, 7, 7], [8, 8, 8, 8, 8, 8], [9, 9, 9, 9, 9, 9]]]), mstype.float32)
>>> out = Tensor.sum(x)
>>> print(out)
270.0
>>> out = Tensor.sum(x, dim=2)
>>> print(out)
[[ 6. 12. 18.]
[24. 30. 36.]
[42. 48. 54.]]
>>> out = Tensor.sum(x, dim=2, keepdim=True)
>>> print(out)
[[[ 6.]
[12.]
[18.]]
[[24.]
[30.]
[36.]]
[[42.]
[48.]
[54.]]]
Tensor.sum(axis=None, dtype=None, keepdims=False, initial=None) Tensor

Return sum of tensor elements over a given axis.

Note

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. The axis with tensor type is only used for compatibility with older versions and is not recommended.

Parameters
  • axis (Union[None, int, tuple(int), list(int), Tensor], optional) – Axis or axes along which a sum is performed. Default: None . If None , sum all the elements of the self tensor. If the axis is negative, it counts from the last to the first axis. If the axis is a tuple or list of ints, a sum is performed on all the axes specified in the tuple or list instead of a single axis or all the axes as before.

  • dtype (mindspore.dtype, optional) – Default: None . Overrides the dtype of the output Tensor.

  • keepdims (bool, optional) – If this is set to True , the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the self array. If the default value is passed, then keepdims will not be passed through to the sum method of sub-classes of ndarray, however any non-default value will be. If the sub-class method does not implement keepdims any exceptions will be raised. Default: False .

  • initial (scalar, optional) – Starting value for the sum. Default: None .

Returns

Tensor. A tensor with the same shape as self, with the specified axis removed. If the self tensor is a 0-d array, or if the axis is None , a scalar is returned.

Raises
  • TypeError – If self is not array_like, or axis is not int, tuple of ints, list of ints or Tensor, or keepdims is not integer, or initial is not scalar.

  • ValueError – If any axis is out of range or duplicate axes exist.

See also

Supported Platforms:

Ascend GPU CPU

Examples

>>> import numpy as np
>>> from mindspore import Tensor
>>> input_x = Tensor(np.array([-1, 0, 1]).astype(np.float32))
>>> print(input_x.sum())
0.0
>>> input_x = Tensor(np.arange(10).reshape(2, 5).astype(np.float32))
>>> print(input_x.sum(axis=1))
[10. 35.]