栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

tensorflow 2.0 基础知识点5 (数据统计——张量运算)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

tensorflow 2.0 基础知识点5 (数据统计——张量运算)

数据统计:求张量在某个维度上或者是全局上的统计值

函数描述
tf.reduce_sum(input,axis)求和
tf.reduce_mean(input,axis)求平均值
tf.reduce_max(input,axis)求最大值
tf.reduce_min(input,axis)求最小值
tf.argmax()求最大值索引
tf.argmin()求最小值索引

<1> 求和函数——tf.reduce_sum()

a = tf.constant([[1,2,3],[4,5,6]])
print(tf.reduce_sum(a,axis=1)) # 对列进行求和
print(tf.reduce_sum(a,axis=0)) # 对行进行求和
print(tf.reduce_sum(a)) # 对所有元素求和

结果
tf.Tensor([ 6 15], shape=(2,), dtype=int32)
tf.Tensor([5 7 9], shape=(3,), dtype=int32)
tf.Tensor(21, shape=(), dtype=int32)

<2>求均值函数——tf.reduce_mean()

a = tf.constant([[1,2,3],[4,5,6]])
print(tf.reduce_mean(a,axis=1)) # 对列求均值
print(tf.reduce_mean(a,axis=0)) # 对行求均值
print(tf.reduce_mean(a)) # 对所有元素求均值

结果:
tf.Tensor([2 5], shape=(2,), dtype=int32)
tf.Tensor([2 3 4], shape=(3,), dtype=int32)
tf.Tensor(3, shape=(), dtype=int32)

<3>求最大最小值——tf.reduce_max()

a = tf.constant([[1,2,3],[4,5,6]])
print(tf.reduce_max(a,axis=1)) # 对列求最大值
print(tf.reduce_max(a,axis=0)) # 对行求最大值
print(tf.reduce_max(a)) # 对所有元素求最大值

结果:
tf.Tensor([3 6], shape=(2,), dtype=int32)
tf.Tensor([4 5 6], shape=(3,), dtype=int32)
tf.Tensor(6, shape=(), dtype=int32)

<4>求最小值—— tf.reduce_min()

a = tf.constant([[1,2,3],[4,5,6]])
print(tf.reduce_max(a,axis=1)) # 对列求最小值
print(tf.reduce_max(a,axis=0)) # 对行求最小值
print(tf.reduce_max(a)) # 对所有元素求最小值

结果:
tf.Tensor([1 4], shape=(2,), dtype=int32)
tf.Tensor([1 2 3], shape=(3,), dtype=int32)
tf.Tensor(1, shape=(), dtype=int32)

<5>求最大值索引——tf.argmax()

a = tf.constant([[1,2,3],[4,5,6]])
print(tf.argmax(a,axis=1)) # 对列求最大值索引
print(tf.argmax(a,axis=0)) # 对行求最大值索引
print(tf.argmax(a)) # 对所有元素求最大值索引

结果:
tf.Tensor([2 2], shape=(2,), dtype=int64)
tf.Tensor([1 1 1], shape=(3,), dtype=int64)
tf.Tensor([1 1 1], shape=(3,), dtype=int64)

<6>求最小值索引——tf.argmin()

a = tf.constant([[1,2,3],[4,5,6]])
print(tf.argmin(a,axis=1)) # 对列求最小值索引
print(tf.argmin(a,axis=0)) # 对行求最小值索引
print(tf.argmin(a)) # 对所有元素求最小值索引

结果:
tf.Tensor([0 0], shape=(2,), dtype=int64)
tf.Tensor([0 0 0], shape=(3,), dtype=int64)
tf.Tensor([0 0 0], shape=(3,), dtype=int64)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/580913.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号