Function Differences with tf.math.reduce_sum

View Source On Gitee

tf.math.reduce_sum

tf.math.reduce_sum(
    input_tensor, axis=None, keepdims=None, name=None, reduction_indices=None,
    keep_dims=None
)

For more information, see tf.math.reduce_sum.

mindspore.Tensor.sum

mindspore.Tensor.sum(self, axis=None, dtype=None, keepdims=False, initial=None)

For more information, see mindspore.Tensor.sum.

Usage

Both interfaces have the same basic function of computing the sum of Tensor in some dimension. The difference is that mindspore.Tensor.sum has one more parameter initial to set the starting value.

Code Example

import mindspore as ms

a = ms.Tensor([10, -5], ms.float32)
print(a.sum()) # 5.0
print(a.sum(initial=2)) # 7.0

import tensorflow as tf
tf.enable_eager_execution()

b = tf.constant([10, -5])
print(tf.math.reduce_sum(b).numpy()) # 5