tensorflow的axis——降维tf.argmax() tf.reduce_mean()

目录

  • tf.argmax()
  • tf.reduce_mean()

tf.argmax(), 自带降低一维

import tensorflow as tf
import numpy as np

x = np.arange(24).reshape((2,3,4,1))
z = tf.argmax(x,axis = 3)

print(z.get_shape())

with tf.Session() as sess:
    print(sess.run(z))

(2, 3, 4)
[[[0 0 0 0]
  [0 0 0 0]
  [0 0 0 0]]

 [[0 0 0 0]
  [0 0 0 0]
  [0 0 0 0]]]

z = tf.argmax(x,axis = 2)

(2, 3, 1)
[[[3]
  [3]
  [3]]

 [[3]
  [3]
  [3]]]

z = tf.argmax(x,axis = 1)

(2, 4, 1)
[[[2]
  [2]
  [2]
  [2]]

 [[2]
  [2]
  [2]
  [2]]]

z = tf.argmax(x,axis = 0)

(3, 4, 1)
[[[1]
  [1]
  [1]
  [1]]

 [[1]
  [1]
  [1]
  [1]]

 [[1]
  [1]
  [1]
  [1]]]


tf.reduce_mean()

import tensorflow as tf
import numpy as np

a = np.arange(8.0).reshape(2,2,2)
b = tf.reduce_mean(a,axis=[1,2],keepdims=True)
print(b.get_shape())

with tf.Session() as sess:
    b=sess.run(b)
print(a)
print(b)

(2, 1, 1)


[[[0. 1.]

  [2. 3.]]

 [[4. 5.]
  [6. 7.]]]


[[[1.5]]

 [[5.5]]]

axis=0垂直屏幕。axis=1纵向,axis=2横向(计算机的传统)。axis=[1,2]就是垂直屏幕的平面各自去平均值了,可以分解为axis=1取平均,然后axis=2取平均。

import tensorflow as tf
import numpy as np

a = np.arange(8.0).reshape(2,2,2)
b = tf.reduce_mean(a,axis= 1,keepdims=True)
print(b.get_shape())

with tf.Session() as sess:
    b=sess.run(b)
print(a)
print(b)

(2, 1, 2)
[[[0. 1.]
  [2. 3.]]

 [[4. 5.]
  [6. 7.]]]


[[[1. 2.]]
 

 [[5. 6.]]]

import tensorflow as tf
import numpy as np

a = np.ones((8,256,256,3))
b = tf.reduce_mean(a,axis=[1,2],keepdims=True)
print(b.get_shape())

(8, 1, 1, 3)


import tensorflow as tf
import numpy as np

a = np.ones((8,256,256,3))
b = tf.reduce_mean(a,axis=0)
print(b.get_shape())

(256, 256, 3)


 

import tensorflow as tf
import numpy as np

a = np.ones((8,256,256,3))
b = tf.reduce_mean(a,axis=[0,3],keepdims=True)
print(b.get_shape())

(1, 256, 256, 1)

 

更多数学原理小文请关注公众号:未名方略

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞行codes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值