• 工资「喂饱肚子」,副业「养活灵魂」!

  • 在本站开通年度VIP,无限制下载本站资源和阅读本站文章

  • 欢迎大家交换友链,可在https://www.stubbornhuang.com/申请友情链接进行友链交换申请!

  • 计算机图形学与计算几何经典必备书单整理,下载链接可参考:https://www.stubbornhuang.com/1256/

  • 如果觉得本站的内容有帮助,可以考虑打赏博主哦!

  • 感谢大家访问本站,希望本站的内容可以帮助到大家!

  • 本站会放置Google广告用于维持域名以及网站服务器费用。

  • 问题反馈可发送邮件到stubbornhuang@qq.com

  • 本站由于前段时间遭受到大量临时和国外邮箱注册,所以对可注册的邮箱类型进行了限制!

Pytorch – torch.cat参数详解与使用

Pytorch 发布于2022-07-25 阅读 7,052次 0次评论 1次点赞 本文共2323个字,阅读需要6分钟。

1 torch.cat参数详解与使用

1.1 torch.cat

1.函数形式

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

2.函数功能

在指定的维度串联指定Tensor序列,所有Tensor都必须具有相同的形状(连接维度除外),或者Tensor为空。

torch.cat可以看作是torch.split或者torch.chunk的反操作。

3.函数参数

  • tensors:Tensor序列,除cat的维度形状可以不同之外,输入的Tensor必须类型相同且形状相同
  • dim:int类型,Tensor序列连接的维度

4.函数返回值

返回串联好的Tensor

1.2 torch.cat的使用

1.2.1 torch.cat串联一维Tensor序列

import torch

if __name__ == '__main__':
    tensor1 = torch.tensor([1,2,3,4])
    tensor2 = torch.tensor([5,6,7,8])
    tensor3 = torch.tensor([9,10,11,12])
    tensor4 = torch.tensor([13,14,15,16])

    output0 = torch.cat([tensor1,tensor2,tensor3,tensor4],dim=0)

    print(output0)

输出

tensor([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16])

1.2.2 torch.cat串联二维Tensor序列

import torch

if __name__ == '__main__':
    tensor1 = torch.tensor([1,2,3,4]).view(2,2)
    tensor2 = torch.tensor([5,6,7,8]).view(2,2)
    tensor3 = torch.tensor([9,10,11,12]).view(2,2)
    tensor4 = torch.tensor([13,14,15,16]).view(2,2)

    output0 = torch.cat([tensor1,tensor2,tensor3,tensor4],dim=0)
    output1 = torch.cat([tensor1, tensor2, tensor3, tensor4], dim=1)

    print('torch.cat dim=0:{0},{1}'.format(output0,output0.shape))
    print('torch.cat dim=1:{0},{1}'.format(output1,output1.shape))

输出

torch.cat dim=0:tensor([[ 1,  2],
        [ 3,  4],
        [ 5,  6],
        [ 7,  8],
        [ 9, 10],
        [11, 12],
        [13, 14],
        [15, 16]]),torch.Size([8, 2])
torch.cat dim=1:tensor([[ 1,  2,  5,  6,  9, 10, 13, 14],
        [ 3,  4,  7,  8, 11, 12, 15, 16]]),torch.Size([2, 8])

1.2.3 torch.cat串联三维Tensor序列

import torch

if __name__ == '__main__':
    tensor1 = torch.arange(0,8).view(2,2,2)
    tensor2 = torch.arange(8,16).view(2,2,2)
    tensor3 = torch.arange(16,24).view(2,2,2)
    tensor4 = torch.arange(24,32).view(2,2,2)

    output0 = torch.cat([tensor1,tensor2,tensor3,tensor4],dim=0)
    output1 = torch.cat([tensor1, tensor2, tensor3, tensor4], dim=1)
    output2 = torch.cat([tensor1, tensor2, tensor3, tensor4], dim=2)

    print('torch.cat dim=0:{0},{1}'.format(output0,output0.shape))
    print('torch.cat dim=1:{0},{1}'.format(output1,output1.shape))
    print('torch.cat dim=2:{0},{1}'.format(output2,output2.shape))

输出

torch.cat dim=0:tensor([[[ 0,  1],
         [ 2,  3]],

        [[ 4,  5],
         [ 6,  7]],

        [[ 8,  9],
         [10, 11]],

        [[12, 13],
         [14, 15]],

        [[16, 17],
         [18, 19]],

        [[20, 21],
         [22, 23]],

        [[24, 25],
         [26, 27]],

        [[28, 29],
         [30, 31]]]),torch.Size([8, 2, 2])
torch.cat dim=1:tensor([[[ 0,  1],
         [ 2,  3],
         [ 8,  9],
         [10, 11],
         [16, 17],
         [18, 19],
         [24, 25],
         [26, 27]],

        [[ 4,  5],
         [ 6,  7],
         [12, 13],
         [14, 15],
         [20, 21],
         [22, 23],
         [28, 29],
         [30, 31]]]),torch.Size([2, 8, 2])
torch.cat dim=2:tensor([[[ 0,  1,  8,  9, 16, 17, 24, 25],
         [ 2,  3, 10, 11, 18, 19, 26, 27]],

        [[ 4,  5, 12, 13, 20, 21, 28, 29],
         [ 6,  7, 14, 15, 22, 23, 30, 31]]]),torch.Size([2, 2, 8])

欢迎扫码关注我的微信公众号,及时获取文章更新

微信公众号二维码

本文作者:StubbornHuang

版权声明:本文为站长原创文章,如果转载请注明原文链接!

原文标题:Pytorch – torch.cat参数详解与使用

原文链接:https://www.stubbornhuang.com/2216/

发布于:2022年07月25日 11:23:32

修改于:2023年06月25日 20:51:13

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

文章末尾
上一篇
资源分享 - Speech and Language Processing - An Introduction to Natural Language Processing, Computational Linguistics, and Speech Recognition , Second Edition 英文PDF下载
AI资源
下一篇
Pytorch - torch.stack参数详解与使用
Pytorch
当前分类随机文章推荐

发表评论

您必须 [ 登录 ] 才能发表留言!

关注我们的公众号

微信公众号