TensorFlow2.0——5、合并与分割

  • 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, 358])

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, 358])

In [19]: a.shape
Out[19]: TensorShape([4, 358])

In [23]: b. shape
Out[23]: Tensor Shape([4, 358])

In [20]: tf.concat([a, b], axis=-1). shape
Out[20]: Tenson Shape([4, 3516])

在这里插入图片描述


tf.stack

创建新维度

所有维度必须相同

In [21]: tf.stack([a, b], axis=0). shape 
Out[21]: Tenson Shape([2, 4, 358])

In [22]: tf.stack([a, b],axis=3).shape
Out[22]: Tensor Shape([4, 358, 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, 358]))
#[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([24, 354])

***指定具体长度,将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, 4352]),TensorShape([2, 4, 354]))

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值