快捷方式

torch.Tensor.scatter_reduce_

Tensor.scatter_reduce_(dim, index, src, reduce, *, include_self=True) Tensor

使用透過 reduce 引數定義的歸約操作("sum""prod""mean""amax""amin"),將所有值從 src 張量歸約到 index 張量中指定的 self 張量索引處。對於 src 中的每個值,它被歸約到 self 中的一個索引處,該索引由其在 src 中對於 dimension != dim 的索引指定,以及由 index 中對於 dimension = dim 的對應值指定。如果 include_self="True",則 self 張量中的值也包含在歸約中。

selfindexsrc 都應該具有相同的維度數。此外,要求對於所有維度 dindex.size(d) <= src.size(d);對於所有維度 d != dim,要求 index.size(d) <= self.size(d)。注意 indexsrc 不支援廣播。

對於一個 3-D 張量,當 reduce="sum"include_self=True 時,輸出如下所示:

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 全面的開發者文件

檢視文件

教程

獲取面向初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並獲得解答

檢視資源