【问题标题】:Torch - repeat tensor like numpy repeatTorch - 像 numpy repeat 一样重复张量
【发布时间】:2016-05-15 14:40:50
【问题描述】:

我试图以两种方式重复 Torch 中的张量。例如重复张量{1,2,3,4} 3 次两种方式产生;

{1,2,3,4,1,2,3,4,1,2,3,4}
{1,1,1,2,2,2,3,3,3,4,4,4}

有一个内置的 torch:repeatTensor 函数将生成两者中的第一个(如numpy.tile()),但我找不到后者的一个(如numpy.repeat())。我确信我可以在第一个上调用 sort 来给出第二个,但我认为这对于较大的数组来说可能计算成本很高?

谢谢。

【问题讨论】:

标签: lua torch


【解决方案1】:

试试 torch.repeat_interleave() 方法:https://pytorch.org/docs/stable/torch.html#torch.repeat_interleave

>>> x = torch.tensor([1, 2, 3])
>>> x.repeat_interleave(2)
tensor([1, 1, 2, 2, 3, 3])

【讨论】:

  • 这应该是合适的答案!
【解决方案2】:

引用https://discuss.pytorch.org/t/how-to-tile-a-tensor/13853 -

z = torch.FloatTensor([[1,2,3],[4,5,6],[7,8,9]])
1 2 3
4 5 6
7 8 9
z.transpose(0,1).repeat(1,3).view(-1, 3).transpose(0,1)
1 1 1 2 2 2 3 3 3
4 4 4 5 5 5 6 6 6
7 7 7 8 8 8 9 9 9

这将使您直观地了解它的工作原理。

【讨论】:

    【解决方案3】:
    a = torch.Tensor([1,2,3,4])
    

    为了得到[1., 2., 3., 4., 1., 2., 3., 4., 1., 2., 3., 4.],我们在第一维重复三次张量:

    a.repeat(3)
    

    为了得到[1,1,1,2,2,2,3,3,3,4,4,4],我们向张量添加一个维度,并在第二个维度重复三次,得到一个4 x 3张量,我们可以将其展平。

    b = a.reshape(4,1).repeat(1,3).flatten()
    

    b = a.reshape(4,1).repeat(1,3).view(-1)
    

    【讨论】:

    • 想解释一下你的答案吗?
    • @AndréSchild 我的错。现在好点了吗?
    • 你的答案不包含严重的python语法错误吗?例如a = torch.Tensor{1,2,3,4}a:repeatTensor(3)?我运行了它并得到了语法错误。
    • 这是 Lua 火炬 (torch.ch) 时代,而不是 Pytorch :)
    【解决方案4】:

    这是一个在张量中重复元素的通用函数。

    def repeat(tensor, dims):
        if len(dims) != len(tensor.shape):
            raise ValueError("The length of the second argument must equal the number of dimensions of the first.")
        for index, dim in enumerate(dims):
            repetition_vector = [1]*(len(dims)+1)
            repetition_vector[index+1] = dim
            new_tensor_shape = list(tensor.shape)
            new_tensor_shape[index] *= dim
            tensor = tensor.unsqueeze(index+1).repeat(repetition_vector).reshape(new_tensor_shape)
        return tensor
    

    如果你有

    foo = tensor([[1, 2],
                  [3, 4]])
    

    通过调用repeat(foo, [2,1]) 你得到

    tensor([[1, 2],
            [1, 2],
            [3, 4],
            [3, 4]])
    

    因此,您复制了维度 0 上的每个元素,并保留了维度 1 上的元素。

    【讨论】:

      【解决方案5】:

      使用 einops:

      from einops import repeat
      
      repeat(x, 'i -> (repeat i)', repeat=3)
      # like {1,2,3,4,1,2,3,4,1,2,3,4}
      
      repeat(x, 'i -> (i repeat)', repeat=3)
      # like {1,1,1,2,2,2,3,3,3,4,4,4}
      

      此代码适用于任何框架(numpy、torch、tf 等)

      【讨论】:

        【解决方案6】:

        你能试试类似的方法吗:

        import torch as pt
        
        #1 work as numpy tile
        
        b = pt.arange(10)
        print(b.repeat(3))
        
        #2 work as numpy tile
        
        b = pt.tensor(1).repeat(10).reshape(2,-1)
        print(b)
        
        #3 work as numpy repeat
        
        t = pt.tensor([1,2,3])
        t.repeat(2).reshape(2,-1).transpose(1,0).reshape(-1)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-12-28
          • 2016-03-03
          • 2021-11-10
          • 2017-09-11
          • 2016-03-21
          • 2016-01-19
          • 2021-02-02
          • 2016-07-02
          相关资源
          最近更新 更多