Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I slice tensors with logical indexing or lists of indices?

I'm trying to slice a PyTorch tensor using a logical index on the columns. I want the columns that correspond to a 1 value in the index vector. Both slicing and logical indexing are possible, but are they possible together? If so, how? My attempt keeps throwing the unhelpful error

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

Desired Output

import torch

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

Logical indexing on the columns only:

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

If the vectors are the same size, logical indexing works:

B_truncated = torch.LongTensor([1, 2, 3])
C = B_truncated[A_log]

And I can get the desired result by repeating the logical index so that it has the same size as the tensor I am indexing, but then I also have to reshape the output.

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

I also tried using a list of indices:

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

If I want contiguous ranges of indices, slicing works:

C = B[:, 1:2]
like image 991
Cecilia Avatar asked May 15 '17 21:05

Cecilia


People also ask

Can you slice tensors?

You can use tf. slice on higher dimensional tensors as well. You can also use tf. strided_slice to extract slices of tensors by 'striding' over the tensor dimensions.

Can you index tensors?

Single element indexing for a 1-D tensors works mostly as expected. Like R, it is 1-based. Unlike R though, it accepts negative indices for indexing from the end of the array. (In R, negative indices are used to remove elements.)

How do you slice tensors in PyTorch?

Slicing a 3D Tensor Slicing: Slicing means selecting the elements present in the tensor by using “:” slice operator. We can slice the elements by using the index of that particular element. Parameters: tensor_position_start: Specifies the Tensor to start iterating.


2 Answers

I think this is implemented as the index_select function, you can try

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
like image 54
dontloo Avatar answered Oct 01 '22 01:10

dontloo


In PyTorch 1.5.0, tensors used as indices must be long, byte or bool tensors.

The following is an index as a tensor of longs.

import torch

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

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

And here's a tensor of bools (logical indexing):

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

B[:, idx2]
# tensor([[1, 3],
#         [4, 6]])
like image 42
user650654 Avatar answered Oct 01 '22 00:10

user650654