Python 火炬聚集三维张量,二维张量为;“收集地图”;

Python 火炬聚集三维张量,二维张量为;“收集地图”;,python,pytorch,Python,Pytorch,对于给定的维度张量data.shape[B,N,F]和index.shape[N,K], 如果K是每个点N的“邻域”的索引,是否有一种简单的方法来收集邻域,以便output.shape[B,N,K,F] data = [[[1,2,3],[2,3,4],[3,4,5]],[[3,4,5],[6,7,8],[2,3,4]] # Shape 2,3,3 indices = [[1,2],[1,0],[0,0]] # Shape 3,2 output = [ [[[2,3,4],[

对于给定的维度张量
data.shape[B,N,F]
index.shape[N,K]

如果
K
是每个点N的“邻域”的索引,
是否有一种简单的方法来收集邻域,以便
output.shape[B,N,K,F]

data = [[[1,2,3],[2,3,4],[3,4,5]],[[3,4,5],[6,7,8],[2,3,4]] # Shape 2,3,3
indices = [[1,2],[1,0],[0,0]] # Shape 3,2

output = [
         [[[2,3,4],[3,4,5]],[[2,3,4],[1,2,3]],[[1,2,3],[1,2,3]]],
         [[[6,7,8],[2,3,4]],[[6,7,8],[3,4,5]],[[3,4,5],[3,4,5]]]
         ]
例如,每个批次中的第一个点与点
[1,2]
“相关”,因此
输出[0][0]=[2,3,4],[3,4,5]
,它们是输入的第一批次中的点
1,2

我的尝试:

batched_indices = indices.unsqueeze(0).unsqueeze(-1).repeat(batch_size,1,1,feature_size)
data_neighs = data.unsqueeze(2).repeat(1,1,num_neighs,1)
output = torch.gather(data_neighs,batched_indices,dim=1)

为了确保正确理解,您能否展示您将如何使用基于循环的天真
方法?为了确保正确理解,您能否展示您将如何使用基于
循环的天真
方法?