Pytorch实现List Tensor转Tensor,reshape拼接等操作

Crq
Crq
管理员
1884
文章
0
粉丝
Linux教程评论5字数 198阅读0分39秒阅读模式
摘要这篇文章主要介绍了Pytorch实现List Tensor转Tensor,reshape拼接等操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

持续更新一些常用的Tensor操作,比如List,Numpy,Tensor之间的转换,Tensor的拼接,维度的变换等操作。

其它Tensor操作如 einsum等见:待更新。

用到两个函数:

  • torch.cat
  • torch.stack
  • 一、List Tensor转Tensor (torch.cat)

    Pytorch实现List Tensor转Tensor,reshape拼接等操作-图片1

    // An highlighted block
    >>> t1 = torch.FloatTensor([[1,2],[5,6]])
    >>> t2 = torch.FloatTensor([[3,4],[7,8]])
    >>> l = []
    >>> l.append(t1)
    >>> l.append(t2)
    >>> ta = torch.cat(l,dim=0)
    >>> ta = torch.cat(l,dim=0).reshape(2,2,2)
    >>> tb = torch.cat(l,dim=1).reshape(2,2,2)
    >>> ta
    tensor([[[1., 2.],
             [5., 6.]],
            [[3., 4.],
             [7., 8.]]])
    >>> tb
    tensor([[[1., 2.],
             [3., 4.]],
            [[5., 6.],
             [7., 8.]]])
    高维tensor

    ** 如果理解了2D to 3DTensor,以此类推,不难理解3D to 4D,看下面代码即可明白:**

    >>> t1 = torch.range(1,8).reshape(2,2,2)
    >>> t2 = torch.range(11,18).reshape(2,2,2)
    >>> l = []
    >>> l.append(t1)
    >>> l.append(t2)
    >>> torch.cat(l,dim=2).reshape(2,2,2,2)
    tensor([[[[ 1.,  2.],
              [11., 12.]],
             [[ 3.,  4.],
              [13., 14.]]],
            [[[ 5.,  6.],
              [15., 16.]],
             [[ 7.,  8.],
              [17., 18.]]]])
    >>> torch.cat(l,dim=1).reshape(2,2,2,2)
    tensor([[[[ 1.,  2.],
              [ 3.,  4.]],
             [[11., 12.],
              [13., 14.]]],
            [[[ 5.,  6.],
              [ 7.,  8.]],
             [[15., 16.],
              [17., 18.]]]])
    >>> torch.cat(l,dim=0).reshape(2,2,2,2)
    tensor([[[[ 1.,  2.],
              [ 3.,  4.]],
             [[ 5.,  6.],
              [ 7.,  8.]]],
            [[[11., 12.],
              [13., 14.]],
             [[15., 16.],
              [17., 18.]]]])
    二、List Tensor转Tensor (torch.stack)

    Pytorch实现List Tensor转Tensor,reshape拼接等操作-图片2

    代码:

    import torch
    t1 = torch.FloatTensor([[1,2],[5,6]])
    t2 = torch.FloatTensor([[3,4],[7,8]])
    l = [t1, t2]
    t3 = torch.stack(l, dim=2)
    print(t3.shape)
    print(t3)
    ## output:
    ## torch.Size([2, 2, 2])
    ## tensor([[[1., 3.],
    ##          [2., 4.]],
    ##        [[5., 7.],
    ##         [6., 8.]]])

    weinxin
    我的微信
    微信号已复制
    我的微信
    这是我的微信扫一扫
     
    Crq
    • 本文由 Crq 发表于2025年3月15日 15:38:26
    • 转载请注明:https://www.cncrq.com/13461.html
    详细讲解Java访问者设计模式 Linux教程

    详细讲解Java访问者设计模式

    大多数情况下你不需要访问者模式,但当一旦需要访问者模式时,那就是真的需要它了,这是设计模式创始人的原话。可以看出应用场景比较少,但需要它的时候是不可或缺的,这篇文章就开始学习最后一...
    Docker基础技术:Linux Namespace(上) Linux教程

    Docker基础技术:Linux Namespace(上)

    时下最热的技术莫过于Docker了,很多人都觉得Docker是个新技术,其实不然,Docker除了其编程语言用go比较新外,其实它还真不是个新东西,也就是个新瓶装旧酒的东西,所谓的...
    带你走进rsync的世界 Linux教程

    带你走进rsync的世界

    Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件,也可以使用 Rsync 同步本地硬盘中的不同目录。rsync...
    在 Lua 中使用参数技巧 Linux教程

    在 Lua 中使用参数技巧

    大多数计算机命令由两部分组成:命令和参数。命令是要执行的程序,而参数可能是命令选项或用户输入。如果没有这种结构,用户将不得不编辑命令的代码,以改变命令所处理的数据。
    匿名

    发表评论

    匿名网友
    :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
    确定

    拖动滑块以完成验证