Document feedback

Question document fragment

When a question document fragment contains a formula, it is displayed as a space.

Submission type
issue

It's a little complicated...

I'd like to ask someone.

Please select the submission type

Problem type
Specifications and Common Mistakes

- Specifications and Common Mistakes:

- Misspellings or punctuation mistakes,incorrect formulas, abnormal display.

- Incorrect links, empty cells, or wrong formats.

- Chinese characters in English context.

- Minor inconsistencies between the UI and descriptions.

- Low writing fluency that does not affect understanding.

- Incorrect version numbers, including software package names and version numbers on the UI.

Usability

- Usability:

- Incorrect or missing key steps.

- Missing main function descriptions, keyword explanation, necessary prerequisites, or precautions.

- Ambiguous descriptions, unclear reference, or contradictory context.

- Unclear logic, such as missing classifications, items, and steps.

Correctness

- Correctness:

- Technical principles, function descriptions, supported platforms, parameter types, or exceptions inconsistent with that of software implementation.

- Incorrect schematic or architecture diagrams.

- Incorrect commands or command parameters.

- Incorrect code.

- Commands inconsistent with the functions.

- Wrong screenshots.

- Sample code running error, or running results inconsistent with the expectation.

Risk Warnings

- Risk Warnings:

- Lack of risk warnings for operations that may damage the system or important data.

Content Compliance

- Content Compliance:

- Contents that may violate applicable laws and regulations or geo-cultural context-sensitive words and expressions.

- Copyright infringement.

Please select the type of question

Problem description

Describe the bug so that we can quickly locate the problem.

mindspore.mint.sum

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

A Tensor, sum of all elements in input.

Raises

TypeError – If input is not a Tensor.

Supported Platforms:

Ascend

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, mint
>>> 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 = mint.sum(x)
>>> print(out)
270.0
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]) – Dimensions along which a sum is performed. 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 [input.ndim,input.ndim) .

  • keepdim (bool) – 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 input.

Raises
  • TypeError – If input is not a Tensor.

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

  • ValueError – If dim is not in the range [input.ndim,input.ndim) .

  • TypeError – If keepdim is not a bool.

Supported Platforms:

Ascend

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, mint
>>> 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 = mint.sum(x)
>>> print(out)
270.0
>>> out = mint.sum(x, dim=2)
>>> print(out)
[[ 6. 12. 18.]
 [24. 30. 36.]
 [42. 48. 54.]]
>>> out = mint.sum(x, dim=2, keepdim=True)
>>> print(out)
[[[ 6.]
  [12.]
  [18.]]
 [[24.]
  [30.]
  [36.]]
 [[42.]
  [48.]
  [54.]]]