TensorFlow 里有多个 reduce_ 前缀系函数,即:

1. tf.reduce_all - 逻辑和

求张量的布尔元素逻辑和. 等价于 np.all.

tf.math.reduce_all(
    input_tensor, # 输入张量
    axis=None,    # 待操作的维度,默认None,对所有维度操作
    keepdims=None,
    name=None,
    reduction_indices=None,
    keep_dims=None # keep_dims 会替换为 keepdims
)

例示:

x = tf.constant([[True,  True], 
                 [False, False]])
tf.reduce_all(x)  # False
tf.reduce_all(x, 0)  # [False, False]
tf.reduce_all(x, 1)  # [True, False]

2. tf.reduce_any - 逻辑或

求张量的布尔元素逻辑和. 等价于 np.any.

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

例示:

x = tf.constant([[True,  True], 
                 [False, False]])
tf.reduce_any(x)  # True
tf.reduce_any(x, 0)  # [True, True]
tf.reduce_any(x, 1)  # [True, False]

3. tf.reduce_logsumexp

计算log(sum(exp(张量的各维度元素))).

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

例示:

x = tf.constant([[0., 0., 0.], 
                 [0., 0., 0.]])
tf.reduce_logsumexp(x)     # log(6)
tf.reduce_logsumexp(x, 0)  # [log(2), log(2), log(2)]
tf.reduce_logsumexp(x, 1)  # [log(3), log(3)]
tf.reduce_logsumexp(x, 1, keepdims=True)  # [[log(3)], 
                                          #  [log(3)]]
tf.reduce_logsumexp(x, [0, 1])  # log(6)

4. tf.reduce_max - 最大值

计算张量的各维度上元素的最大值. 等价于 np.max.

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

例示:

x = tf.constant([[1, 2, 3], 
                 [4, 5, 6]])
tf.reduce_max(x)  # 6
tf.reduce_max(x, 0)  # [4, 5, 6]
tf.reduce_max(x, 1)  # [3, 6]
tf.reduce_max(x, 1, keepdims=True)  # [[3],
                                    #  [6]]
tf.reduce_max(x, [0, 1])  # 6

5. tf.reduce_mean - 均值

计算张量的各维度上元素的均值. 等价于 np.mean.

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

例示:

x = tf.constant([[1., 1.], 
                 [2., 2.]])
tf.reduce_mean(x)     # 1.5
tf.reduce_mean(x, 0)  # [1.5, 1.5]
tf.reduce_mean(x, 1)  # [1.,  2.]

注:np.mean 可以利用 dtype参数指定输出的数据类型,默认为 dtype=float64.

tf.reduce_mean() 则是根据 input_tensor 推断输出的数据类型,如:

x = tf.constant([1, 0, 1, 0])
tf.reduce_mean(x)  # 0
y = tf.constant([1., 0., 1., 0.])
tf.reduce_mean(y)  # 0.5

6. tf.reduce_min - 最小值

计算张量的各维度上元素的最小值. 等价于 np.min.

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

例示:

x = tf.constant([[1, 2, 3], 
                 [4, 5, 6]])
tf.reduce_min(x)  # 1
tf.reduce_min(x, 0)  # [1, 2, 3]
tf.reduce_min(x, 1)  # [1, 4]
tf.reduce_min(x, 1, keepdims=True)  # [[1],
                                    #  [4]]
tf.reduce_min(x, [0, 1])  # 1

9. tf.reduce_prob - 元素乘积

计算张量的各维度上元素的乘积值. 等价于 np.prob.

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

例示:

x = tf.constant([[1, 2, 3], 
                 [4, 5, 6]])
tf.reduce_prod(x)  # 720
tf.reduce_prod(x, 0)  # [4, 10, 18]
tf.reduce_prod(x, 1)  # [6, 120]
tf.reduce_prod(x, 1, keepdims=True)  # [[  6],
                                     #  [120]]
tf.reduce_prod(x, [0, 1])  # 720

10. tf.reduce_sum - 元素和

计算张量的各维度上元素的求和值. 等价于 np.sum.

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

例示:

x = tf.constant([[1, 1, 1], 
                 [1, 1, 1]])
tf.reduce_sum(x)  # 6
tf.reduce_sum(x, 0)  # [2, 2, 2]
tf.reduce_sum(x, 1)  # [3, 3]
tf.reduce_sum(x, 1, keepdims=True)  # [[3], 
                                    #  [3]]
tf.reduce_sum(x, [0, 1])  # 6
Last modification:November 23rd, 2018 at 02:51 pm