Rearrange a 5D tensor in PyTorch

Question:

I have a 5D tensor in the shape of (N,C,T,H,W). I want to rearrange it using PyTorch to the form of (N,T,HW,C). How can I do that?

Asked By: dtr43

||

Answers:

Naturally you can reshape the last two dimensions of your tensor by flattening your tensor from dim=-2, this will produce a shape of (N,C,T,HW):

>>> x.flatten(-2)

Then you can permute the dimensions around:

>>> x.flatten(-2).permute(0,2,3,1)
Answered By: Ivan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.