捷徑

torch.Tensor.scatter_

Tensor.scatter_(dim, index, src, *, reduce=None) Tensor

將張量 src 中的所有值,根據 index 張量指定的索引位置,寫入到 self 中。對於 src 中的每個值,其輸出索引由它在 src 中的索引決定,當 dimension != dim 時;而當 dimension = dim 時,則由 index 中的對應值決定。

對於一個 3 維張量,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

這是 gather() 中描述方式的反向操作。

selfindexsrc (如果它是 Tensor) 應該具有相同的維度數。 此外,對於所有維度 d,必須滿足 index.size(d) <= src.size(d),且對於所有維度 d != dim,必須滿足 index.size(d) <= self.size(d)。 請注意,indexsrc 不進行廣播 (broadcast)。

此外,與 gather() 類似,index 的值必須介於 0self.size(dim) - 1 (包含) 之間。

警告

當索引不是唯一時,行為是不確定的(將任意選擇 src 中的其中一個值),並且梯度將是不正確的(它將傳播到源中對應於相同索引的所有位置)!

注意

反向傳播僅針對 src.shape == index.shape 實作。

另外,接受一個可選的 reduce 參數,允許指定一個可選的歸約運算,該運算應用於張量 src 中的所有值,寫入到 self 中,位置由 index 指定。 對於 src 中的每個值,歸約運算應用到 self 中的一個索引,該索引由其在 src 中的索引決定,如果 dimension != dim,索引就是 src 中的索引。如果 dimension = dim,索引就是 index 中的對應值。

給定一個 3 維張量,並使用乘法運算進行歸約,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

使用加法運算進行歸約與使用 scatter_add_() 相同。

警告

帶有 Tensor src 的 reduce 參數已被棄用,將在未來的 PyTorch 版本中移除。 請改用 scatter_reduce_() 以獲得更多歸約選項。

參數
  • dim (int) – 要沿其索引的軸。

  • index (LongTensor) – 要分散 (scatter) 元素的索引,可以是空的,也可以與 src 具有相同的維度。 為空時,該操作返回未更改的 self

  • src (Tensor) – 要分散的來源元素。

關鍵字參數

reduce (str, optional) – 要應用的歸約運算,可以是 'add''multiply'

範例

>>> src = torch.arange(1, 11).reshape((2, 5))
>>> src
tensor([[ 1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10]])
>>> index = torch.tensor([[0, 1, 2, 0]])
>>> torch.zeros(3, 5, dtype=src.dtype).scatter_(0, index, src)
tensor([[1, 0, 0, 4, 0],
        [0, 2, 0, 0, 0],
        [0, 0, 3, 0, 0]])
>>> index = torch.tensor([[0, 1, 2], [0, 1, 4]])
>>> torch.zeros(3, 5, dtype=src.dtype).scatter_(1, index, src)
tensor([[1, 2, 3, 0, 0],
        [6, 7, 0, 0, 8],
        [0, 0, 0, 0, 0]])

>>> torch.full((2, 4), 2.).scatter_(1, torch.tensor([[2], [3]]),
...            1.23, reduce='multiply')
tensor([[2.0000, 2.0000, 2.4600, 2.0000],
        [2.0000, 2.0000, 2.0000, 2.4600]])
>>> torch.full((2, 4), 2.).scatter_(1, torch.tensor([[2], [3]]),
...            1.23, reduce='add')
tensor([[2.0000, 2.0000, 3.2300, 2.0000],
        [2.0000, 2.0000, 2.0000, 3.2300]])
scatter_(dim, index, value, *, reduce=None) Tensor:

value 中的值,根據 index 張量指定的索引位置,寫入到 self 中。 此操作等同於先前的版本,只是 src 張量完全填充了 value

參數
  • dim (int) – 要沿其索引的軸。

  • index (LongTensor) – 要分散 (scatter) 元素的索引,可以是空的,也可以與 src 具有相同的維度。 為空時,該操作返回未更改的 self

  • value (Scalar) – 要分散的值。

關鍵字參數

reduce (str, optional) – 要應用的歸約運算,可以是 'add''multiply'

範例

>>> index = torch.tensor([[0, 1]])
>>> value = 2
>>> torch.zeros(3, 5).scatter_(0, index, value)
tensor([[2., 0., 0., 0., 0.],
        [0., 2., 0., 0., 0.],
        [0., 0., 0., 0., 0.]])

文件

取得 PyTorch 的完整開發者文件

檢視文件

教學

取得初學者和進階開發者的深入教學

檢視教學

資源

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

檢視資源