reduce_sum和reduce_mean函数
https://blog.youkuaiyun.com/Vipbinn/article/details/82978003
https://www.cnblogs.com/rossoneri/p/8029940.html
https://blog.youkuaiyun.com/chengshuhao1991/article/details/78545723
一句话总结
对于其中的常用参数,input_tensor就是输入的tensor,axis/reduction_indices二选一,代表reduce的维度,这个数越大,代表越深入,粒度越小地进行sum/mean等计算。axis只能传入一个数,例如是三维,只能是0,1,2三者其一,代表在这个方向上降维计算,而reduction_indices传入为[ ]可以是[0],[1],[0,1],[0,1,2]等等,可以在多个方向上降维,这就是二者的区别。
reduce_mean方法的源代码如下
def reduce_mean(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None): """Computes the mean of elements across dimensions of a tensor. Reduces `input_tensor` along the dimensions given in `axis`. Unless `keep_dims` is true, the rank of the tensor is reduced by 1 for each entry in `axis`. If `keep_dims` is true, the reduced dimensions are retained with length 1. If `axis` has no entries, all dimensions are reduced, and a tensor with a single element is returned. For example: ```python # 'x' is [[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.] ``` Args: input_tensor: The tensor to reduce. Should have numeric type. axis: The dimensions to reduce. If `None` (the default), reduces all dimensions. keep_dims: If true, retains reduced dimensions with length 1. name: A name for the operation (optional). reduction_indices: The old (deprecated) name for axis. Returns: The reduced tensor. @compatibility(numpy) Equivalent to np.mean @end_compatibility """ return gen_math_ops._mean( input_tensor, _ReductionDims(input_tensor, axis, reduction_indices), keep_dims, name=name)
说一下其中很重要的几个点吧:
Args:(参数列表) input_tensor: The tensor to reduce. Should have numeric type. axis: The dimensions to reduce. If None
(the default), reduces all dimensions. keep_dims: If true, retains reduced dimensions with length 1. name: A name for the operation (optional). reduction_indices: The old (deprecated) name for axis.
也就是说,tensorflow里边的reduce_indices参数和axis参数有一个就行了。。
tf的reduce_XXX这几个函数本身就被认为是一种降维,降维sum,降维mean等。。。好好理解其中的思想吧。
在计算损失时,通常会用到reduce_sum()函数来进行求和,但是在使用过程中常常会搞不清楚具体是怎样进行计算的,通过查阅资料,逐渐搞清楚了这个函数的用法,下面就来详细解释一下。
在TensorFlow官方文档中有这样的解释:
其实在reduce_sum()中,是从维度上去考虑的。其中的参数reduction_indices很容易搞蒙圈,上个图加深理解吧。
调用reduce_sum(arg1, arg2)时,参数arg1即为要求和的数据,arg2有两个取值分别为0和1,通常用reduction_indices=[0]或reduction_indices=[1]来给arg2传递参数。从上图可以看出,对于reduce_sum()而言
1)当arg2 = [0]时,是纵向对矩阵求和,原来矩阵有几列最后就得到几个值;
2)当arg2 = [1]时,是横向对矩阵求和,原来矩阵有几行最后就得到几个值;
3)当省略arg2参数时,默认对矩阵所有元素进行求和,最后得到一个值。
看到这里,函数名的前缀为什么是reduce_其实也就很容易理解了,reduce就是“对矩阵降维”的含义,下划线后面的部分就是降维的方式,在reduce_sum()中就是按照求和的方式对矩阵降维。那么其他reduce前缀的函数也举一反三了,比如reduce_mean()就是按照某个维度求平均值,等等。
对于reduce_mean而言
1)当arg2 = [0]时,是纵向对矩阵求平均,原来矩阵有几列最后就得到几个值;
2)当arg2 = [1]时,是横向对矩阵平均,原来矩阵有几行最后就得到几个值;
3)当省略arg2参数时,默认对矩阵所有元素进行求和(零维),最后得到一个值。
总结规律: 对于k维的, tf.reduce_xyz(x, axis=k-1)的结果是对最里面一维所有元素进行求和。 tf.reduce_xyz(x, axis=k-2)是对倒数第二层里的向量对应的元素进行求和。 tf.reduce_xyz(x, axis=k-3)把倒数第三层的每个向量对应元素相加。
http://wossoneri.github.io/2017/11/15/%5BTensorflow%5DThe-dimension-of-Tensor/