通过按index中给定的顺序 选择索引,用val值填充 自己(自张量)的元素。
dim(int)–索引所依据的维度
index(LongTensor)–要填充的自张量的索引
val(浮点数)–要填充的值
import torch
a = torch.randn(4, 3)
print(a)
# tensor([[-1.7189, 0.9798, -0.0428],
# [ 0.7184, -0.2824, -1.0289],
# [ 1.2858, 0.8423, -1.0473],
# [-0.0269, -0.9876, -2.3126]])
index = torch.tensor([0, 2])
b=a.index_fill(1, index, 9)#要填充1维
print(b)
# tensor([[ 9.0000, 0.9798, 9.0000],
# [ 9.0000, -0.2824, 9.0000],
# [ 9.0000, 0.8423, 9.0000],
# [ 9.0000, -0.9876, 9.0000]])
c=a.index_fill(0, index, 9)#要填充0维
print(c)
# tensor([[ 9.0000, 9.0000, 9.0000],
# [ 0.7184, -0.2824, -1.0289],
# [ 9.0000, 9.0000, 9.0000],
# [-0.0269, -0.9876, -2.3126]])