pytorch에서 인덱스가 겹치는 다른 텐서에 인덱스 선택한 텐서를 추가

Jan 06 2021

이 질문에 대한 후속 질문 입니다. 나는 pytorch에서 똑같은 일을하고 싶습니다. 이것이 가능합니까? 그렇다면 어떻게?

import torch
image = torch.tensor([[246,  50, 101], [116,   1, 113], [187, 110,  64]])
iy = torch.tensor([[1, 0, 2], [1, 0, 2], [2, 2, 2]])
ix = torch.tensor([[0, 2, 1], [1, 2, 0], [0, 1, 2]])
warped_image = torch.zeros(size=image.shape)

다음과 같은 torch.add.at(warped_image, (iy, ix), image)출력이 필요 합니다.

[[  0.   0.  51.]
 [246. 116.   0.]
 [300. 211.  64.]]

참고 지표에서 (0,1)(1,1)포인트 같은 위치에 (0,2). 그래서 나는 warped_image[0,2] = image[0,1] + image[1,1] = 51.

답변

3 Ivan Jan 06 2021 at 01:23

당신이 찾고 torch.Tensor.index_put_있는 것은 accumulate인수 가 다음 과 같이 설정된 것입니다 True.

>>> warped_image = torch.zeros_like(image)

>>> warped_image.index_put_((iy, ix), image, accumulate=True)
tensor([[  0,   0,  51],
        [246, 116,   0],
        [300, 211,  64]])

또는 외부 버전 사용 torch.index_put:

>>> torch.index_put(torch.zeros_like(image), (iy, ix), image, accumulate=True)
tensor([[  0,   0,  51],
        [246, 116,   0],
        [300, 211,  64]])