Related to: Machine Learning

개요

PyTorch와 NumPy에서 텐서의 형태를 바꾸는 두 메서드 viewreshape의 메모리 동작 방식 차이를 정리합니다.

핵심 개념

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가능하면 공유, 아니면 복사미보장

관련 개념

참조

https://stackoverflow.com/questions/49643225/whats-the-difference-between-reshape-and-view-in-pytorch

Week 2