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.Tensor.add

Tensor.add(other) Tensor

Adds other value to self element-wise.

outi=selfi+otheri

Note

  • When self and other have different shapes, they must be able to broadcast to a common shape.

  • self and other can not be bool type at the same time, [True, Tensor(True, bool_), Tensor(np.array([True]), bool_)] are all considered bool type.

  • self and other comply with the implicit type conversion rules to make the data types consistent.

  • The dimension of self should be greater than or equal to 1.

Parameters

other (Union[Tensor, number.Number, bool]) – other is a number.Number or a bool or a tensor whose data type is number or bool_.

Returns

Tensor with a shape that is the same as the broadcasted shape of self and other, and the data type is the one with higher precision or higher digits between self and other.

Raises

TypeError – If other is not one of the following: Tensor, number.Number, bool.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import numpy as np
>>> import mindspore
>>> from mindspore import Tensor
>>> # case 1: x and y are both Tensor.
>>> x = Tensor(np.array([1, 2, 3]).astype(np.float32))
>>> y = Tensor(np.array([4, 5, 6]).astype(np.float32))
>>> output = Tensor.add(x, y)  # x.add(y)
>>> print(output)
[5. 7. 9.]
>>> # case 2: x is a scalar and y is a Tensor
>>> x = Tensor(1, mindspore.int32)
>>> y = Tensor(np.array([4, 5, 6]).astype(np.float32))
>>> output = Tensor.add(x, y)  # x.add(y)
>>> print(output)
[5. 6. 7.]
>>> # the data type of x is int32, the data type of y is float32,
>>> # and the output is the data format of higher precision float32.
>>> print(output.dtype)
Float32
Tensor.add(other, *, alpha=1) Tensor

Adds scaled other value to self.

outi=selfi+alpha×otheri

Note

  • When self and other have different shapes, they must be able to broadcast to a common shape.

  • self, other and alpha comply with the implicit type conversion rules to make the data types consistent.

Parameters

other (Union[Tensor, number.Number, bool]) –

other is a number.Number or a bool or a tensor whose data type is number or bool_.

Keyword Arguments

alpha (number.Number) – A scaling factor applied to other, default 1.

Returns

Tensor with a shape that is the same as the broadcasted shape of the self and other, and the data type is the one with higher precision or higher digits among self, other and alpha.

Raises
  • TypeError – If the type of other or alpha is not one of the following: Tensor, number.Number, bool.

  • TypeError – If alpha is of type float but self and other are not of type float.

  • TypeError – If alpha is of type bool but self and other are not of type bool.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import numpy as np
>>> import mindspore
>>> from mindspore import Tensor
>>> x = Tensor(1, mindspore.int32)
>>> y = Tensor(np.array([4, 5, 6]).astype(np.float32))
>>> alpha = 0.5
>>> output = Tensor.add(x, y, alpha=alpha)  # x.add(y, alpha=alpha)
>>> print(output)
[3. 3.5 4.]
>>> # the data type of x is int32, the data type of y is float32,
>>> # alpha is a float, and the output is the data format of higher precision float32.
>>> print(output.dtype)
Float32