import tensorflow as tf
x = tf.constant([[1., 1.],
[2., 2.]])
tf.reduce_mean(x)
# 第一个参数是一个集合,可以是列表、二维数组和多维数组。
#第二个参数指定在哪个维度上面求平均值。默认对所有的元素求平均
m1 = tf.reduce_mean(x, axis=0) # [1.5, 1.5]
m2 = tf.reduce_mean(x, 1) # [1., 2.]
xx = tf.constant([[[1., 1, 1],
[2., 2, 2]],
[[3, 3, 3],
[4, 4, 4]]])
m3 = tf.reduce_mean(xx, [0, 1]) # [2.5 2.5 2.5]
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(m1))
print(sess.run(m2))
print(xx.get_shape())
print(sess.run(m3))
[1.5 1.5]
[1. 2.]
(2, 2, 3)
[2.5 2.5 2.5]