tf.reduce_max 与 reduce 系列 API

reduce 可以理解为 python 里的 reduce 函数;

tensorflow 中有很多 reduce_ API,其用法完全相同

tf.reduce_max

以这个为例进行说明

def reduce_max(input_tensor,
                  axis=None,
                  keepdims=None,
                  name=None,
                  reduction_indices=None,
                  keep_dims=None):
  """Computes the maximum of elements across dimensions of a tensor.

求指定维度上的最大值

input_tensor:输入数据,tensor、array、dataframe 都可以

axis:维度,0 表示 列,1 表示行,-1 表示最后一个维度,可以是标量和列表

keepdims:bool 值,按原维度输出

reduction_indices:等价于 axis,已废弃

示例

data = tf.constant([[1, 2],
                    [3, 4],
                    [2, 5]])
print(data.shape)       # (3, 2)
out1 = tf.reduce_max(data, axis=0)      ### 第 0 个维度即 每列最大值
out2 = tf.reduce_max(data, axis=1)      ### 第 1 个维度即 每行最大值
out3 = tf.reduce_max(data, axis=0, keepdims=True)       ### 保持原维度
out4 = tf.reduce_max(data, reduction_indices=[1])   ### 等价于 axis=1
out5 = tf.reduce_max(data, axis=[0, 1]) ### 多个维度最大值

sess = tf.Session()
print(sess.run(out1))       # [3 5]
print(sess.run(out2))       # [2 4 5]
print(sess.run(out3))       # [[3 5]]
print(sess.run(out4))       # [2 4 5]
print(sess.run(out5))       # 5

reduce_

tf.reduce_min:最小

tf.reduce_sum:求和

tf.reduce_mean:均值

tf.reduce_all:计算张量在维度上的逻辑和

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

参考资料:

猜你喜欢

转载自www.cnblogs.com/yanshw/p/12377021.html