torch.Tensor.scatter_reduce_¶
- Tensor.scatter_reduce_(dim, index, src, reduce, *, include_self=True) Tensor ¶
将
src
张量中的所有值根据index
张量中的索引在self
张量中通过reduce
参数定义的减法操作进行归约。对于src
中的每个值,它被归约为一个索引self
,该索引由src
中的索引指定,并由dimension != dim
中的对应值指定index
。如果include_self="True"
,则self
张量中的值包含在归约中。self
、index
和src
的所有维度都应该相同。还要求对于所有维度d
,index.size(d) <= src.size(d)
都应相同,对于所有维度d != dim
,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
的反向传播。警告
此函数处于测试阶段,未来可能会发生变化。
- 参数:
dim(整数)- 指索引的轴
索引(LongTensor)- 要分散和减少的元素的索引。
src(Tensor)- 要分散和减少的源元素。
reduce(字符串)- 对于非唯一索引(
"sum"
、"prod"
、"mean"
、"amax"
、"amin"
)要应用的减少操作。include_self(布尔值)- 是否将
self
张量中的元素包含在缩减中
示例:
>>> 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.])