코딩하는 애옹😸

[PyTorch] AutoGrad 본문

부스트캠프 AI Tech 3기/Study

[PyTorch] AutoGrad

DevYe 2022. 1. 25. 23:42
728x90
반응형

AutoGrad & Optimizer

torch.nn.Module

  • 딥러닝을 구성하는 Layer의 Base class
  • Input, Output, Forward, Backward(AutoGrad;weights 미분) 정의
  • 학습의 대상(weights)는 parameter(tensor)로 정의

nn.Parameter

  • nn.Module 내에 attribute가 될 때, (required_grad=True)로 자동 지정 -> AutoGrad의 대상이 됨
  • 대부분은 이미 만들어져 있어서, 직접 지정할 일은 잘 없음
## 모듈
class Linear(nn.Module):
 def __init__(self, in_features, out_features, bias = True):
    super().__init__()
    self.in_features = in_features
    self.out_features = out_features

    ## (in_features by out_features) 모양의 행렬
    self.weights = nn.Parameter(torch.radn(in_features, out_features))

    self.bias = nn.Parameter(torch.randn(out_features))

  def forward(self, x : Tensor):
    return x @ self.weights + self.bias

Backward

  • Parameter들의 미분
  • Forward의 결과값(output)과 loss에 대해 미분
  • 해당 값으로 Parameter 업데이트

optimizer.zero_grad() : 이전의 grad값이 지금의 학습에 영향을 끼치지 않도록 초기화

  • Module에서 backward와 optimizer 오버라이딩

PyTorch Dataset

데이터 모으고 전처리한 후 저장

-> Dataset Class

  • 데이터 입력 형식 표준화
  • __init__(): 초기 데이터 생성 방법 지정
  • __len__() : 데이터의 전체 길의
  • __getitem__() : 하나의 데이터를 불러올 때 어떻게 반환해주는지 (map-style)
  • 최근에는 HuggingFace 등 표준화된 라이브러리 사용

-> Transforms : 전처리, Tensor로 바꿈

  • Data의 Batch 생성
  • 학습직전(GPU feed전) 데이터 변환
  • sampler : 데이터를 어떻게 뽑을 것인지 인덱스 지정
  • collate_fn : data / lable 나눔. 동일하게 padding을 하기 위해 사용

-> DataLoader : transforms에서 만든 데이터들을 묶어서 모델에 feeding

반응형

'부스트캠프 AI Tech 3기 > Study' 카테고리의 다른 글

[PyTorch] Multi-GPU  (0) 2022.01.27
[PyTorch] 모델 불러오기  (0) 2022.01.26
[PyTorch] Basics  (0) 2022.01.25
[예제] BackPropagation  (0) 2022.01.23
(AI Math 10강) RNN  (0) 2022.01.21
Comments