假设我有一个二维张量,看起来像这样:
[[44, 50, 1, 32],
.
.
.
[7, 13, 90, 83]]
以及我要选择的行索引列表,看起来像这样的[0, 34, 100, ..., 745]
。如何遍历并创建一个仅包含其索引包含在数组中的行的新张量?
答案 0 :(得分:3)
您可以选择numpy之类的
import torch
x = torch.Tensor([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 8, 7, 6],
[5, 4, 2, 1]])
indices = [0, 3]
print(x[indices])
# tensor([[1., 2., 3., 4.],
# [5., 4., 2., 1.]])