mindspore.mint.add

View Source On AtomGit
mindspore.mint.add(input, other, *, alpha=1) Tensor[source]

Scales the other value by alpha and adds it to input.

\[out_{i} = input_{i} + alpha \times other_{i}\]

Note

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

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

Parameters
  • input (Union[Tensor, number.Number, bool]) – input is a number.Number or a bool or a tensor whose data type is number or bool.

  • 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, optional) – A scaling factor applied to other. Default 1.

Returns

Tensor with a shape that is the same as the broadcasted shape of the input and other, and the data type is the one with higher precision or higher digits among input, 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 input and other are not of type float.

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

Supported Platforms:

Ascend

Examples

>>> import numpy as np
>>> import mindspore
>>> from mindspore import Tensor, mint
>>> x = Tensor(1, mindspore.int32)
>>> y = Tensor(np.array([4, 5, 6]).astype(np.float32))
>>> alpha = 0.5
>>> output = mint.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