Function Differences with tf.math.add

View Source On Gitee

tf.math.add

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

For more information, see tf.math.add.

mindspore.ops.add

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

For more information, see mindspore.ops.add.

Differences

TensorFlow: Computes the element sum of input x and input y, and return a Tensor with the same type as x.

MindSpore: MindSpore API implements the same function as TensorFlow, and only the parameter names are different.

Categories

Subcategories

TensorFlow

MindSpore

Differences

Parameters

Parameter 1

x

x

-

Parameter 2

y

y

-

Parameter 3

name

-

Not involved

Code Example 1

MindSpore and TensorFlow output the same result when both x and y inputs are Tensor and the data types are the same.

# 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.]]

Code Example 2

TensorFlow supports scalar summation and the x and y data types must be the same. MindSpore version 1.8.1 does not support scalar summation at this time, but the x and y data types can be different. In order to get the same result, the scalar is converted to Tensor for calculation.

# 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.]]

cOde Example 3

The name parameter of TensorFlow is used to define the name of the operation and has no effect on the calculation result.

# 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.]]