Related to: Machine Learning
개요
PyTorch와 NumPy에서 텐서의 형태를 바꾸는 두 메서드 view와 reshape의 메모리 동작 방식 차이를 정리합니다.
핵심 개념
numpy.ndarray.view와 numpy.ndarray.reshape의 차이
view는 항상 같은 메모리임을 보장,reshape의 경우 항상 같은 메모리임을 보장하지 않음-
view는 contiguity를 보장하지만reshape는 보장하지 않음
Returns a tensor with the same data and number of elements as input, but with the specified shape. When possible, the returned tensor will be a view of input. Otherwise, it will be a copy. Contiguous inputs and inputs with compatible strides can be reshaped without copying, but you should not depend on the copying vs. viewing behavior.
-
정리
| 메서드 | 메모리 공유 | Contiguity |
|---|---|---|
| view | 항상 보장 | 보장 |
| reshape | 가능하면 공유, 아니면 복사 | 미보장 |
관련 개념
- Machine Learning - 텐서 연산이 사용되는 머신러닝
- torch.mm vs torch.matmul 차이점 - 텐서 행렬 연산 관련 개념
- Pytorch Performance Tuning Practices - 메모리 효율 최적화 기법