如何通过访问pytorch中2 X 2张量给定索引处的特定值来创建张量?

如何通过访问pytorch中2 X 2张量给定索引处的特定值来创建张量?,pytorch,tensor,Pytorch,Tensor,假设 mat=torch.rand((5,7))我想通过传递索引,比如说idxs=[0,4,2,3,6]从第一维度(这里,7)获取值。我现在能够做的方法是做mat[[0,1,2,3,4],idxs]。我希望mat[:,idxs]能工作,但没有。第一个选项是唯一的方法还是有更好的方法?是您想要的: torch.gather(mat,1,torch.tensor(idxs)[:,None])

假设
mat=torch.rand((5,7))
我想通过传递索引,比如说
idxs=[0,4,2,3,6]
从第一维度(这里,7)获取值。我现在能够做的方法是做
mat[[0,1,2,3,4],idxs]
。我希望
mat[:,idxs]
能工作,但没有。第一个选项是唯一的方法还是有更好的方法?

是您想要的:

torch.gather(mat,1,torch.tensor(idxs)[:,None])