1. 程式人生 > >Tensorflow中的axis小結

Tensorflow中的axis小結

Tensorflow中的一些函式包含axis引數,在對tensor進行處理時,表示對tensor的第axis階上的資料進行某種操作。

1、out_tensor = tf.reduce_sum( input_tensor, axis , keep_dims = False)。例如,input_tensor為(5,3,6)型的,tf.reduce_sum( input_tensor, 1 )表示對第1上的資料進行加和,out_tensor為(5, 6)型的(keep_dims預設值為False,第1階的被加和後毀掉了,若設定keep_dims=True,則為(5,1,6)型),out_tensor第i行,第j列的元素為sum(input_tensor[i,:,j]),其中,i = 0, 1,2,3,4, j = 0,1,2,3,4,5。例

import tensorflow as tf
const = tf.constant(   np.arange(1, 91).reshape(5,3,6)   )
sess.run(const)
## 執行結果:
array([[[ 1,  2,  3,  4,  5,  6],
        [ 7,  8,  9, 10, 11, 12],
        [13, 14, 15, 16, 17, 18]],

       [[19, 20, 21, 22, 23, 24],
        [25, 26, 27, 28, 29, 30],
        [31, 32, 33, 34, 35, 36]],

       [[37, 38, 39, 40, 41, 42],
        [43, 44, 45, 46, 47, 48],
        [49, 50, 51, 52, 53, 54]],

       [[55, 56, 57, 58, 59, 60],
        [61, 62, 63, 64, 65, 66],
        [67, 68, 69, 70, 71, 72]],

       [[73, 74, 75, 76, 77, 78],
        [79, 80, 81, 82, 83, 84],
        [85, 86, 87, 88, 89, 90]]])

sess.run(tf.reduce_sum(const, axis=1))
## 執行結果:
array([[ 21,  24,  27,  30,  33,  36],
       [ 75,  78,  81,  84,  87,  90],
       [129, 132, 135, 138, 141, 144],
       [183, 186, 189, 192, 195, 198],
       [237, 240, 243, 246, 249, 252]])

   對於函式 tf.reduce_max( input_tensor, axis ), tf.reduce_min( input_tensor, axis ),tf.reduce_mean( input_tensor, axis )等,情況類似。

2、