tensorflow的axis——軸研科技

目錄

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

tf.argmax(), 自帶降低一維

import tensorflow as tf
import numpy as np

x = np.ones((2,3,3,1))
z = tf.argmax(x,axis = 3)

print(z.get_shape())

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

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

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

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

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

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

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

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

 [[0]
  [0]
  [0]]

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


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.arange(24.0).reshape(2,2,2,3)
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, 3)

[[[[ 4.5  5.5  6.5]]]


 [[[16.5 17.5 18.5]]]]

import tensorflow as tf
import numpy as np

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

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

print(b)

[[[[ 6.  7.  8.]
   [ 9. 10. 11.]]

  [[12. 13. 14.]
   [15. 16. 17.]]]]

import tensorflow as tf
import numpy as np

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

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

print(b)

[[[[ 7.]
   [10.]]

  [[13.]
   [16.]]]]

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章