当前位置:网站首页>tf.reduce_mean()与tf.reduce_sum()

tf.reduce_mean()与tf.reduce_sum()

2022-08-11 06:45:00 phac123

简介

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行/列/…

原网站

版权声明
本文为[phac123]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_42596275/article/details/126258946