tf.reduce_mean(input_tensor, reduction_indices=None, keep_dims=False, name=None)
Computes the mean of elements across dimensions of a tensor.
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.
If reduction_indices has no entries, all dimensions are reduced, and a tensor with a single element is returned.
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.
For example:
# 'x' is [[1., 1.]
# [2., 2.]]
tf.reduce_mean(x) ==> 1.5
tf.reduce_mean(x, 0) ==> [1.5, 1.5]
tf.reduce_mean(x, 1) ==> [1., 2.]
numpy.mean(a, axis=None, dtype=None, out=None, keepdims=False)[source]
Compute the arithmetic mean along the specified axis.
Returns the average of the array elements. The average is taken over the flattened array by default, otherwise over the specified axis. float64 intermediate and return values are used for integer inputs.
Parameters:
a : array_like
Array containing numbers whose mean is desired. If a is not an array, a conversion is attempted.
axis : None or int or tuple of ints, optional
Axis or axes along which the means are computed. The default is to compute the mean of the flattened array.
If this is a tuple of ints, a mean is performed over multiple axes, instead of a single axis or all the axes as before.
dtype : data-type, optional
Type to use in computing the mean. For integer inputs, the default is float64; for floating point inputs, it is the same as the input dtype.
out : ndarray, optional
Alternate output array in which to place the result. The default is None; if provided, it must have the same shape as the expected output, but the type will be cast if necessary. See doc.ufuncs for details.
keepdims : bool, optional
If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original arr.
Returns:
m : ndarray, see dtype parameter above
If out=None, returns a new array containing the mean values, otherwise a reference to the output array is returned.
The arithmetic mean is the sum of the elements along the axis divided by the number of elements.
Note that for floating-point input, the mean is computed using the same precision the input has. Depending on the input data, this can cause the results to be inaccurate, especially for float32 (see example below). Specifying a higher-precision accumulator using the dtype keyword can alleviate this issue.
Examples:
a = np.array([[1, 2], [3, 4]])
np.mean(a)
>>> 2.5
np.mean(a, axis=0)
array([ 2., 3.])
np.mean(a, axis=1)
array([ 1.5, 3.5])
In single precision, mean can be inaccurate:
a = np.zeros((2, 512*512), dtype=np.float32)
a[0, :] = 1.0
a[1, :] = 0.1
np.mean(a)
>>> 0.546875
Computing the mean in float64 is more accurate:
np.mean(a, dtype=np.float64)
>>> 0.55000000074505806
import numpy as np
import tensorflow as tf
c = np.array([[3.,4], [5.,6], [6.,7]])
# np.mean
print "#Result of np.mean:"
print(np.mean(c,keepdims=True))
print(np.mean(c,keepdims=False))
print(np.mean(c,0,keepdims=True))
print(np.mean(c,0,keepdims=False))
print(np.mean(c,1,keepdims=True))
print(np.mean(c,1,keepdims=False))
# tf.reduce_mean
Mean1 = tf.reduce_mean(c,keep_dims=True)
Mean2 = tf.reduce_mean(c,keep_dims=False)
Mean3 = tf.reduce_mean(c,0,keep_dims=True)
Mean4 = tf.reduce_mean(c,0,keep_dims=False)
Mean5 = tf.reduce_mean(c,1,keep_dims=True)
Mean6 = tf.reduce_mean(c,1,keep_dims=False)
with tf.Session() as sess:
result1 = sess.run(Mean1)
result2 = sess.run(Mean2)
result3 = sess.run(Mean3)
result4 = sess.run(Mean4)
result5 = sess.run(Mean5)
result6 = sess.run(Mean6)
print "#Result of tf_reduce_mean:"
print result1
print result2
print result3
print result4
print result5
print result6
输出结果:
#Result of np.mean:
[[ 5.16666667]]
5.16666666667
[[ 4.66666667 5.66666667]]
[ 4.66666667 5.66666667]
[[ 3.5]
[ 5.5]
[ 6.5]]
[ 3.5 5.5 6.5]
#Result of tf_reduce_mean:
[[ 5.16666667]]
5.16666666667
[[ 4.66666667 5.66666667]]
[ 4.66666667 5.66666667]
[[ 3.5]
[ 5.5]
[ 6.5]]
[ 3.5 5.5 6.5]