torch.Tensor.scatter_reduce_
- Tensor.scatter_reduce_(dim, index, src, reduce, *, include_self=True) → Tensor
-
使用通过
reduce参数定义的减少操作(如"sum"、"prod"、"mean"、"amax"和"amin"),将src张量中的所有值减少到self张量中由index张量指定的索引。对于src中的每个值,它被减少为在dimension != dim时由其在src中的索引,在dimension = dim时则由index中对应的值指定的self张量中的一个索引。如果include_self="True",则在减少操作中包含self张量中的值。self、index和src应该具有相同的维度数量。此外,对于所有维度d,需要满足index.size(d) <= src.size(d),并且对于所有不等于dim的维度d,需满足index.size(d) <= self.size(d)。请注意,index和src不进行广播。对于一个具有
reduce="sum"和include_self=True属性的3-D张量,其输出为: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.])