Pytorch - Indexing a range of multiple Indices?
Lets say I have a tensor of size [100, 100] and I have a set of start_indices and end_indices of size [100]

I want to be able to do something like this:

tensor[start_indices:end_indices, :] = 0

Unfortunately, I get an error saying 

TypeError: only integer tensors of a single element can be converted to an index

So is this actually possible without a for loop?

To the best of my knowledge this is not possible without some sort of loop or list comprehension.

Below are some alternatives which may be useful depending on your use-case. Specifically if you are looking to reuse the same start_indices and end_indices for multiple assignments, or if you are looking have only one in-place assignment to tensor then the solutions below would be useful.



If instead of start_indices and end_indices you were given a list of indices, for example

row_indices = torch.cat([torch.arange(s, e, dtype=torch.int64) for s, e in zip(start_indices, end_indices)])


Then this would be possible using

tensor[row_indices, :] = 0




Or if you were given a mask

mask = torch.zeros(tensor.shape, dtype=torch.bool, device=tensor.device)
for s, e in zip(start_indices, end_indices):
    mask[s:e, :] = True


then this would be possible using

tensor[mask] = 0

