Sum over dim=1 every 10 matrices

Let’s say I have a tensor of the size
torch.Size([512, 5000, 14, 14])

and I want to sum each 10 entries in second dim, e.g
sum [512, 0:10, :, :] over dim 1
sum [512, 10:20, :, :] over dim 1

So the resulting tensor is of size
[512, 500, 14, 14]

Is there any way to do it efficiently without loops?

Hi,

You can first view the Tensor before doing the reduction:
t.view(512, 500, 10, 14, 14).sum(2)

1 Like