Pytorch tensor dimensions - replace and multiply operations

Dear friends :slight_smile:

I have a tensor with the dimensions of x = torch.Size([128, 99, 8, 31]).
I need to multiply the second position ‘channels’ with itself to get x = torch.Size([128, (99 * 99 ), 8, 31]) = torch.Size([128, 9801, 8, 31]).
Is this logically accepted, and how to do it?


I have a tensor with the dimnsions of y = torch.Size([128, 1, 99, 99]).
I need to replace the third position dimension (99) with (1), to get y = torch.Size([128, 1, 1, 99]). how I can do that?

I have tried tensor.view and tensor.reshape but it didn’t work with me as I expect.

You likely what to think about what this should do with the values in the tensors.
For the first, there is repeat and tile which might work, the second could be achieved with some reduction (sum or mean with keepdims=True). But you really want to get the values right, too, not just the shape.

Thank you Sir. for your replay.
I’m sorry if my question looked ambiguous. Actually, I’m looking for the shape, not values.

Pytorch experts, any suggestion to work it on :slight_smile:

As @tom explained, there is not a single correct answer and it depends on what you are trying to achieve.
E.g. the second example would need a reduction (reduce the shape of 99 to 1), which can be accomplished using torch.mean, torch.sum, etc. or simply by indexing.
So the more interesting question is: what kind of operation should be used (or what are you trying yo achieve)?