PyTorch:在张量的单例尺寸上应用映射

时间:2019-04-20 21:42:00

标签: python numpy pytorch reshape tensor

恐怕标题不是很具描述性,但我想不出更好的标题。本质上我的问题是:

对于任意整数=ARRAYFORMULA(SUBSTITUTE(SUBSTITUTE(QUERY(TRANSPOSE(QUERY(TRANSPOSE( SUBSTITUTE(A1:C, " ", "♦")), , 999^99))), " ", ""), "♦", " ")) (n, 1, h, w)n,我有一个形状为h的pytorch张量(在我的特定情况下,该数组代表一批尺寸为1的灰度图像w

我还有另一个张量为h x w的张量,该张量将第一个数组中的每个可能值(即第一个数组可以包含从(m, 2)0的值)映射到某个值元组。我想将此映射“应用”到第一个数组,以便获得形状为m - 1的数组。

我希望这很清楚,我很难用语言来表达,这是一个代码示例(但是请注意,由于涉及到四个维度数组,因此也不是很直观):

(n, 2, h, w)

如何高效地执行此转换? (理想情况下,不使用任何其他内存)。在numpy中,可以使用import torch m = 18 # could also be arbitrary tensor with this shape with values between 0 and m - 1 a = torch.arange(m).reshape(2, 1, 3, 3) # could also be arbitrary tensor with this shape b = torch.LongTensor( [[11, 17, 9, 6, 5, 4, 2, 10, 3, 13, 14, 12, 7, 1, 15, 16, 8, 0], [11, 8, 4, 14, 13, 12, 16, 1, 5, 17, 0, 10, 7, 15, 9, 6, 2, 3]]).t() # I probably have to do this and the permute/reshape, but how? c = b.index_select(0, a.flatten()) # ... # another approach that I think works (but I'm not really sure why, I found this # more or less by trial and error). I would ideally like to find a 'nicer' way # of doing this c = torch.stack([ b.index_select(0, a_.flatten()).reshape(3, 3, 2).permute(2, 0, 1) for a_ in a ]) # the end result should be: #[[[[11, 17, 9], # [ 6, 5, 4], # [ 2, 10, 3]], # # [[11, 8, 4], # [14, 13, 12], # [16, 1, 5]]], # # # [[[13, 14, 12], # [ 7, 1, 15], # [16, 8, 0]], # # [[17, 0, 10], # [ 7, 15, 9], # [ 6, 2, 3]]]] 轻松实现,但是似乎没有与之相对应的pytorch。

1 个答案:

答案 0 :(得分:1)

这是使用切片,堆叠和基于视图的重塑的一种方法:

In [239]: half_way = b.shape[0]//2

In [240]: upper_half = torch.stack((b[:half_way, :][:, 0], b[:half_way, :][:, 1]), dim=0).view(-1, 3, 3)
In [241]: lower_half = torch.stack((b[half_way:, :][:, 0], b[half_way:, :][:, 1]), dim=0).view(-1, 3, 3)

In [242]: torch.stack((upper_half, lower_half))
Out[242]: 
tensor([[[[11, 17,  9],
          [ 6,  5,  4],
          [ 2, 10,  3]],

         [[11,  8,  4],
          [14, 13, 12],
          [16,  1,  5]]],


        [[[13, 14, 12],
          [ 7,  1, 15],
          [16,  8,  0]],

         [[17,  0, 10],
          [ 7, 15,  9],
          [ 6,  2,  3]]]])

一些警告是,这仅适用于n=2。但是,这比基于循环的方法快1.7倍,但涉及更多代码。


这是一种更通用的方法,它可以缩放为任意正整数n

In [327]: %%timeit
     ...: block_size = b.shape[0]//a.shape[0]
     ...: seq_of_tensors = [b[block_size*idx:block_size*(idx+1), :].permute(1, 0).flatten().reshape(2, 3, 3).unsqueeze(0)  for idx in range(a.shape[0])]
     ...: torch.cat(seq_of_tensors)
     ...: 
23.5 µs ± 460 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

您也可以使用view代替整形:

block_size = b.shape[0]//a.shape[0]
seq_of_tensors = [b[block_size*idx:block_size*(idx+1), :].permute(1, 0).flatten().view(2, 3, 3).unsqueeze(0)  for idx in range(a.shape[0])]
torch.cat(seq_of_tensors)
# outputs
tensor([[[[11, 17,  9],
          [ 6,  5,  4],
          [ 2, 10,  3]],

         [[11,  8,  4],
          [14, 13, 12],
          [16,  1,  5]]],


        [[[13, 14, 12],
          [ 7,  1, 15],
          [16,  8,  0]],

         [[17,  0, 10],
          [ 7, 15,  9],
          [ 6,  2,  3]]]])

注意:请注意,由于我们必须将张量b均匀划分以进行置换,展平,重塑,解压,然后连接/堆叠,因此我仍然使用列表理解尺寸0。它仍然比我上面的解决方案快一点。