Torch.sum over multidimensional tensor

I have a tensor t of shape (38, 38, 7, 7, 21) -> (x, y, i, j, c)

Supposing I have x_min, x_max, y_min, y_max and two values i,j, and a class num c

I want to sum all elements in the range

(x_min : xmax, y_min:y_max, i, j, c)

I am thinking something like
torch.sum(t[x_min : xmax, y_min:y_max, i:i+1, j:j+1, c:c+1]) but this would not work
if i = 6 for instance, so I would need if statements. Is there a cleaner way of achieving this?

Why wounldn’t it?
I think that selecting the subset you care about and calling sum is the right thing to do here.
Note that you don’t need to give ranges for dimensions where you want a single value: torch.sum(t[x_min : xmax, y_min:y_max, i, j, c]) but that will give the same result :slight_smile: