I stumbled across a strange phenomenon today when debugging some PyTorch code:
I created a tensor like this
import torch
mat = torch.rand(3,5)
and then indexed it using a range like:
print(mat[2,4:7])
Lo and behold, it returned the element at mat[2,4]
alone!, i.e. mat[2,4:6] == mat[2,4]
. There was no indication that my range extended beyond the bounds of the tensor. However, running:
print(mat[2,5])
raised the expected IndexError
: *** IndexError: index 5 is out of bounds for dimension 1 with size 5
Thinking I had stumbled across a PyTorch bug perhaps, I tested it on numpy
arrays as well and found the same behavior to hold:
import numpy as np
mat = np.random.rand(3,5)
print(mat[2,4:7]) # prints mat[2,4]
print(mat[2,5]) # raises IndexError (*** IndexError: index 5 is out of bounds for axis 1 with size 5)
I would have expected this range indexing to at the very least raise a warning or perhaps even to loop around modulo the shape of the dimension so that it returns an array of the expected output shape. Frankly, I'm surprised it doesn't raise an IndexError
altogether.
Why is the actual behavior to be expected?
(Note: I imagine it is based on a Numpy convention first)