【问题标题】:Split and extract values from a PyTorch tensor according to given dimensions根据给定的维度从 PyTorch 张量中拆分和提取值
【发布时间】:2020-07-20 16:32:12
【问题描述】:

我有一个张量 Aof sizetorch.Size([32, 32, 3, 3]),我想拆分它并从中提取一个大小为 torch.Size([16, 16, 3, 3]) 的张量 B。张量可以是 1d 或 4d,并且必须根据给定的新张量维度进行拆分。我已经能够生成目标尺寸,但我无法从源张量中拆分和提取值。我尝试过torch.narrow,但它只需要 3 个参数,而且在很多情况下我需要 4 个。 torch.split 将 dim 作为 int ,因为张量仅沿一维拆分。但我想将其拆分为多个维度。

【问题讨论】:

    标签: pytorch tensor


    【解决方案1】:

    您有多种选择:

    • 多次使用.split
    • 多次使用.narrow
    • 使用切片

    例如:

    t = torch.rand(32, 32, 3, 3)
    
    t0, t1 = t.split((16, 16), 0)
    print(t0.shape, t1.shape)
    >>> torch.Size([16, 32, 3, 3]) torch.Size([16, 32, 3, 3])
    
    t00, t01 = t0.split((16, 16), 1)
    print(t00.shape, t01.shape)
    >>> torch.Size([16, 16, 3, 3]) torch.Size([16, 16, 3, 3])
    
    t00_alt, t01_alt = t[:16, :16, :, :], t[16:, 16:, :, :]
    print(t00_alt.shape, t01_alt.shape)
    >>> torch.Size([16, 16, 3, 3]) torch.Size([16, 16, 3, 3])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-31
      • 2021-10-23
      • 2017-09-05
      • 1970-01-01
      • 2020-05-11
      • 2021-11-12
      • 2022-07-26
      相关资源
      最近更新 更多