torch.Tensor.scatter_reduce_¶
- Tensor.scatter_reduce_(dim, index, src, reduce, *, include_self=True) Tensor¶
使用
reduce参数 ("sum","prod","mean","amax","amin") 定义的应用的缩减操作,将src张量中的所有值缩减到index张量中指定的索引,在self张量中。对于src中的每个值,它将被缩减到self中的一个索引,该索引由其在src中的索引指定,用于dimension != dim,以及由index中的对应值指定,用于dimension = dim。如果include_self="True",则self张量中的值将包含在缩减中。self、index和src应该都具有相同数量的维度。还需要index.size(d) <= src.size(d)对于所有维度d,并且index.size(d) <= self.size(d)对于所有维度d != dim。请注意,index和src不会广播。对于具有
reduce="sum"和include_self=True的 3 维张量,输出将给出为self[index[i][j][k]][j][k] += src[i][j][k] # if dim == 0 self[i][index[i][j][k]][k] += src[i][j][k] # if dim == 1 self[i][j][index[i][j][k]] += src[i][j][k] # if dim == 2
注意
当在 CUDA 设备上给出张量时,此操作可能表现出不确定性。有关详细信息,请参阅 可重复性。
注意
仅针对
src.shape == index.shape实现反向传播。警告
此功能处于测试阶段,可能在不久的将来发生变化。
- 参数
示例
>>> src = torch.tensor([1., 2., 3., 4., 5., 6.]) >>> index = torch.tensor([0, 1, 0, 1, 2, 1]) >>> input = torch.tensor([1., 2., 3., 4.]) >>> input.scatter_reduce(0, index, src, reduce="sum") tensor([5., 14., 8., 4.]) >>> input.scatter_reduce(0, index, src, reduce="sum", include_self=False) tensor([4., 12., 5., 4.]) >>> input2 = torch.tensor([5., 4., 3., 2.]) >>> input2.scatter_reduce(0, index, src, reduce="amax") tensor([5., 6., 5., 2.]) >>> input2.scatter_reduce(0, index, src, reduce="amax", include_self=False) tensor([3., 6., 5., 2.])