TensorFlow学习(三):tf.reduce_sum()

2021-07-07 18:06:24 浏览数 (3)

压缩求和

代码语言:javascript复制
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.

例子:

代码语言:javascript复制
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()

0 人点赞