问题描述
我正试图了解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,torch.tensor(1.))
它返回
tensor([[0.,0.])
我想知道index_put
在世界上的规则是什么?如果我想将三个值放到a上以使其返回
tensor([0.,],0.])
感谢您的帮助!
解决方法
我在这里复制了示例,插入了参数名称,方括号和正确的输出(已交换您的输出):
a.index_put(indices=[torch.tensor([1,0]),torch.tensor([1,1])],values=torch.tensor(1.))
tensor([[0.,1.,0.],[0.,0.]])
a.index_put(indices=[torch.tensor([0,torch.tensor([0,values = torch.tensor(1.))
tensor([[1.,0.,0.]]
此方法的作用是将值插入由a
指示的原始indices
张量中的位置。 index是插入的x坐标和插入的y坐标的列表。值可以是单个值或一维张量。
要获得所需的输出,请使用:
a.index_put(indices=[torch.tensor([0,1]),2,1.],0.]])
此外,您可以在values
参数中传递多个值以将它们插入到指示的位置:
a.index_put(indices=[torch.tensor([0,values=torch.tensor([1.,2.,3.]))
tensor([[0.,2.],3.,0.]])