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.])