tensorflow reduce系列函数(tf.reduce_mean, tf.reduce_sum, tf.reduce_prod, tf.reduce_max, tf.reduce_min)

简而言之,reduce系列的函数都可在张量指定的维度上操作

目录

输入参数

tf.reduce_all   在boolean张量的维度上计算元素的 "逻辑和"

tf.reduce_any 在boolean张量的维度上计算元素的 "逻辑或"

tf.reduce_max 计算张量的各个维度上元素的最大值

tf.reduce_min  计算张量的各个维度上元素的最小值

tf.reduce_mean 计算张量的各个维度上的元素的平均值

tf.reduce_sum 计算张量的各个维度上元素的总和 

tf.reduce_prod 计算张量的各个维度上元素的乘积

tf.reduce_logsumexp 计算log(sum(exp(张量的各维数的元素))) 

tf.reduce_join 在给定的维度上加入一个字符串张量

 


输入参数

tf.reduce_all/reduce_any/reduce_max/reduce_min/reduce_mean/reduce_sum/reduce_prod/reduce_logsumexp(
    input_tensor,
    axis=None,
    keepdims=None,
    name=None,
    reduction_indices=None,
    keep_dims=None
)
  • input_tensor:输入的张量。
  • axis:要操作的维度,如果为None(默认),则操作所有维度。必须在范围[-rank(input_tensor), rank(input_tensor))内。
  • keepdims:如果为 true,则保留长度为1的缩小维度。
  • name:操作的名称(可选)。
  • reduction_indices:参数axis的已弃用的别名名称。(为了兼容性)
  • keep_dims:参数keepdims的已弃用的别名名称。(为了兼容性)

tf.reduce_all   在boolean张量的维度上计算元素的 "逻辑和"

按照axis给定的维度减少input_tensor (boolean Tensor) 。除非keepdims为 true,否则张量的秩将在轴的每个条目中减少1。如果keep_dims为 true,则减小的维度将保留为长度1。

如果axis=None,则会减少所有维度,并返回具有单个元素的张量。

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]

tf.reduce_any 在boolean张量的维度上计算元素的 "逻辑或"

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]

tf.reduce_max 计算张量的各个维度上元素的最大值

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

tf.reduce_min  计算张量的各个维度上元素的最小值

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

tf.reduce_mean 计算张量的各个维度上的元素的平均值

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

要注意数据类型的兼容性

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

tf.reduce_sum 计算张量的各个维度上元素的总和 

x = tf.constant([[1, 2, 3], [4, 5, 6]])
tf.reduce_sum(x)  # 21
tf.reduce_sum(x, 0)  # [5, 7, 9]
tf.reduce_sum(x, 1)  # [6, 15]
tf.reduce_sum(x, 1, keepdims=True)  # [[ 6], 
                                    #  [15]]
tf.reduce_sum(x, [0, 1])  # 21

tf.reduce_prod 计算张量的各个维度上元素的乘积

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

tf.reduce_logsumexp 计算log(sum(exp(张量的各维数的元素))) 

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)

这个函数在数值上比 log(sum(exp(input))) 更稳定。它避免了大量输入的 exp 引起的溢出和小输入日志带来的下溢。

tf.reduce_join 在给定的维度上加入一个字符串张量

tf.reduce_join(
    inputs,
    axis=None,
    keep_dims=False,
    separator='',
    name=None,
    reduction_indices=None
)
# tensor `a` is [["a", "b"], ["c", "d"]]
tf.reduce_join(a, 0) # ==> ["ac", "bd"]
tf.reduce_join(a, 1) # ==> ["ab", "cd"]
tf.reduce_join(a, -2) # = tf.reduce_join(a, 0)  ==> ["ac", "bd"]
tf.reduce_join(a, -1) # = tf.reduce_join(a, 1)  ==> ["ab", "cd"]
tf.reduce_join(a, 0, keep_dims=True) # ==> [["ac", "bd"]]
tf.reduce_join(a, 1, keep_dims=True) # ==> [["ab"], ["cd"]]
tf.reduce_join(a, 0, separator=".") # ==> ["a.c", "b.d"]
tf.reduce_join(a, [0, 1]) # ==> "acbd"
tf.reduce_join(a, [1, 0]) # ==> "abcd"
tf.reduce_join(a, []) # ==> [["a", "b"], ["c", "d"]]
tf.reduce_join(a) # = tf.reduce_join(a, [1, 0]) ==> "abcd"

在具有给定形状的 [d_0, d_1,...,d_{n-1}]字符串张量中计算跨维度的字符串连接。返回一个新的Tensor,它由输入字符串与给定的分隔符separator(默认:空字符串)连接创建的。axis为负则从末端向后数,-1相当于n - 1。

  • separator:可选的string。默认为""。加入时要使用的分隔符。

 

  • 6
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值