mindspore.ops.Add
- class mindspore.ops.Add[源代码]
两个输入Tensor逐元素相加。
更多参考详见
mindspore.ops.add()
。说明
两个输入中至少有一个Tensor,当两个输入具有不同的shape时,它们的shape必须要能广播为一个共同的shape。
两个输入不能同时为bool类型。[True, Tensor(True, bool_), Tensor(np.array([True]), bool_)]等都为bool类型。
两个输入遵循隐式类型转换规则,使数据类型保持一致。
- 输入:
- 输出:
Tensor,shape与输入 x、 y 广播后的shape相同,数据类型为两个输入中精度较高的类型。
- 支持平台:
Ascend
GPU
CPU
样例:
>>> import mindspore >>> import numpy as np >>> from mindspore import Tensor, ops >>> # case 1: x and y are both Tensor. >>> add = ops.Add() >>> x = Tensor(np.array([1, 2, 3]).astype(np.float32)) >>> y = Tensor(np.array([4, 5, 6]).astype(np.float32)) >>> output = add(x, y) >>> print(output) [5. 7. 9.] >>> # case 2: x is a scalar and y is a Tensor >>> add = ops.Add() >>> x = Tensor(1, mindspore.int32) >>> y = Tensor(np.array([4, 5, 6]).astype(np.float32)) >>> output = add(x, 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 >>> # case 3: one of x and y is a bool scalar >>> add = ops.Add() >>> x = True >>> y = Tensor(np.array([4, 5, 6]).astype(np.float32)) >>> output = add(x, y) >>> print(output) [5. 6. 7.] >>> # case 4: one of x and y is a bool Tensor >>> add = ops.Add() >>> x = Tensor(np.array([True, False, True]), mindspore.bool_) >>> y = Tensor(np.array([4, 5, 6]).astype(np.float32)) >>> output = add(x, y) >>> print(output) [5. 5. 7.]