捷徑

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 張量中的值也會被包含在縮減中。

selfindexsrc 應具有相同的維度數。 也需要對於所有維度 dindex.size(d) <= src.size(d),並且對於所有維度 d != dimindex.size(d) <= self.size(d)。請注意,indexsrc 不會進行廣播 (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 階段,在不久的將來可能會發生變化。

參數
  • dim (int) – 沿其進行索引的軸

  • index (LongTensor) – 要分散和縮減的元素的索引。

  • src (Tensor) – 要分散和縮減的來源元素

  • reduce (str) – 應用於非唯一索引的縮減運算 ("sum""prod""mean""amax""amin")

  • include_self (bool) – 是否將 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.])

文件

取得 PyTorch 的完整開發人員文件

檢視文件

教學課程

取得適合初學者和進階開發人員的深入教學課程

檢視教學課程

資源

尋找開發資源並獲得您的問題解答

檢視資源