简介
TF中常常使用tf.reduce_mean()与tf.reduce_sum()分别对指定张量的指定维度求平均值,求和。
# 计算张量沿着指定维度的平均值
tf.reduce_mean (张量名,axis=操作轴)
# 计算张量沿着指定维度的和
tf.reduce_sum (张量名,axis=操作轴)
代码
import tensorflow as tf
x = tf.constant([[1, 2, 3], [2, 2, 3]])
print("x: ", x)
print("mean of x: ", tf.reduce_mean(x))
print("mean of x: ", tf.reduce_mean(x, axis = 0))
print("mean of x: ", tf.reduce_mean(x, axis = 1))
print("mean of x: ", tf.reduce_sum(x))
print("sum of x: ", tf.reduce_sum(x, axis = 0))
print("sum of x: ", tf.reduce_sum(x, axis = 1))
axis指定哪个,就把那个维度变成1行/列/…