Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calculating mean across all dimensions of a tensor except nth dimension

I have a torch tensor of dimension [7, 12, 12, 197, 197]. I would like to calculate mean such that resultant vector is of shape [7,1,1,197,1]. Is there any other way rather than using a for loop? Could you provide answer with benchmark on execution time?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Torch provides mean along with dim as argument. This dim can take tuple of integers.

import torch
tensor = torch.randn(4, 5, 6, 7)

dims_to_reduce = (1,3)
means = torch.mean(tensor, dim=dims_to_reduce, keepdim=True)
print(means.shape)
>>torch.Size([4, 1, 6, 1])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading