tensorflow函数之tf.reduce_mean

tf.reduce_mean()

tf.reduce_mean(
    input_tensor,
    axis=None,
    keepdims=None,
    name=None,
    reduction_indices=None,
    keep_dims=None
)

tf.reduce_mean函数的作用是求平均值。第一个参数是一个集合,可以是列表、二维数组和多维数组。第二个参数指定在哪个维度上面求平均值。默认对所有的元素求平均。

示例

import tensorflow as tf

x = tf.constant([[1., 1.],
                 [2., 2.]])
tf.reduce_mean(x)  # 1.5
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]

参考:https://blog.csdn.net/liangyihuai/article/details/79050018

猜你喜欢

转载自blog.csdn.net/weixin_41278720/article/details/80271452