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.
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]
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.
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.)
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.
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
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]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With