快捷方式

torch.Tensor.scatter_add_

Tensor.scatter_add_(dim, index, src) Tensor

在 `index` 張量指定的索引處,將來自張量 src 的所有值加到 self 中,方式類似於 scatter_()。對於 src 中的每個值,它被新增到 self 中由其在 srcdimension != dim 的索引以及在 indexdimension = dim 的對應值指定的索引位置。

對於一個 3-D 張量,self 的更新方式為

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

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

注意

當在 CUDA 裝置上給定張量時,此操作可能表現出不確定性。有關更多資訊,請參閱可復現性

注意

反向傳播僅在 src.shape == index.shape 時實現。

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

  • index (LongTensor) – 要分散並相加的元素的索引,可以是空張量或與 src 具有相同維度的張量。當為空時,此操作返回未更改的 self

  • src (Tensor) – 要分散並相加的源元素

示例

>>> src = torch.ones((2, 5))
>>> index = torch.tensor([[0, 1, 2, 0, 0]])
>>> torch.zeros(3, 5, dtype=src.dtype).scatter_add_(0, index, src)
tensor([[1., 0., 0., 1., 1.],
        [0., 1., 0., 0., 0.],
        [0., 0., 1., 0., 0.]])
>>> index = torch.tensor([[0, 1, 2, 0, 0], [0, 1, 2, 2, 2]])
>>> torch.zeros(3, 5, dtype=src.dtype).scatter_add_(0, index, src)
tensor([[2., 0., 0., 1., 1.],
        [0., 2., 0., 0., 0.],
        [0., 0., 2., 1., 1.]])

文件

查閱全面的 PyTorch 開發者文件

檢視文件

教程

獲取針對初學者和高階開發者的深入教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源