np.mean和tf.reduce_mean之间有什么区别?

12 浏览
0 Comments

np.mean和tf.reduce_mean之间有什么区别?

MNIST入门教程中,有一条语句accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))tf.cast基本上是改变张量对象的类型,但是np.meantf.reduce_mean之间有什么区别呢?

这是tf.reduce_mean的文档:

reduce_mean(input_tensor, reduction_indices=None, keep_dims=False, name=None)

input_tensor:需要减少的张量。应该是数值类型。

reduction_indices:需要减少的维度。如果为None(默认值),则减少所有维度。

# 'x' 是 [[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.]

对于一个一维向量,看起来np.mean == tf.reduce_mean,但是我不理解tf.reduce_mean(x, 1) ==> [1., 2.]中发生了什么。tf.reduce_mean(x, 0) ==> [1.5, 1.5]有点有道理,因为[1, 2][1, 2]的均值是[1.5, 1.5],但是tf.reduce_mean(x, 1)中发生了什么?

0