我想以行的粒度分散张量。
例如考虑,
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]
被分散到输入[I[1]]
我怎样才能实现这个目标?我正在寻找一种更有效的方法,而不是循环遍历 S
中的行。
最佳答案
执行输入[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]])
关于python - 在pytorch中沿行散布张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62350436/