沿行在 pytorch 中散布张量
Scatter tensor in pytorch along the rows
我想以行的粒度散布张量。
例如考虑,
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
中的行。
做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]])
回答可能有点晚,但无论如何,你可以这样做:
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)
我想以行的粒度散布张量。
例如考虑,
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
中的行。
做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]])
回答可能有点晚,但无论如何,你可以这样做:
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)