▪ Cat
▪ Stack
▪ Chunk
▪ Split
对于二维矩阵:
import torch
a = torch.rand(4,32,8)
b = torch.rand(5,32,8)
print(a.shape)
print(a.dim())
print(b.shape)
print(b.dim())
print(torch.cat([a,b], dim=0).shape) # dim = x
print(torch.cat([a,b], axis=0).shape) # axis = x
torch.Size([4, 32, 8])
3
torch.Size([5, 32, 8])
3
torch.Size([9, 32, 8])
torch.Size([9, 32, 8])
a1 = torch.rand(4, 3, 32, 32)
a2 = torch.rand(5, 3, 32, 32)
torch.cat([a1, a2], dim=0).shape
torch.Size([9, 3, 32, 32])
a = torch.tensor([[1,2,3,4], [1,2,3,4]])
print(a.shape)
b = torch.tensor([[1,2,3,4,5], [1,2,3,4,5]])
print(b.shape)
c = torch.cat([a,b], dim = 1) # 二维按照列进行连接
print(c)
torch.Size([2, 4])
torch.Size([2, 5])
tensor([[1, 2, 3, 4, 1, 2, 3, 4, 5],
[1, 2, 3, 4, 1, 2, 3, 4, 5]])
a2 = torch.rand(4,1,32,32)
torch.cat([a1, a2], dim=0).shape
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
in
1 a2 = torch.rand(4,1,32,32)
----> 2 torch.cat([a1, a2], dim=0).shape
RuntimeError: Sizes of tensors must match except in dimension 0. Got 3 and 1 in dimension 1 (The offending index is 1)
a2 = torch.rand(4,1,32,32)
torch.cat([a1,a2], dim=1).shape
torch.Size([4, 4, 32, 32])
a1 = torch.rand(4,3,16,32)
a2 = torch.rand(4,3,16,32)
torch.cat([a1,a2],dim=2).shape
torch.Size([4, 3, 32, 32])
# 0 1 2 3
# 4 3 16 32
# 4 3 16 32
a1 = torch.rand(4,3,16,32)
a2 = torch.rand(4,3,16,32)
torch.cat([a1, a2],dim = 2).shape # dim=2进行连接
torch.Size([4, 3, 32, 32])
# stack expects each tensor to be equal size
torch.stack([a1,a2], dim=2).shape #create new dim
torch.Size([4, 3, 2, 16, 32])
import torch
a = torch.rand(32, 8)
b = torch.rand(32, 8)
torch.stack([a,b], dim=0).shape
torch.Size([2, 32, 8])
a = torch.rand(32, 8)
a.shape
torch.Size([32, 8])
b = torch.rand([30, 8])
b.shape
torch.Size([30, 8])
torch.stack([a, b], dim=0)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
in
----> 1 torch.stack([a, b], dim=0)
RuntimeError: stack expects each tensor to be equal size, but got [32, 8] at entry 0 and [30, 8] at entry 1
torch.cat([a,b], dim=0).shape
torch.Size([62, 8])
c=torch.tensor([[1,4,7,9,11],[2,5,8,9,13]])
c
tensor([[ 1, 4, 7, 9, 11],
[ 2, 5, 8, 9, 13]])
print(torch.chunk(c,3,dim=1))
print(c.chunk(3, dim=1))
(tensor([[1, 4],
[2, 5]]), tensor([[7, 9],
[8, 9]]), tensor([[11],
[13]]))
(tensor([[1, 4],
[2, 5]]), tensor([[7, 9],
[8, 9]]), tensor([[11],
[13]]))
a=torch.rand(32, 8)
print(a.shape)
b=torch.rand(32, 8)
print(b.shape)
torch.Size([32, 8])
torch.Size([32, 8])
c = torch.stack([a,b],dim=0)
c.shape
torch.Size([2, 32, 8])
aa, bb = c.split(2, dim=0)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in
----> 1 aa, bb = c.split(2, dim=0)
ValueError: not enough values to unpack (expected 2, got 1)
aa, bb = c.chunk(2, dim=0) # 按照dim=0,均匀分成两份,最后不足的略小
aa.shape, bb.shape
(torch.Size([1, 32, 8]), torch.Size([1, 32, 8]))
第一个参数是待分割张量。
第二个参数有两种形式。
第一种是分割份数,这就和torch.chunk()一样了。
第二种这是分割方案,这是一个list,待分割张量将会分割为len(list)份,每一份的大小取决于list中的元素。
第三个参数为分割维度
section=[1,2,1,2,2]
d=torch.randn(8,4)
print(d.shape)
print(torch.split(d,section,dim=0))
torch.Size([8, 4])
(tensor([[ 0.8547, -0.1175, -0.4718, 2.6114]]), tensor([[-0.9087, 0.4853, 0.6559, -0.7248],
[ 0.7142, 0.5827, -0.1316, -0.3491]]), tensor([[-1.5595, -0.9129, 0.1164, 0.3764]]), tensor([[ 1.0861, 1.0993, -1.0416, 1.3909],
[ 1.0647, 0.1584, -0.6370, -0.8937]]), tensor([[ 0.1180, -1.3337, -1.5601, -0.8674],
[-0.1289, 1.0672, -0.3246, -0.8980]]))
a=torch.rand(32, 8)
print(a.shape)
b=torch.rand(32, 8)
print(b.shape)
torch.Size([32, 8])
torch.Size([32, 8])
# stack expects each tensor to be equal size
c = torch.stack([a,b], dim=0)
c.shape
torch.Size([2, 32, 8])
aa, bb = c.split([1, 1], dim=0)
aa.shape, bb.shape
(torch.Size([1, 32, 8]), torch.Size([1, 32, 8]))
aa, bb = c.split([30, 2], dim=1) # 30+2 = 32
aa.shape, bb.shape
(torch.Size([2, 30, 8]), torch.Size([2, 2, 8]))
aa, bb = c.split(1, dim=0)
aa.shape, b.shape
(torch.Size([1, 32, 8]), torch.Size([32, 8]))
aa, bb = c.split(2, dim=0)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in
----> 1 aa, bb = c.split(2, dim=0)
ValueError: not enough values to unpack (expected 2, got 1)
参考:https://www.cnblogs.com/moon3/p/12685911.html