gpt4 book ai didi

numpy - 在pytorch中将索引选定张量添加到另一个具有重叠索引的张量

转载 作者:行者123 更新时间:2023-12-03 16:47:06 24 4
gpt4 key购买 nike

这是对 this question 的后续问题.我想在 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 .

最佳答案

您要找的是 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]])

关于numpy - 在pytorch中将索引选定张量添加到另一个具有重叠索引的张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65584330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com