赞
踩
tf.reduce_mean()用于计算tensor(张量)沿着指定的数轴(即tensor的某一维度)上的平均值,用作降维或者计算tensor的平均值。
tf.reduce_mean(input_tensor, axis=None, keepdims=False, name=None)
keep_dims为旧版写法
1.input_tensor:输入待降维的tensor
2.axis:指定的轴,默认为计算所有元素的均值
3.keepdims:是否降低维度,默认值为False。(当为True时,输出结果保持输入tensor的原状;当为False时,输出结果将会降低维度
)
4.name:操作的名称
import tensorflow as tf
X = np.array([[0, 1, 2], [3, 4, 5]])
Y = tf.cast(X, tf.float32)
mean_all = tf.reduce_mean(Y)
mean_0 = tf.reduce_mean(Y, axis=0)
mean_1 = tf.reduce_mean(Y, axis=1)
print(mean_all)
print(mean_0)
print(mean_1)
OUT:
参数keepdims设置为True时,保持原来tensor的维度
import tensorflow as tf
X = np.array([[0, 1, 2], [3, 4, 5]])
Y = tf.cast(X, tf.float32)
mean_all = tf.reduce_mean(Y, keepdims=True)
mean_0 = tf.reduce_mean(Y, axis=0, keepdims=True)
mean_1 = tf.reduce_mean(Y, axis=1, keepdims=True)
print(mean_all)
print(mean_0)
print(mean_1)
OUT:
希望这篇文章对大家的学习有所帮助!
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。