pytorch-拼接与拆分

在这里插入图片描述

拼接与拆分

▪ Cat
▪ Stack
▪ Chunk
▪ Split

cat (竖着堆)

torch.cat(tensors,dim=0,out=None)

对于二维矩阵:

  • 第一个参数tensors是你想要连接的若干个张量,按你所传入的顺序进行连接,注意每一个张量需要形状相同,或者更准确的说,进行行连接的张量要求列数相同,进行列连接的张量要求行数相同;
  • 第二个参数dim表示维度,dim=0则表示按行连接,dim=1表示按列连接 。
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])

cat的例子

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)

<ipython-input-14-658c5d295176> in <module>
      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])

stack(横着堆,多出了一个维度,create new dim)

# 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)

<ipython-input-10-6b2ccc0c3730> in <module>
----> 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])

Chunk

torch.cat()函数是把各个tensor连接起来,这里的torch.chunk()的作用是把一个tensor均匀分割成若干个小tensor

torch.chunk(intput,chunks,dim=0)

  • 第一个参数input是你想要分割的tensor
  • 第二个参数chunks是你想均匀分割的份数,如果该tensor在你要进行分割的维度上的size不能被chunks整除,则最后一份会略小(也可能为空)
  • 第三个参数表示分割维度,dim=0按行分割,dim=1表示按列分割
  • 该函数返回由小tensor组成的list
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)

<ipython-input-29-986cc89b358d> in <module>
----> 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]))

Split

这个函数可以说是torch.chunk()函数的升级版本,它不仅可以按份数均匀分割,还可以按特定方案进行分割。

torch.split(tensor,split_size_or_sections,dim=0)

  • 第一个参数是待分割张量。

  • 第二个参数有两种形式。

     第一种是分割份数,这就和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)

<ipython-input-18-986cc89b358d> in <module>
----> 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值