**
**
cat/stack
cat在指定的维度上进行连接;
stack创建了新的维度进行连接。
In [1]: import torch
In [2]: a = torch.rand(4,32,8)
In [3]: b = torch.rand(5,32,8)
In [4]: torch.cat([a,b],dim=0).shape
Out[4]: torch.Size([9, 32, 8])
In [5]: a1 = torch.rand(4,3,32,32)
In [6]: a2 = torch.rand(5,3,32,32)
In [7]: torch.cat([a1,a2],dim=0).shape
Out[7]: torch.Size([9, 3, 32, 32])
In [8]: a2 = torch.rand(4,1,32,32)
In [9]: torch.cat([a1,a2],dim=1).shape
Out[9]: torch.Size([4, 4, 32, 32])
In [10]: a1 = torch.rand(4,3,16,32)
In [11]: a2 = torch.rand(4,3,16,32)
In [12]: torch.cat([a1,a2],dim=2).shape
Out[12]: torch.Size([4, 3, 32, 32])
In [10]: a1 = torch.rand(4,3,16,32)
In [11]: a2 = torch.rand(4,3,16,32)
In [12]: torch.cat([a1,a2],dim=2).shape
Out[12]: torch.Size([4, 3, 32, 32])
In [13]: torch.stack([a1,a2],dim=2).shape
Out[13]: torch.Size([4, 3, 2, 16, 32])
In [14]: a = torch.rand(32,8)
In [15]: b = torch.rand(32,8)
In [16]: torch.stack([a,b],dim=0).shape
Out[16]: torch.Size([2, 32, 8])
**
**
split/chunk
split根据每个单元固定长度来拆分或者根据需要的长度来拆分
chunk按照数量来拆分,给定的参数为需要拆分的数量
In [14]: a = torch.rand(32,8)
In [15]: b = torch.rand(32,8)
In [16]: torch.stack([a,b],dim=0).shape
Out[16]: torch.Size([2, 32, 8])
In [17]: a = torch.rand(32,8)
In [18]: b = torch.rand(32,8)
In [19]: c = torch.stack([a,b],dim=0)
In [20]: a.shape,b.shape,c.shape
Out[20]: (torch.Size([32, 8]), torch.Size([32, 8]), torch.Size([2, 32, 8]))
In [21]: aa,bb = c.split([1,1],dim=0)
In [22]: aa.shape,bb.shape
Out[22]: (torch.Size([1, 32, 8]), torch.Size([1, 32, 8]))
In [23]: aa,bb = c.split(1,dim=0)
In [24]: aa.shape,bb.shape
Out[24]: (torch.Size([1, 32, 8]), torch.Size([1, 32, 8]))
In [25]: aa,bb = c.split(2,dim=0)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-25-27b6f4946a79> in <module>
----> 1 aa,bb = c.split(2,dim=0)
ValueError: not enough values to unpack (expected 2, got 1)
In [32]: a = torch.rand(6,32,8)
In [33]: aa,bb,cc = a.split(2,dim=0)#固定长度拆分
In [34]: aa.shape,bb.shape,cc.shape
Out[34]: (torch.Size([2, 32, 8]), torch.Size([2, 32, 8]), torch.Size([2, 32, 8]))
In [35]: aa,bb,cc = a.split([1,3,2],dim=0)#按照实际需求长度拆分
In [36]: aa.shape,bb.shape,cc.shape
Out[36]: (torch.Size([1, 32, 8]), torch.Size([3, 32, 8]), torch.Size([2, 32, 8]))
In [26]: aa,bb = c.chunk(2,dim=0)
In [27]: aa.shape,bb.shape
Out[27]: (torch.Size([1, 32, 8]), torch.Size([1, 32, 8]))
In [37]: aa,bb,cc = a.chunk(3,dim=0)
In [38]: aa.shape,bb.shape,cc.shape
Out[38]: (torch.Size([2, 32, 8]), torch.Size([2, 32, 8]), torch.Size([2, 32, 8]))