【问题标题】:Scatter tensor in pytorch along the rows沿行在pytorch中散布张量
【发布时间】:2020-10-02 15:13:51
【问题描述】:

我想以行的粒度分散张量。

例如考虑,

Input = torch.tensor([[2, 3], [3, 4], [4, 5]])

我要散场

S = torch.tensor([[1,2],[1,2]])

到索引

I = torch.tensor([0,2])

我希望输出为torch.tensor([[1, 2], [3, 4], [1, 2]])

这里S[0]分散到Input[I[0]],同样S[1]分散到Input[I[1]]

我怎样才能做到这一点?我不是在循环S 中的行,而是在寻找一种更有效的方法。

【问题讨论】:

    标签: python tensorflow pytorch tensor scatter


    【解决方案1】:

    input[I] = S

    例子:

    input = torch.tensor([[2, 3], [3, 4], [4, 5]])
    S = torch.tensor([[1,2],[1,2]])
    I = torch.tensor([0,2])
    
    input[I] = S
    input
    tensor([[1, 2],
            [3, 4],
            [1, 2]])
    

    【讨论】:

      【解决方案2】:

      回答可能有点晚,但无论如何,你可以这样做:

      import torch
      
      inp = torch.tensor([[2,3], [3,4], [4,5]])
      src = torch.tensor([[1,2], [1,2]])
      idxs = torch.tensor([[0,0],[2,2]])
      
      y = torch.scatter(inp, 0, idxs, src)
      

      【讨论】:

        猜你喜欢
        • 2019-08-05
        • 2020-09-24
        • 2021-06-10
        • 2021-12-01
        • 2018-02-24
        • 1970-01-01
        • 2019-07-22
        • 2021-02-07
        • 2021-06-19
        相关资源
        最近更新 更多