tf.reduce_sum()_tf.reduce_mean()_tf.reduce_max()
根据官方文档:
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. IfNone
(the default), reduces all dimensions. Must be in the range (rank(input_tensor), rank(input_tensor))
.#取0第一维,取1第二维,取-1最后一维keepdims
: If true, retains reduced dimensions with length 1.#按照原来的维度name
: A name for the operation (optional).reduction_indices
: The old (deprecated) name for axis.#axis的原来的名字keep_dims
: Deprecated alias forkeepdims
.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]
求最大值tf.reduce_max(input_tensor, reduction_indices=None, keep_dims=False, name=None)
求平均值tf.reduce_mean(input_tensor, reduction_indices=None, keep_dims=False, name=None)
参数1--input_tensor:待求值的tensor。
参数2--reduction_indices:在哪一维上求解。
参数(3)(4)可忽略
import tensorflow as tf import numpy as np x = tf.constant([[1,2],[3,4]]) with tf.Session() as sess: print(sess.run(tf.reduce_mean(x))) #所有求平均 print(sess.run(tf.reduce_mean(x, 0))) #按 列 求和 print(sess.run( tf.reduce_mean(x, 1)))#按行求平均 print(sess.run(tf.reduce_max(x))) print(sess.run(tf.reduce_max(x, 0))) print(sess.run(tf.reduce_max(x, 1)))
###############输出##########
2 [2 3] [1 3] 4 [3 4] [2 4]