pytorch:concat和stack的区别

整体来讲,concat是对dim进行拼接,stack是对dim维进行堆叠。
concat:不会增加新的维度,在指定维度上拼接。
stack:增加一个新的维度将两个单位,然后再上一维度分别进行堆叠。

下面的代码仅为直观维度上的变化,第二部分有图像辅助理解。

代码测试

测试1:以两个[3,4]矩阵a,b作为输入进行concat、stack测试
输入:

#data a and b
a = torch.tensor([[1,2,3,4],
                 [5,6,7,8]],dtype=torch.int16)
b = torch.tensor([[9,10,11,12],
                 [13,14,15,16]],dtype=torch.int16)
print("a:\n{}".format(a))
print("b:\n{}".format(b))
#dim=0
ab_concat_0 = torch.cat([a, b], dim=0)
ab_stack_0 = torch.stack([a, b], dim=0)
print("ab_concat_0:\n{}\nshape:{}".format(ab_concat_0,ab_concat_0.shape))
print("ab_stack_0:\n{}\nshape:{}".format(ab_stack_0,ab_stack_0.shape))
#dim=1
ab_concat_1 = torch.cat([a, b], dim=1)
ab_stack_1 = torch.stack([a, b], dim=1)
print("ab_concat_1:\n{}\nshape:{}".format(ab_concat_1,ab_concat_1.shape))
print("ab_stack_1:\n{}\nshape:{}".format(ab_stack_1,ab_stack_1.shape))
#dim=2
# ab_concat_2 = torch.cat([a, b], dim=2) error
ab_stack_2 = torch.stack([a, b], dim=2)
# print("ab_concat_2:\n{}".format(ab_concat_2))
print("ab_stack_2:\n{}\nshape:{}".format(ab_stack_2,ab_stack_2.shape))

输出:

#data
a:
tensor([[ 1,  2,  3,  4],
        [ 5,  6,  7,  8],
        [ 9, 10, 11, 12]], dtype=torch.int16)
b:
tensor([[13, 14, 15, 16],
        [17, 18, 19, 20],
        [21, 22, 23, 24]], dtype=torch.int16)
#dim=0
ab_concat_0:
tensor([[ 1,  2,  3,  4],
        [ 5,  6,  7,  8],
        [ 9, 10, 11, 12],
        [13, 14, 15, 16],
        [17, 18, 19, 20],
        [21, 22, 23, 24]], dtype=torch.int16)
shape:torch.Size([6, 4])
ab_stack_0:
tensor([[[ 1,  2,  3,  4],
         [ 5,  6,  7,  8],
         [ 9, 10, 11, 12]],

        [[13, 14, 15, 16],
         [17, 18, 19, 20],
         [21, 22, 23, 24]]], dtype=torch.int16)
shape:torch.Size([2, 3, 4])
#dim=1
ab_concat_1:
tensor([[ 1,  2,  3,  4, 13, 14, 15, 16],
        [ 5,  6,  7,  8, 17, 18, 19, 20],
        [ 9, 10, 11, 12, 21, 22, 23, 24]], dtype=torch.int16)
shape:torch.Size([3, 8])
ab_stack_1:
tensor([[[ 1,  2,  3,  4],
         [13, 14, 15, 16]],

        [[ 5,  6,  7,  8],
         [17, 18, 19, 20]],

        [[ 9, 10, 11, 12],
         [21, 22, 23, 24]]], dtype=torch.int16)
shape:torch.Size([3, 2, 4])
#dim=2
ab_stack_2:
tensor([[[ 1, 13],
         [ 2, 14],
         [ 3, 15],
         [ 4, 16]],

        [[ 5, 17],
         [ 6, 18],
         [ 7, 19],
         [ 8, 20]],

        [[ 9, 21],
         [10, 22],
         [11, 23],
         [12, 24]]], dtype=torch.int16)
shape:torch.Size([3, 4, 2])

测试2:升一个维度,用两个维度为[3,4,5]的矩阵进行测试

#data
a:
shape:torch.Size([3, 4, 5])
b:
shape:torch.Size([3, 4, 5])

#dim=0
ab_concat_0:
shape:torch.Size([6, 4, 5])

ab_stack_0:
shape:torch.Size([2, 3, 4, 5])
#dim=1
ab_concat_1:
shape:torch.Size([3, 8, 5])

ab_stack_1:
shape:torch.Size([3, 2, 4, 5])
#dim=2
ab_concat_2:
shape:torch.Size([3, 4, 10])

ab_stack_2:
shape:torch.Size([3, 4, 2, 5])

在图像上

concat

可以理解为沿着dim维延申拼接。
dim=0:
在dim=0,该维度上的单位为红色框的A=(4,3)矩阵,此时concat表示在dim=0上,以A为单位进行延申拼接。
在这里插入图片描述
dim=1:
在dim=1,该维度上的单位为类似红色框的B=(1,3)矩阵,此时concat表示在dim=1上,以B为单位进行延申拼接。
在这里插入图片描述

dim=2:
在dim=1,该维度上的单位为类似红色框的C=(3,1)矩阵,此时concat表示在dim=2上,以C为单位进行延申拼接。
在这里插入图片描述

stack

两个矩阵分别将dim维度下的所有数据为单位,在新的维度上进行堆叠。
dim=0:
在dim=1,此维度下以A=(3,4,3)为单位,如红色框所示,以新的维度分别在上一维((3,4,3)的上一维度为0,也就是直接将单位在新维度堆叠形成新维度)进行堆叠,得到(3,2,4,3)。
在这里插入图片描述
dim=1:
在dim=1,此维度下以B=(4,3)为单位,如红色框所示,以新的维度分别在上一维((4,3)的上一维度为3)进行堆叠,得到(3,2,4,3)。
在这里插入图片描述
dim=2:
在dim=2,此维度下以C=(3)为单位,如红色框所示,以新的维度分别在上一维((3)的上一维度为4)进行堆叠,得到C’=(4,2,3),然后,以C’为单位,分别在上一维((4,2,3)的上一维度为3)进行堆叠得到(3,4,2,3)。
在这里插入图片描述

dim=3:
在dim=3,此维度下以D=[3] 的下一维度为单位,即最小元素,如红色框所示,以新的维度分别在上一维(上一维度为3)进行堆叠,得到D’=(3,2),然后,以D’为单位,分别在上一维((3,2)的上一位度为4)进行堆叠得到(4,2,3),然后同理得到(3,4,3,2)。
在这里插入图片描述
上述仅为个人理解,如有问题欢迎交流指正。

  • 22
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值