TensorFlow. tf.reduce_mean

tf.reduce_mean

tf.reduce_mean(input_tensor,reduction_indices=None,keep_dims=False,name=None)
Computes the mean of elements across dimensions of a tensor.
按某一维度(reduction_indices)计算一个张量的个元素的平均值。

Reduces input_tensor along the dimensions given in reduction_indices. Unless keep_dims is true, the rank of the tensor is reduced by 1 for each entry in reduction_indices.
If keep_dims is true, the reduced dimensions are retained with length 1.
沿着reduction_indices给出的维度方向降维。如果keep_dims等于true,则按reduction_indices给出的降维的尺寸保留为1维,如果是False,则降维到1维。

If reduction_indices has no entries, all dimensions are reduced, and a tensor with a single element is returned.
如果reduction_indices没有指定,则所有维度都要降维,返回一个含有单个元素的张量。
举例:
import tensorflow as tf
sess = tf.InteractiveSession()
x = tf.constant(value=[[1.,2.,3.],[4.,5.,6.],[7.,8.,9.]],dtype=tf.float32,shape=[3,3],name=”x”)
print sess.run(x)
print x

mean1 = tf.reduce_mean(input_tensor=x, reduction_indices=None, keep_dims=False)
mean1 = tf.reduce_mean(input_tensor=x, reduction_indices=None, keep_dims=False)
mean2 = tf.reduce_mean(input_tensor=x, reduction_indices=0, keep_dims=False)
mean3 = tf.reduce_mean(input_tensor=x, reduction_indices=1, keep_dims=False)
mean4 = tf.reduce_mean(input_tensor=x, reduction_indices=0, keep_dims=True)
mean5 = tf.reduce_mean(input_tensor=x, reduction_indices=1, keep_dims=True)
mean6 = tf.reduce_mean(input_tensor=x, reduction_indices=None, keep_dims=True)

print sess.run(mean1)
print mean1
print sess.run(mean2)
print mean2
print sess.run(mean3)
print mean3
print sess.run(mean4)
print mean4
print sess.run(mean5)
print mean5
print sess.run(mean6)
print mean6

输出:
[[ 1. 2. 3.]
[ 4. 5. 6.]
[ 7. 8. 9.]]
Tensor(“x:0”, shape=(3, 3), dtype=float32)
5.0
Tensor(“Mean_2:0”, shape=(), dtype=float32)
[ 4. 5. 6.]
Tensor(“Mean_3:0”, shape=(3,), dtype=float32)
[ 2. 5. 8.]
Tensor(“Mean_4:0”, shape=(3,), dtype=float32)
[[ 4. 5. 6.]]
Tensor(“Mean_5:0”, shape=(1, 3), dtype=float32)
[[ 2.]
[ 5.]
[ 8.]]
Tensor(“Mean_6:0”, shape=(3, 1), dtype=float32)
[[ 5.]]
Tensor(“Mean_1:0”, shape=(1, 1), dtype=float32)

Args:
input_tensor: The tensor to reduce. Should have numeric type.
reduction_indices: The dimensions to reduce. If None (the default), reduces all dimensions.
keep_dims: If true, retains reduced dimensions with length 1.
name: A name for the operation (optional).
Returns:
The reduced tensor.

参数说明:
Input_tensor:被降维的张量必须其数据类型必须被预先指定。
reduction_indices:降维的维度如果为None(default),则所有维度都要降维。
keep_dims:如果keep_dims为true,则降维的尺寸将保留为1
name:降维操作的名字。
返回一个降维后的张量。

如发现文章中有错误,请不吝指教

猜你喜欢

转载自blog.csdn.net/shaozhulei555/article/details/71450903