It is not completely clear, but I cannot use a comment for this, so.
For the first case you have:
t1 = torch.tensor([[1., 1.], [1., 2.], [3., 4.], [1.,2.]]) t1.shape #=> torch.Size([4, 2]) t1 tensor([[1., 1.], [1., 2.], [3., 4.], [1., 2.]])
To get the desired output you should reshape:
tr1 = t1.reshape([2, 2, 2]) res1 = torch.sum(tr1, axis = 1) res1.shape #=> torch.Size([2, 2]) res1 tensor([[2., 3.], [4., 6.]])
Let's take a tensor with all one elements (torch.ones) for the second case.
t2 = torch.ones((20, 5)) t2.shape #=> torch.Size([20, 5]) t2 tensor([[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]])
So, reshaping to get the required (?) result:
tr2 = tensor.reshape((10, 2, 5)) res2 = torch.sum(tr2, axis = 0) res2.shape #=> torch.Size([2, 5]) res2 tensor([[10., 10., 10., 10., 10.], [10., 10., 10., 10., 10.]])
Is this what you are looking for?