- tf.concat
- tf.split
- tf.stack
- tf.unstack
注意(axis=?)
tf.concat
合并操作
除了axis可以不同,其他必须相同
In [3]: a=tf. ones([4,35,8])
In [4]: b=tf.ones([2,35,8])
In [6]: c=tf.concat([a,b],axis=0)
In [7]: C. shape
Out[7]: TensorShape([6, 35, 8])
In [8]: a=tf. ones([4,32,8])
In [9]: b=tf.ones([4,3,8])
In [10]: tf.concat([a, b],axis=1). shape
Out[10]: TensorShape([4, 35, 8])
In [19]: a.shape
Out[19]: TensorShape([4, 35, 8])
In [23]: b. shape
Out[23]: Tensor Shape([4, 35, 8])
In [20]: tf.concat([a, b], axis=-1). shape
Out[20]: Tenson Shape([4, 35,16])
tf.stack
创建新维度
所有维度必须相同
In [21]: tf.stack([a, b], axis=0). shape
Out[21]: Tenson Shape([2, 4, 35,8])
In [22]: tf.stack([a, b],axis=3).shape
Out[22]: Tensor Shape([4, 35, 8, 2])
Unstack
对等打散维度
In [30]: a.shape # Tensor Shape([4, 35, 8])
In [32]: b=tf.ones([4,35,8])
In [33]: c=tf.stack([a, b])
In [34]: C. shape
Out[34]: Tensor Shape([2, 4, 35, 8])
In [35]: aa, bb=tf. unstack(c, axis=0)
In [36]: aa. shape,bb. shape
Out[36]: (TensorShape([4, 35, 8]), Tensor Shape([4, 35, 8]))
#[2,4,35,8]
In [41]: res=tf. unstack(c, axis=3)
In [42]: res[0].shape, res[7]. shape
Out[42]: (Tensor Shape([2, 4, 35]), Tensor Shape([2, 4, 35]))
#[2,4,35,8]
In [43]: res=tf .unstack(c,axis=3)
In [44]: len(res)
Out[44]: 8
In [45]: res=tf.split(c,axis=3, num_ or_ size_ splits=2)
In [46]: len(res)
Out[46]: 2
In [47]: res[0]. shape
0ut[47]: Tensor Shape([2,4, 35,4])
***指定具体长度,将8分为[2,2,4]
In [48]: res=tf.split(c,axis=3, num_ or_ _size_ splits=[2,2,4])
In [49]: res[0]. shape, res[2]. shape
Out[49]: (Tensor Shape([2, 4,35,2]),TensorShape([2, 4, 35,4]))