【问题标题】:Concatination on a 3 dimensional tensor (Tensor Re-Shaping)3 维张量上的连接(张量整形)
【发布时间】:2021-07-03 19:23:13
【问题描述】:

我有 2 个张量,

目前它们的格式分别为 [13, 2]。我正在尝试将两者组合成一个维度为 [2, 13, 2] 的 3 维张量,以便它们彼此堆叠,但是作为批次分开。

这里是格式为 [13, 2] 的张量之一的示例:

tensor([[[-1.8588,  0.3776],
         [ 0.1683,  0.2457],
         [-1.2740,  0.5683],
         [-1.7262,  0.4350],
         [-1.7262,  0.4350],
         [ 0.1683,  0.2457],
         [-1.0160,  0.5940],
         [-1.3354,  0.5565],
         [-0.7497,  0.5792],
         [-0.2024,  0.4251],
         [ 1.0791, -0.2770],
         [ 0.3032,  0.1706],
         [ 0.8681, -0.1607]])

我想保持形状,但将它们分成 2 组在同一个张量中。下面是我所追求的格式示例:

tensor([[[-1.8588,  0.3776],
         [ 0.1683,  0.2457],
         [-1.2740,  0.5683],
         [-1.7262,  0.4350],
         [-1.7262,  0.4350],
         [ 0.1683,  0.2457],
         [-1.0160,  0.5940],
         [-1.3354,  0.5565],
         [-0.7497,  0.5792],
         [-0.2024,  0.4251],
         [ 1.0791, -0.2770],
         [ 0.3032,  0.1706],
         [ 0.8681, -0.1607]],

         [[-1.8588,  0.3776],
         [ 0.1683,  0.2457],
         [-1.2740,  0.5683],
         [-1.7262,  0.4350],
         [-1.7262,  0.4350],
         [ 0.1683,  0.2457],
         [-1.0160,  0.5940],
         [-1.3354,  0.5565],
         [-0.7497,  0.5792],
         [-0.2024,  0.4251],
         [ 1.0791, -0.2770],
         [ 0.3032,  0.1706],
         [ 0.8681, -0.1607]]])

有人对如何使用连接有任何想法吗?我在使用 torch.cat((a, b.unsqueeze(0)), dim=-1) 时尝试过使用 .unsqueeze,但是它将格式更改为 [13, 4, 1],这不是我所追求的格式。

下面的解决方案有效,但是,我的想法是我将通过循环继续堆叠到 y 而不受形状的限制。很抱歉没有把我的想法表达得足够清楚。

它们的大小都是 [13,2],所以它会以 [1,13,2], [2,13,2], [3,13,2], [4, 13,2] 等等...

【问题讨论】:

  • 澄清一下:你有两个形状为 (13,2) 的张量,你想用它们制作一个大小为 (2,26,2) 的张量?不是 (2, 13, 2) 也不是 (26, 2),你确定吗?因为这意味着您需要的不仅仅是串联,您的张量中没有足够的数字来生成 (2,26,2) 张量。您也想以某种方式复制它们吗?
  • 啊抱歉我的错,我想我自己弄糊涂了,我现在会改变它,但我的理想结果是 [2, 13, 2],希望这更有意义吗?

标签: python pytorch concatenation reshape


【解决方案1】:

在这种情况下,你需要torch.stack而不是torch.cat,至少它更方便:

x1 = torch.randn(13,2)
x2 = torch.randn(13,2)
y = torch.stack([x1,x2], 0) # creates a new dimension 0
print(y.shape)
>>> (2, 13, 2)

虽然您确实可以使用unsqueezecat,但您需要解压缩两个输入张量:

x1 = torch.randn(13,2).unsqueeze(0) # shape: (1,13,2)
x2 = torch.randn(13,2).unsqueeze(0) # same
y = torch.cat([x1, x2], 0)
print(y.shape)
>>> (2,13,2)

这里有一个有用的线程来理解差异:difference between cat and stack

如果您需要堆叠更多张量,这并不难,堆叠适用于任意数量的张量:

# This list of tensors is what you will build in your loop
tensors = [torch.randn(13, 2) for i in range(10)]
# Then at the end of the loop, you stack them all together
y = torch.stack(tensors, 0)
print(y.shape)
>>> (10, 13, 2)

或者,如果您不想使用该列表:

# first, build the y tensor to which the other ones will be appended
y = torch.empty(0, 13, 2)
# Then the loop, and don't forget to unsqueeze
for i in range(10):
    x = torch.randn(13, 2).unsqueeze(0)
    y = torch.cat([y, x], 0)

print(y.shape)
>>> (10, 13, 2)

【讨论】:

  • 惊人的堆栈功能工作,谢谢!
  • 很抱歉再次打扰,但这是一个后续问题,这是一个循环的一部分,因此它不会是 x1 和 x2,而是我要添加的一大堆组玩具。看来您的方法仅适用于组合 2,有没有办法通过循环继续添加到 y 而不受形状限制?它们的大小都是 [13,2],所以它会以 [1,13,2]、[2,13,2]、[3,13,2]、[4,13,2] 的形式上升] 等等...
  • 非常感谢您的帮助,它有效!
  • 请接受答案,然后关闭线程。很高兴有帮助
猜你喜欢
  • 1970-01-01
  • 2018-10-04
  • 1970-01-01
  • 2021-05-17
  • 2017-09-22
  • 2019-02-19
  • 2022-09-29
  • 2018-12-13
  • 2022-01-22
相关资源
最近更新 更多