Which function is invoked when do slicing on Tensor?

When two tensors are added together, for example,

c = a + b

the function tensor.__add__ is invoked to really “add” two tensors.

When a tensor is sliced on a given axis, which function is invoked?

# slice on axis=1
b = a[:, 1:2]

I searched for *slice on the pytorch reference documentation but found nothing.

Hi,

The __getitem__ method of that class is called.
If you’re interested of how pytorch implements this, it is routed to this function.

1 Like