想象一下以下场景:
data = torch.Tensor([0.5,0.4,1.2,1.1,0.4,0.4])
indices = torch.Tensor([0,1,1,2,2,2])我想要实现的是:通过data中的样本子集计算indices索引的平均值
subset_means == torch.Tensor([0.5, 0.8, 0.8, 0.63, 0.63, 0.63])到目前为止,我还没有想出令人满意的解决办法。
发布于 2022-01-25 10:37:35
可以使用Tensor.index_put根据某些索引数组累积数组的值。通过这种方式,您可以将属于同一索引的所有值进行汇总。在下面的片段中,我使用了一个单独的调用,其中包含了一个仅包含一个数组的调用,用于计算每个索引的出现数,从而能够从和中计算出平均值:
import torch
data = torch.tensor([0.5,0.4,1.2,1.1,0.4,0.4])
indices = torch.tensor([0,1,1,2,2,2]).to(torch.long)
# sum groups according to indices
accum = torch.zeros((indices.max()+1, )).index_put((indices,), data, accumulate=True)
# count groups according to indices
cnt = torch.zeros((indices.max()+1,)).index_put((indices,), torch.ones((1,)), accumulate=True)
# compute means and expand according to indices
subset_means = (accum / cnt)[indices]
print(subset_means)
#subset_means == torch.Tensor([0.5, 0.8, 0.8, 0.63, 0.63, 0.63])https://stackoverflow.com/questions/70845096
复制相似问题