335 Star 1.5K Fork 861

MindSpore / docs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
add.md 3.20 KB
一键复制 编辑 原始数据 按行查看 历史
俞涵 提交于 2023-01-29 16:21 . add mindquantum and modify url

比较与tf.math.add的功能差异

tf.math.add

tf.math.add(x, y, name=None) -> Tensor

更多内容详见tf.math.add

mindspore.ops.add

mindspore.ops.add(x, y) -> Tensor

更多内容详见mindspore.ops.add

差异对比

TensorFlow:计算输入x和输入y的元素和,返回一个与x具有相同类型的Tensor。

MindSpore:MindSpore此API实现功能与TensorFlow一致,仅参数名不同。

分类 子类 TensorFlow MindSpore 差异
参数 参数1 x x -
参数2 y y -
参数3 name - 不涉及

代码示例1

当x和y输入都为Tensor且数据类型一致时,MindSpore和TensorFlow输出结果一致。

# TensorFlow
import tensorflow as tf
import numpy as np

x = tf.constant(np.array([[1,2]]).astype(np.float32))
y = tf.constant(np.array([[1],[2]]).astype(np.float32))
output = tf.math.add(x, y)
print(output.numpy())
# [[2. 3.]
#  [3. 4.]]

# MindSpore
import mindspore
from mindspore import Tensor
import numpy as np

x = Tensor(np.array([1, 2]).astype(np.float32))
y = Tensor(np.array([[1], [2]]).astype(np.float32))
output = mindspore.ops.add(x, y)
print(output.asnumpy())
# [[2. 3.]
#  [3. 4.]]

代码示例2

TensorFlow支持标量相加,且x和y数据类型必须保持一致,MindSpore 1.8.1版本暂不支持标量相加,,但x和y数据类型可以不同。为了得到相同的结果,将标量转化为Tensor进行计算。

# TensorFlow
import tensorflow as tf
import numpy as np

x = np.array([[1,2]]).astype(np.float32)
y = np.array([[1],[2]]).astype(np.float32)
output = tf.math.add(x, y)
print(output.numpy())
# [[2. 3.]
#  [3. 4.]]

# MindSpore
import mindspore
from mindspore import Tensor
import numpy as np

x = Tensor(np.array([1, 2]).astype(np.int32))
y = Tensor(np.array([[1], [2]]).astype(np.float32))
output = mindspore.ops.add(x, y)
print(output.asnumpy())
# [[2. 3.]
#  [3. 4.]]

代码示例3

TensorFlow的name参数用于定义操作的名称,对计算结果不影响。

# TensorFlow
from unicodedata import name
import tensorflow as tf
import numpy as np

x = tf.constant(np.array([[1,2]]).astype(np.float32))
y = tf.constant(np.array([[1],[2]]).astype(np.float32))
output = tf.math.add(x, y, name="add")
print(output.numpy())
# [[2. 3.]
#  [3. 4.]]

# MindSpore
import mindspore
from mindspore import Tensor
import numpy as np

x = Tensor(np.array([1, 2]).astype(np.float32))
y = Tensor(np.array([[1], [2]]).astype(np.float32))
output = mindspore.ops.add(x, y)
print(output.asnumpy())
# [[2. 3.]
#  [3. 4.]]
1
https://gitee.com/mindspore/docs.git
git@gitee.com:mindspore/docs.git
mindspore
docs
docs
r2.0.0-alpha

搜索帮助