mindspore.mint.sum

View Source On AtomGit
mindspore.mint.sum(input, *, dtype=None) Tensor[source]

Calculate sum of all elements in tensor.

Parameters

input (Tensor) – The input tensor.

Keyword Arguments

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

Returns

Tensor

Supported Platforms:

Ascend

Examples

>>> import mindspore
>>> x = mindspore.tensor([[[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]]], mindspore.float32)
>>> mindspore.mint.sum(x)
Tensor(shape=[], dtype=Float32, value= 270)
mindspore.mint.sum(input, dim, keepdim=False, *, dtype=None) Tensor[source]

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
  • input (Tensor) – The input tensor.

  • dim (Union[int, tuple(int), list(int), Tensor]) – Specify the dimension to compute.

  • keepdim (bool, optional) – Whether the output tensor has 'dim' retained. Default False .

Keyword Arguments

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

Returns

Tensor

Supported Platforms:

Ascend

Examples

>>> import mindspore
>>> x = mindspore.tensor([[[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]]], mindspore.float32)
>>> mindspore.mint.sum(x, dim=2)
Tensor(shape=[3, 3], dtype=Float32, value=
[[ 6.00000000e+00,  1.20000000e+01,  1.80000000e+01],
 [ 2.40000000e+01,  3.00000000e+01,  3.60000000e+01],
 [ 4.20000000e+01,  4.80000000e+01,  5.40000000e+01]])
>>> mindspore.mint.sum(x, dim=2, keepdim=True)
Tensor(shape=[3, 3, 1], dtype=Float32, value=
[[[ 6.00000000e+00],
  [ 1.20000000e+01],
  [ 1.80000000e+01]],
 [[ 2.40000000e+01],
  [ 3.00000000e+01],
  [ 3.60000000e+01]],
 [[ 4.20000000e+01],
  [ 4.80000000e+01],
  [ 5.40000000e+01]]])
>>> mindspore.mint.sum(x, dim=[1, 2])
Tensor(shape=[3], dtype=Float32, value= [ 3.60000000e+01,  9.00000000e+01,  1.44000000e+02])