我可以使用逻辑索引或索引列表来分割张量吗?

23
我尝试使用列上的逻辑索引切片PyTorch张量。我想要与索引向量中的1值对应的列。切片和逻辑索引都是可能的,但它们可以一起使用吗?如果可以,如何操作?我的尝试一直报错:
TypeError: indexing a tensor with an object of type ByteTensor. The only supported types are integers, slices, numpy scalars and torch.LongTensor or torch.ByteTensor as the only argument.

MCVE

期望输出
import torch

C = torch.LongTensor([[1, 3], [4, 6]])
# 1 3
# 4 6

仅针对列进行逻辑索引:

A_log = torch.ByteTensor([1, 0, 1]) # the logical index
B = torch.LongTensor([[1, 2, 3], [4, 5, 6]])
C = B[:, A_log] # Throws error

如果向量大小相同,则逻辑索引起作用:
B_truncated = torch.LongTensor([1, 2, 3])
C = B_truncated[A_log]

我只需要通过重复逻辑索引来获得所需的结果,使它与我正在索引的张量具有相同的大小,但这样我还必须重新塑造输出。

C = B[A_log.repeat(2, 1)] # [torch.LongTensor of size 4]
C = C.resize_(2, 2)

我还尝试使用索引列表

A_idx = torch.LongTensor([0, 2]) # the index vector
C = B[:, A_idx] # Throws error

如果我想要连续范围的索引,切片就可以实现:

C = B[:, 1:2]
3个回答

28

我认为这可以通过index_select函数来实现,你可以尝试一下

import torch

A_idx = torch.LongTensor([0, 2]) # the index vector
B = torch.LongTensor([[1, 2, 3], [4, 5, 6]])
C = B.index_select(1, A_idx)
# 1 3
# 4 6

6

在 PyTorch 1.5.0 中,用作索引的张量必须是长整型(long)、字节型(byte)或布尔型(bool)张量。

下面是一个以长整型张量形式表示的索引。

import torch

B = torch.LongTensor([[1, 2, 3], [4, 5, 6]])
idx1 = torch.LongTensor([0, 2])

B[:, idx1]
# tensor([[1, 3],
#         [4, 6]])

这里是一个布尔张量(逻辑索引):

idx2 = torch.BoolTensor([True, False, True]) 

B[:, idx2]
# tensor([[1, 3],
#         [4, 6]])

1
很好,现在这更加简单了。这个变化是在1.5之前还是之后发生的? - Cecilia

2
我尝试了这段代码,并在旁边写下了结果的注释。
import torch

arr = torch.tensor([[0,1,2],[3,4,5]])
arr = torch.arange(6).reshape((2,3))
print(arr)
#   tensor([[0, 1, 2],
#   [3, 4, 5]])

print(arr[1]) # tensor([3, 4, 5])
print(arr[1,1]) # tensor(4)
print(arr[1, :]) # tensor([3, 4, 5])
#print(arr[1,1,1]) #    IndexError: too many indices for tensor of dimension 2
print(arr[1, [0,1]]) # tensor([3, 4])
print(arr[[0, 1],0]) # tensor([0, 3])

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接