評價此頁

torch.Tensor.index_fill_#

Tensor.index_fill_(dim, index, value) Tensor#

使用 index 中給出的索引順序,用 value 填充 self 張量中的元素。

引數
  • dim (int) – 沿哪個維度進行索引

  • index (LongTensor) – 要填充的 self 張量的索引

  • value (float) – 用於填充的值

示例

>>> x = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=torch.float)
>>> index = torch.tensor([0, 2])
>>> x.index_fill_(1, index, -1)
tensor([[-1.,  2., -1.],
        [-1.,  5., -1.],
        [-1.,  8., -1.]])