Python 沿行在pytorch中散布张量

Python 沿行在pytorch中散布张量,python,tensorflow,pytorch,tensor,scatter,Python,Tensorflow,Pytorch,Tensor,Scatter,我想把张量分散成几行 例如,考虑< /P> 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]分散到输入[I[0]],同样S[1]分散到Input[I[1]] 我怎样才能做到这一点?我正在寻找一种更有效的方法,而不是在S中的行上循

我想把张量分散成几行

例如,考虑< /P>

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]
分散到输入
[I[0]]
,同样
S[1]
分散到
Input[I[1]]


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

Do
input[I]=S

例如:

input = torch.tensor([[2, 3], [3, 4], [4, 5]])
S = torch.tensor([[1,2],[1,2]])
I = torch.tensor([0,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)
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)