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
中的每個值,它會被縮減到self
中的一個索引,該索引由其在src
中的索引指定,當dimension != dim
時,以及由index
中的相應值指定,當dimension = dim
時。如果include_self="True"
,則self
張量中的值也會被包含在縮減中。self
、index
和src
應具有相同的維度數。 也需要對於所有維度d
,index.size(d) <= src.size(d)
,並且對於所有維度d != dim
,index.size(d) <= self.size(d)
。請注意,index
和src
不會進行廣播 (broadcast)。對於具有
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
實作反向傳播。警告
此函數處於 beta 階段,在不久的將來可能會發生變化。
- 參數
範例
>>> 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.])