Reshape tensor while keeping data in a given dimension

Hi,

Yes it works, actually, in your case you are extracting patches using sliding window then permuting channels.

Here is the code that will do the job for your arbitrary example:

x.unfold(2, 2, 2)[0].unfold(2, 2, 2).contiguous().view(3, -1, 2, 2).permute((1, 0, 2, 3))

But if you have question how it works, here is another post I have explained the calculations:

Bests

1 Like