欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

TensorFlow2.0:张量的合并与分割实例

程序员文章站 2023-11-29 18:23:46
** 一 tf.concat( ) 函数–合并 ** in [2]: a = tf.ones([4,35,8])...

**

一 tf.concat( ) 函数–合并
**

in [2]: a = tf.ones([4,35,8])                          

in [3]: b = tf.ones([2,35,8])                          

in [4]: c = tf.concat([a,b],axis=0)                       

in [5]: c.shape                                 
out[5]: tensorshape([6, 35, 8])

in [6]: a = tf.ones([4,32,8])                          

in [7]: b = tf.ones([4,3,8])                          

in [8]: c = tf.concat([a,b],axis=1)                       

in [9]: c.shape                                 
out[9]: tensorshape([4, 35, 8])

**

二 tf.stack( ) 函数–数据的堆叠,创建新的维度
**

in [2]: a = tf.ones([4,35,8])                          

in [3]: a.shape                                 
out[3]: tensorshape([4, 35, 8])

in [4]: b = tf.ones([4,35,8])                          

in [5]: b.shape                                 
out[5]: tensorshape([4, 35, 8])

in [6]: tf.concat([a,b],axis=-1).shape                     
out[6]: tensorshape([4, 35, 16])

in [7]: tf.stack([a,b],axis=0).shape                      
out[7]: tensorshape([2, 4, 35, 8])

in [8]: tf.stack([a,b],axis=3).shape                      
out[8]: tensorshape([4, 35, 8, 2])

**

三 tf.unstack( )函数–解堆叠
**

in [16]: a = tf.ones([4,35,8])                                                                                       

in [17]: b = tf.ones([4,35,8])                                                                                       

in [18]: c = tf.stack([a,b],axis=0)                                                                                     

in [19]: a.shape,b.shape,c.shape                                                                                      
out[19]: (tensorshape([4, 35, 8]), tensorshape([4, 35, 8]), tensorshape([2, 4, 35, 8]))

in [20]: aa,bb = tf.unstack(c,axis=0)                                                                                    

in [21]: aa.shape,bb.shape                                                                                         
out[21]: (tensorshape([4, 35, 8]), tensorshape([4, 35, 8]))

in [22]: res = tf.unstack(c,axis=1)                                                                                     

in [23]: len(res)                                                                                              
out[23]: 4

**

四 tf.split( ) 函数
**

in [16]: a = tf.ones([4,35,8])                                                                                       

in [17]: b = tf.ones([4,35,8])                                                                                       

in [18]: c = tf.stack([a,b],axis=0)                                                                                     

in [19]: a.shape,b.shape,c.shape                                                                                      
out[19]: (tensorshape([4, 35, 8]), tensorshape([4, 35, 8]), tensorshape([2, 4, 35, 8]))

in [20]: aa,bb = tf.unstack(c,axis=0)                                                                                    

in [21]: aa.shape,bb.shape                                                                                         
out[21]: (tensorshape([4, 35, 8]), tensorshape([4, 35, 8]))

in [22]: res = tf.unstack(c,axis=1)                                                                                     

in [23]: len(res)                                                                                              
out[23]: 4

以上这篇tensorflow2.0:张量的合并与分割实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。