我正在尝试理解index_put
在PyTorch中的行为,但是文档对我来说不是很清楚。
给定的
a = torch.zeros(2, 3)
a.index_put([torch.tensor(1, 0), torch.tensor([1, 1])], torch.tensor(1.))
它返回
tensor([[1., 1., 0.],
[0., 0., 0.])
在给定的时候
a = torch.zeros(2, 3)
a.index_put([torch.tensor(0, 0), torch.tensor([1, 1])], torch.tensor(1.))
它返回
tensor([[0., 1., 0.],
[0., 0., 0.])
我想知道index_put
到底有什么规则?如果我想将三个值赋给a,那么它将返回
tensor([0., 1., 1.,],
[0., 1., 0.])
如有任何帮助,我们不胜感激!
发布于 2020-09-20 10:26:02
我在这里复制了你的例子,插入了参数名,固定了括号和正确的输出(你的被交换了):
a.index_put(indices=[torch.tensor([1, 0]), torch.tensor([1, 1])], values=torch.tensor(1.))
tensor([[0., 1., 0.],
[0., 1., 0.]])
a.index_put(indices=[torch.tensor([0, 0]), torch.tensor([0, 1])], values = torch.tensor(1.))
tensor([[1., 1., 0.],
[0., 0., 0.]]
此方法所做的是将值插入到由indices
指示的原始a
张量中的位置。索引是插入的x坐标和插入的y坐标的列表。值可以是单个值,也可以是一维张量。
要获得所需的输出,请使用以下命令:
a.index_put(indices=[torch.tensor([0,0,1]), torch.tensor([1, 2, 1])], values=torch.tensor(1.))
tensor([[0., 1., 1.],
[0., 1., 0.]])
此外,您可以在values
参数中传递多个值,以将它们插入到指定的位置:
a.index_put(indices=[torch.tensor([0,0,1]), torch.tensor([1, 2, 1])], values=torch.tensor([1., 2., 3.]))
tensor([[0., 1., 2.],
[0., 3., 0.]])
https://stackoverflow.com/questions/63977774
复制相似问题