【CNN记录】tensorflow中reduceMax、reduceMin、reduceSum、reduceMean介绍

reduceSum 

tf.reduce_sum(
    input_tensor, axis=None, keepdims=False, name=None
)

说明:在指定维度上求多维tensor元素之和,并且按照axis消除该维度,除非keepdims为True

input_tensor:输入张量

axis:需要降维的维度

keepdims:是否保留维度

name:操作数的名字(可选)

举个栗子

>a = np.random.random_integers(3,size=(2,3,4))
>input = tf.constant(a)
tf.Tensor(
[[[2 3 1 1]
 [1 1 2 2]
 [1 2 2 1]]
 [[1 2 1 2]
 [2 2 3 3]
 [3 1 3 3]]], shape=(2, 3, 4), dtype=int32)
>output = tf.reduce_sum(input);
tf.Tensor(45, shape=(), dtype=int32)
#axis为None的时候,所有维度都会被降维(展成一维加起来),所有元素加起来为45
>output = tf.reduce_sum(input,axis=0)
tf.Tensor(
[[3 5 2 3]
 [3 3 5 5]
 [4 3 5 4]], shape=(3, 4), dtype=int32)
#axis=0时,最外的维度相加,这里就是2+1、3+2、1+1、1+2 ...,最终得到一个(3,4)的张量
如果keepdims=True
>output = tf.reduce_sum(input,axis=0,keepdims=True)
tf.Tensor(
[[[3 5 2 3]
 [3 3 5 5]
 [4 3 5 4]]], shape=(1, 3, 4), dtype=int32)
#保留了axis指定的维度,(2,3,4)变成(1,3,4)
>output = tf.reduce_sum(input,axis=1);
tf.Tensor(
[[4 6 5 4]
 [6 5 7 8]], shape=(2, 4), dtype=int32)
#axis=1时,dim1维度相加,这里就是2+1+1、3+1+2、1+2+2、1+2+1 ...,最终得到一个(2,4)的张量

reduceMax

tf.reduce_max(
    input_tensor, axis=None, keepdims=False, name=None
)

参数说明参考reducesum 

继续举个栗子

>a = np.random.random_integers(3,size=(2,3,4)) #创建一个固定dim的numpy
>input = tf.constant(a)
tf.Tensor(
[[[2 3 1 2]
 [3 2 2 1]
 [2 1 1 1]]
 [[2 3 3 2]
 [3 1 3 1]
 [1 1 2 1]]], shape=(2, 3, 4), dtype=int32)
>output = tf.reduce_max(input)
tf.Tensor(3, shape=(), dtype=int32)
#axis为None时取所有元素最大值
>output = tf.reduce_max(input,axis=0)
tf.Tensor(
[[2 3 3 2]
 [3 2 3 1]
 [2 1 2 1]], shape=(3, 4), dtype=int32)
#axis=0的时候,按照dim0取最大值,即:max(2,2)、max(3,3)、max(1,3)、max(2,2)。。。
#最终得到一个(3,4)的tensor

reduceMin

tf.reduce_min(
    input_tensor, axis=None, keepdims=False, name=None
)

说明:同reduceMax,只是取最小值

 reduceMean

说明:指定axis代表的维度求均值,并且按照axis消除该维度,除非keepdims为True

>input = tf.constant([[1., 1.], [2., 2.]])
>output = tf.reduce_mean(input)
tf.Tensor(1.5, shape=(), dtype=float32)
#所有元素求均值
>output = tf.reduce_mean(input, 0)
tf.Tensor([1.5 1.5], shape=(2,), dtype=float32)
#dim0维度求均值,即计算[1,2]、[1,2]均值为1.5、1.5
>output = tf.reduce_mean(input, 1)
tf.Tensor([1. 2.], shape=(2,), dtype=float32)
#dim0维度求均值,即计算[1,1]、[2,2]均值为1.、2.

其实这些都差不多,包括argmin、argmax

作者:侵蚀昨天原文地址:https://blog.csdn.net/q2519008/article/details/127964600

%s 个评论

要回复文章请先登录注册