Different between permute, transpose, view? Which should I use?

Hello all, what is different among permute, transpose and view?

  1. If I have a feature size of BxCxHxW, I want to reshape it to BxCxHW where HW is a number of channels likes (H=3, W=4 then HW=3x4=12). Which one is a good option?
  2. If I have a feature size of BxCxHxW, I want to change it to BxCxWxH . Which one is a good option?
  3. If I have a feature size of BxCxH, I want to change it to BxCxHx1 . Which one is a good option?

In my opinion, it will

  1. permute
  2. transpose
  3. view
    Thanks
8 Likes

I think you habe it mixed up:

  • permute changes the order of dimensions aka axes, so 2 would be a use case. Transpose is a special case of permute, use it with 2d tensors.
  • view can combine and split axes, so 1 and 3 can use view,
  • note that view can fail for noncontiguous layouts (e.g. crop a picture using indexing), in these cases reshape will do the right thing,
  • for adding dimensions of size 1 (case 3), there also are unsqueeze and indexing with None.

Best regards

Thomas

35 Likes