How can I get the mean of 5 tensors along an axis?
I have 5 tensors of shape torch.Size([7, 20, 180])

I want to get the mean of each along dim=1 so that my final shape will be torch.Size([7, 20, 180]).

Basically, I want the first element from the first tensor, then the first element from the second tensor, and so on to be averaged.

You did not mention how these 5 tensors are stored, but let's assume they are in a list. Here's a way to do it:

import torch

x = [torch.rand((7, 20, 180)) for _ in range(5)]
y = torch.stack(x).mean(dim=0)

print(y.shape)
# &gt;&gt;&gt; torch.Size([7, 20, 180])


I'm also assuming you said dim=1 as if PyTorch was 1-based indexing, which it is not.




I see you are asking many questions recently, which is not a problem. I've said this once, but again: always try to provide a Minimal, Reproducible Example. It is always good to show some effort as well. Have you tried anything before asking?

