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

tf.math.reduce_sum

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

更多内容详见tf.math.reduce_sum

mindspore.Tensor.sum

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

更多内容详见mindspore.Tensor.sum

使用方式

两接口基本功能相同,都是计算某个维度上Tensor的和。不同点在于,mindspore.Tensor.sum多一个入参initial用于设置起始值。

代码示例

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