tf.reduce_mean函数

tf.reduce_mean()函数用于计算张量tensor沿着指定的数轴(tensor中的某一维度)上的平均值,主要用于降维或者计算tensor(图像)的平均值。

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

input_tensor:输入的tensor

axis:指定的轴,axis=0沿列进行求均值,axis=1沿行进行求均值,若不指定,则计算所有元素的均值

keep_dims:是否将维度,默认为False,输出结果会降低维度;若设置为True,输出的结果保持输入tensor的形状

name:操作的名称

import tensorflow as tf
x = [[1,2,3],[1,1,3]]
xx = tf.cast(x,tf.float32)  
#tf.cast(input_data,dtype,name=None)执行的是张量的数据类型转换
mean_all = tf.reduce_mean(xx)
mean_0 = tf.reduce_mean(xx,axis=0)  #axis=0计算列方向
mean_1 = tf.reduce_mean(xx,axis=1)  #axis=1计算行方向
with tf.Session() as sess:
    m_a,m_0,m_1=sess.run([mean_all,mean_0,mean_1])

 

print(m_a)
print(m_0)
print(m_1)

1.83333
[ 1.   1.5  3. ]
[ 2.          1.66666663]

类似的函数还有:

*tf.reduce_sum:计算tensor指定轴方向上的元素的累加和

*tf.reduce_max:计算tensor指定轴方向上的元素的最大值

*tf.reduce_all:计算********元素的逻辑和(and运算)

*tf.reduce_any:计算********元素的逻辑或(or运算)

你可能感兴趣的:(python)