1. 程式人生 > >TensorFlow學習(三):tf.reduce_sum()

TensorFlow學習(三):tf.reduce_sum()

壓縮求和

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

Args:

  • input_tensor: The tensor to reduce. Should have numeric type. #輸入
  • axis: The dimensions to reduce. If None (the default), reduces all dimensions. Must be in the range [-rank(input_tensor), rank(input_tensor))
    .
  • keepdims: If true, retains reduced dimensions with length 1.
  • name: A name for the operation (optional).
  • reduction_indices: The old (deprecated) name for axis.
  • keep_dims: Deprecated alias for keepdims.

Returns:

The reduced tensor, of the same dtype as the input_tensor.

例子:

import tensorflow as tf
import numpy as np

x = tf.constant([[1,1,1],[2,2,2]])
with tf.Session() as sess:
    print(sess.run(tf.reduce_sum(x))) #所有求和
    print(sess.run(tf.reduce_sum(x,0))) #按 列 求和
    print(sess.run(tf.reduce_sum(x,1))) #按 行 求和
    print(sess.run(tf.reduce_sum(x,1,keepdims=True))) #按維度 行 求和
    print(sess.run(tf.reduce_sum(x,[0,1])))  #行列求和
    print(sess.run(tf.reduce_sum(x,reduction_indices=[1])))
    

輸出結果:

9
[3 3 3]
[3 6]
[[3]
 [6]]
9
[3 6]

 

Reference:

tf.reduce_sum()