부스트캠프 AI Tech 4기
[PyTorch] 15. Pytorch Basic
StoneSeller
2022. 9. 26. 11:29
가장 많이 사용되는 딥러닝 프레임워크의 종류에는 PyTorch와 TensorFlow가 있다.
딥러닝 프레임워크의 기본적인 작동 구조
PyTorch | TensorFlow |
Dynamic Computation Graph Define by Run |
Define and Run |
실행을 하면서 그래프를 생성 | 그래프를 먼저 정의 → 실행시점에 데이터를 feed |
PyTorch의 장점
- Define by Run의 장점으로 즉시 확인이 가능하다.
TensorFlow의 장점
- Production과 Scalability의 장점
Pytorch's Operation
Tensor
- 다차원 Arrays를 표현하는 PyTorch 클래스
- numpy의 ndarray와 동일하다.
import numpy as np
n_array = np.arange(10).reshape(2,5)
print(n_array)
print("n_dim :",n_array.ndim, "shape: ", n_array.shape)
import torch
t_array = torch.FloatTensor(n_array)
print(t_array)
print("n_dim :", t_array.ndim, "shape: ", t_array.shape)
- tensor가 가질 수 있는 data 타입은 numpy와 동일하고 GPU를 사용할 수 있느냐 없느냐가 차이점이다.
Tensor handling
- view : tensor의 shape을 변환(reshape같은 역할)
view와 reshape의 차이점
torch.view와 torch.reshape의 가장 큰 차이는 contiguous 속성을 만족하지 않는 텐서에 적용이 가능하느냐 여부이다.
view는 contiguous 속성이 만족되지 않는 경우 일부 사용이 제한될 수 있다.
- view : contiguous tensor에서만 작동
contiguous tensor를 반환 - reshape : contiguous tensor에서는 view와 동일하게 작동
non-contiguous tensor에서는 data를 copy한다.
reshape를 사용한 경우 non-contiguous 텐서에 대해서는 data를 copy해오기 때문에 a를 1로 바꿔도 b에는 변화가 없다.
contiguous란 해석 그대로 인접성을 의미하는데 여기서 contiguous의 의미는 data들이 메모리상에서 실제로 인접해있는지를 의미한다.
- squeeze: 차원의 개수가 1인 차원을 삭제(압축)
- unsqueeze: 차원의 개수가 1인 차원을 추가
- 행렬 곱셈 연산은 dot 함수가 아닌 mm 또는 matmul을 사용해야 한다.
- matmul은 broadcasting을 지원해준다.
Tensor Operations for ML/DL formula
- nn.functional 모듈을 통해 다양한 수식 변환을 지원
AutoGrad
- PyTorch의 핵심은 자동 미분의 지원이다!
- backward 함수를 사용한다.
$$ y = w^{2} $$
$$ z = 10 * y + 25 = 10 * w^{2} + 25 $$
$ \frac{\partial z}{\partial w} $ 를 구해주면 $20w$이다.
부스트캠프 AI Tech 교육 자료를 참고하였습니다.
728x90