mindspore.Tensor.index_fill_

查看源文件
mindspore.Tensor.index_fill_(dim, index, value) Tensor[源代码]

index 中给定的顺序选择索引,将输入 value 的值填充到 self Tensor的所有 dim 维元素。

警告

这是一个实验性API,后续可能修改或删除。

说明

计算 value 的梯度时, index 的值必须在 [0, self.shape[dim]) 范围内,如果超出该范围,结果未定义。

参数:
  • dim (int) - 填充 self Tensor的维度。

  • index (Tensor) - 填充 self Tensor的索引。 index 必须是一个0D或1D Tensor,数据类型为int32或int64。

  • value (Union[Tensor, Number, bool]) - 填充 self Tensor的值。 value 为数值型、bool,或者数据类型为数值型或bool的Tensor。如果 value 是Tensor时,必须为0D Tensor。

返回:

Tensor,shape与 self 的shape相同,数据类型和 self 的数据类型相同。

异常:
  • TypeError - 如果 index 的数据类型不是int32或int64。

  • RuntimeError - 如果 dim 不在 \([-self.ndim, self.ndim)\) 范围内。

  • RuntimeError - 如果 index 的维数大于1。

  • RuntimeError - 如果 value 是一个Tensor时,其维数不为0。

支持平台:

Ascend

样例:

>>> import mindspore
>>> from mindspore import Tensor
>>> import numpy as np
>>> x = Tensor(np.array([[1, 2, 3], [4, 5, 6]]), mindspore.int32)
>>> dim = 1
>>> index = Tensor(np.array([0, 2]), mindspore.int32)
>>> value = Tensor(0, mindspore.int32)
>>> output = x.index_fill_(dim, index, value)
>>> print(output)
[[ 0  2  0]
 [ 0  5  0]]
>>> print(x)
[[ 0  2  0]
 [ 0  5  0]]