今天在查tf.reduce_sum()的用法的时候,看到的一个解释。
具体的可以看这个问题底下杨镒铭的回答:https://www.zhihu.com/question/51325408?from=profile_question_card
tf.reduce_sum(tensor,dimension),其中第二个参数是维度。
这个函数表面上看是进行加和操作,实际上在做的事情是在降维。比如这个例子:
>>> a = tf.constant([[[1,2,3,4],[2,3,4,5],[3,4,5,6]],[[4,5,6,7],[5,6,7,8],[6,7,8,9]]])
>>> a
<tf.Tensor 'Const_1:0' shape=(2, 3, 4) dtype=int32>
>>> b = tf.reduce_sum(a,0)
>>> b
<tf.Tensor 'Sum_2:0' shape=(3, 4) dtype=int32>
>>> c = tf.reduce_sum(a,1)
>>> c
<tf.Tensor 'Sum_3:0' shape=(2, 4) dtype=int32>
a是一个2*3*4维度的矩阵
reduce_sum(a,0)就是将第零个维度抹去,经过该函数之后,维度变为3*4
reduce_sum(a,1)就是将第一个维度抹去,变为2*4