代码拉取完成,页面将自动刷新
tf.math.add(x, y, name=None) -> Tensor
更多内容详见tf.math.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 | - | 不涉及 |
当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.]]
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.]]
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.]]
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。