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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值