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
less = tf.Session()
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、tf.split(tensor, num_or_size_splits, axis = 0)。

   ① tensor:要对其进行分割的tensor;

   ② num_or_size_splits: num或shape, 若为num,则对tensor在第axis阶上平均分割成num个子tensor(此时要求tensor在第axis阶上的维数能被num整除),若为shape,则对tensor在axis维上分割成len(shape)个子tensor所组成的列表,子tensor在axis维上的长度分别为shape[0],shape[1],…,shape[len(shape)-1];
              ③ axis: 分割的维。例:

import tensorflow as tf
sess = tf.Session()
value = np.reshape(np.arange(1, 37), (2, 3, 6))
value = tf.constant(value)
sess.run(value) 
# 运行结果:
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]]])

splits= tf.split(value, 3, 2)
sess.run(splits)
# 运行结果:
[array([[[ 1,  2],
         [ 7,  8],
         [13, 14]],
 
        [[19, 20],
         [25, 26],
         [31, 32]]]), 
 array([[[ 3,  4],
         [ 9, 10],
         [15, 16]],
 
        [[21, 22],
         [27, 28],
         [33, 34]]]), 
 array([[[ 5,  6],
         [11, 12],
         [17, 18]],
 
        [[23, 24],
         [29, 30],
         [35, 36]]])]

splits= tf.split(value, [1,2,3], 2)
sess.run(splits)
## 运行结果:
[array([[[ 1],
         [ 7],
         [13]],
 
        [[19],
         [25],
         [31]]]), 
 array([[[ 2,  3],
         [ 8,  9],
         [14, 15]],
 
        [[20, 21],
         [26, 27],
         [32, 33]]]), 
 array([[[ 4,  5,  6],
         [10, 11, 12],
         [16, 17, 18]],
 
        [[22, 23, 24],
         [28, 29, 30],
         [34, 35, 36]]])]

3、tf.concat(values, axis, name='concat')。将values中的张量按照指定维度axis进行合并, 例如values为[t1, t2, t3],则要求t1,t2,t3三个张量在除了第axis阶之外的其他阶上的维度必须相同。例:

import tensorflow as tf
sess = tf.Session()
value1 = np.reshape(np.arange(1,  25), (2, 3, 4))
value2 = np.reshape(np.arange(25, 41), (2, 2, 4))
value3 = np.reshape(np.arange(41, 49), (2, 1, 4))
t1 = tf.constant(value1)
t2 = tf.constant(value2)
t3 = tf.constant(value3)
sess.run([t1, t2, t3])
# 运行结果:
[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]]]),  ##(2,3,4)型

 array([[[25, 26, 27, 28],
         [29, 30, 31, 32]],
        [[33, 34, 35, 36],
         [37, 38, 39, 40]]]), ## (2,2,4)型

 array([[[41, 42, 43, 44]],
        [[45, 46, 47, 48]]])] ## (2,1,4)型

T = tf.concat([t1, t2, t3], axis=1)
sess.run(T)
# 运行结果:
array([[[ 1,  2,  3,  4],
        [ 5,  6,  7,  8],
        [ 9, 10, 11, 12],
        [25, 26, 27, 28],
        [29, 30, 31, 32],
        [41, 42, 43, 44]],

       [[13, 14, 15, 16],
        [17, 18, 19, 20],
        [21, 22, 23, 24],
        [33, 34, 35, 36],
        [37, 38, 39, 40],
        [45, 46, 47, 48]]])

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值