# Perform operations on a 4D tensor

I have a 4D tensor of shape say (100, 10, 5, 5).

I want to perform operations in last to dimensions.
e.g. on each (5, 5) subtensor.

Let’s say…
Mean of all 5 rows, mean of all 5 colums => 10 values
Max. of each row and column = > 10 values
Min. of each row and column = > 10 values
So, I have now 10+10+10= 30 values, which can be viewed as (5,6) subtensor.

So, I want to replace original (5, 5) subtensors by these (5, 6) subtensors such that the shape of that 4D tensor now becomes (100, 10, 5, 6) or maybe (100, 10, 30).

I want to avoid for loops because backward method is very slow in that case.

Could you show how to implement it using for loops? I couldn’t understand

10+10+10= 30 values, which can be viewed as (15,15) subtensor.

30 values could be viewed as (5, 6).

Hi @Eta_C ,

I was thinking to view it as a 3D tensor maybe and iterate through each (5, 5) sub tensors with for loop.
But that way would be extremely slow as I mentioned. I corrected the mistake Thanks.

Hi @Hdk

I has a similar use case where a tensor lets say A, A.shape = (100,10,5,5)
I wanted to add the last two dimensions such that new Ashape = (100,10,5) and following did the trick

A.sum(axis=2)

Hopefully it give you more ideas.

regards,

``````result = torch.cat(