1. 程式人生 > >tensorflow 中 reduce_sum 理解

tensorflow 中 reduce_sum 理解

post flow const body 理解 ant pan ims tensor

定義如下:

reduce_sum(
    input_tensor,
    axis=None,
    keep_dims=False,
    name=None,
    reduction_indices=None
)

reduce_sum 是 tensor 內部求和的工具。其參數中:

1. input_tensor 是要求和的 tensor

2. axis 是要求和的 rank,如果為 none,則表示所有 rank 都要仇和

3. keep_dims 求和後是否要降維

4. 這個操作的名稱,可能在 graph 中 用

5. 已被淘汰的,被參數 axis 替代

示例如下:

x = tf.constant([[1, 1, 1], [1, 1, 1]])
tf.reduce_sum(x, 0)  # 對 tensor 的 0 級進行求和,[1,1,1] + [1,1,1] =  [2, 2, 2]
tf.reduce_sum(x, 1)  # 對 tensor 的 1 級進行仇和,[1+1+1, 1+1+1] = [3, 3]
tf.reduce_sum(x, 1, keep_dims=True)  # 對第 1 級進行求和,但不降維, [[3], [3]]
tf.reduce_sum(x, [0, 1])  # 0 級和 1級都要求和,6
tf.reduce_sum(x) # 因為 x 只有 2 級,所以結果同上一個,6

tensorflow 中 reduce_sum 理解