Pytorch view, reshape, permute, transpose 차이 - jinwooklim/my-exp GitHub Wiki
Reference
https://sanghyu.tistory.com/3
https://subinium.github.io/pytorch-Tensor-Variable/#%EC%B0%A8%EC%9B%90-%EC%9E%AC%EA%B5%AC%EC%84%B1-1--reshape--view
Pytorch
Dimention 모양 변경
view : dimention의 모양을 변경할 때 사용한다. (B*T, C) -> (B, T, C) // (contiguous)
reshape : dimention의 모양을 변경할 때 사용한다. // non-contiguous, 기본적으로 view 처럼 return하지만 non-contiguous한 경우, 새로 contiguous하게 만들어서 return함.
따라서, view는 메모리 같은 메모리를 공유하는 것을 보장하지만, reshape는 같은 메모리를 공유하지 않을 수 있다.
차원의 순서 변경
permute , transpose : 이미 나뉘어져 있는 tensor의 axis를 바꿀 때 사용한다 (B, T, C) -> (T, B, C) // non-contiguous
permute, transpose는 non-contiguous 하다 (붙어 있는 값이, 메모리 레벨에서는 서로 붙어 있지 않다는 뜻)
따라서, permute, transpose이후 contiguous()를 사용해주면 좋다.